Skip to content

Commit 137db54

Browse files
committed
Fix the routine copy method
We set the start date of the copied routine to the current date, and the duration to be the same as the original.
1 parent 8241be2 commit 137db54

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

wger/manager/api/views.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
from django.conf import settings
2020
from django.core.cache import cache
2121
from django.db.models import Q
22-
2322
# Third Party
2423
from rest_framework import viewsets
2524
from rest_framework.decorators import action
@@ -165,7 +164,7 @@ def date_sequence_gym_mode(self, request, pk):
165164
@action(detail=True)
166165
def structure(self, request, pk):
167166
"""
168-
Return full object structure of the routine.
167+
Return the full object structure of the routine.
169168
"""
170169
cache_key = CacheKeyMapper.routine_api_structure_key(pk)
171170
cached_data = cache.get(cache_key)

wger/manager/models/routine.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@
5252
)
5353
from wger.utils.cache import CacheKeyMapper
5454

55-
5655
logger = logging.getLogger(__name__)
5756

5857

@@ -161,6 +160,10 @@ def save(self, *args, **kwargs):
161160

162161
super().save(*args, **kwargs)
163162

163+
@property
164+
def duration(self) -> datetime.timedelta:
165+
return self.end - self.start
166+
164167
@property
165168
def label_dict(self) -> dict[datetime.date, str]:
166169
out = {}

wger/manager/tests/test_copy_routine.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# GNU General Public License for more details.
1212
#
1313
# You should have received a copy of the GNU Affero General Public License
14-
14+
import datetime
1515
# Standard Library
1616
import logging
1717

@@ -22,7 +22,6 @@
2222
from wger.core.tests.base_testcase import WgerTestCase
2323
from wger.manager.models import Routine
2424

25-
2625
logger = logging.getLogger(__name__)
2726

2827

@@ -45,7 +44,14 @@ def copy_routine_and_assert(self):
4544
self.assertEqual(count_after, 6)
4645

4746
routine_original = Routine.objects.get(pk=3)
48-
routine_copy = Routine.objects.get(pk=4)
47+
routine_copy = Routine.objects.get(pk=6)
48+
49+
self.assertEqual(routine_copy.name, routine_original.name)
50+
self.assertEqual(routine_copy.description, routine_original.description)
51+
self.assertEqual(routine_copy.is_template, routine_original.is_template)
52+
self.assertEqual(routine_copy.is_public, routine_original.is_public)
53+
self.assertEqual(routine_copy.start, datetime.date.today())
54+
self.assertEqual(routine_copy.end, datetime.date.today() + routine_original.duration)
4955

5056
days_original = routine_original.days.all()
5157
days_copy = routine_copy.days.all()

wger/manager/views/routine.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
# Standard Library
1818
import copy
19+
import datetime
1920
import logging
2021
from typing import List
2122

@@ -34,7 +35,6 @@
3435
SlotEntry,
3536
)
3637

37-
3838
logger = logging.getLogger(__name__)
3939

4040

@@ -63,9 +63,15 @@ def copy_config(configs: List[AbstractChangeConfig], slot_entry: SlotEntry):
6363
# Copy workout
6464
routine_copy: Routine = copy.copy(routine)
6565
routine_copy.pk = None
66+
routine_copy.created = None
6667
routine_copy.user = request.user
6768
routine_copy.is_template = False
6869
routine_copy.is_public = False
70+
71+
# Update the start and end date
72+
routine_copy.start = datetime.date.today()
73+
routine_copy.end = routine_copy.start + routine.duration
74+
6975
routine_copy.save()
7076

7177
# Copy the days

0 commit comments

Comments
 (0)