Self hosted FLOSS fitness/workout, nutrition and weight tracker
Fix the routine copy method
Roland Geider137db549f16c
diff --git a/wger/manager/api/views.py b/wger/manager/api/views.pyindex f62dd9922..c9a7bc813 100644--- a/wger/manager/api/views.py+++ b/wger/manager/api/views.py@@ -19,7 +19,6 @@from django.conf import settingsfrom django.core.cache import cachefrom django.db.models import Q-# Third Partyfrom rest_framework import viewsetsfrom rest_framework.decorators import action@@ -165,7 +164,7 @@ class RoutineViewSet(viewsets.ModelViewSet):@action(detail=True)def structure(self, request, pk):"""- Return full object structure of the routine.+ Return the full object structure of the routine."""cache_key = CacheKeyMapper.routine_api_structure_key(pk)cached_data = cache.get(cache_key)diff --git a/wger/manager/models/routine.py b/wger/manager/models/routine.pyindex 221679f1c..88d3bdf9a 100644--- a/wger/manager/models/routine.py+++ b/wger/manager/models/routine.py@@ -52,7 +52,6 @@ from wger.manager.models import ()from wger.utils.cache import CacheKeyMapper-logger = logging.getLogger(__name__)@@ -161,6 +160,10 @@ class Routine(models.Model):super().save(*args, **kwargs)+ @property+ def duration(self) -> datetime.timedelta:+ return self.end - self.start+@propertydef label_dict(self) -> dict[datetime.date, str]:out = {}diff --git a/wger/manager/tests/test_copy_routine.py b/wger/manager/tests/test_copy_routine.pyindex 7e69e452e..c11517b13 100644--- a/wger/manager/tests/test_copy_routine.py+++ b/wger/manager/tests/test_copy_routine.py@@ -11,7 +11,7 @@# GNU General Public License for more details.## You should have received a copy of the GNU Affero General Public License-+import datetime# Standard Libraryimport logging@@ -22,7 +22,6 @@ from django.urls import reversefrom wger.core.tests.base_testcase import WgerTestCasefrom wger.manager.models import Routine-logger = logging.getLogger(__name__)@@ -45,7 +44,14 @@ class CopyRoutineTestCase(WgerTestCase):self.assertEqual(count_after, 6)routine_original = Routine.objects.get(pk=3)- routine_copy = Routine.objects.get(pk=4)+ routine_copy = Routine.objects.get(pk=6)++ self.assertEqual(routine_copy.name, routine_original.name)+ self.assertEqual(routine_copy.description, routine_original.description)+ self.assertEqual(routine_copy.is_template, routine_original.is_template)+ self.assertEqual(routine_copy.is_public, routine_original.is_public)+ self.assertEqual(routine_copy.start, datetime.date.today())+ self.assertEqual(routine_copy.end, datetime.date.today() + routine_original.duration)days_original = routine_original.days.all()days_copy = routine_copy.days.all()diff --git a/wger/manager/views/routine.py b/wger/manager/views/routine.pyindex 84e3d3cd9..0aab7bc44 100644--- a/wger/manager/views/routine.py+++ b/wger/manager/views/routine.py@@ -16,6 +16,7 @@# Standard Libraryimport copy+import datetimeimport loggingfrom typing import List@@ -34,7 +35,6 @@ from wger.manager.models import (SlotEntry,)-logger = logging.getLogger(__name__)@@ -63,9 +63,15 @@ def copy_routine(request, pk):# Copy workoutroutine_copy: Routine = copy.copy(routine)routine_copy.pk = None+ routine_copy.created = Noneroutine_copy.user = request.userroutine_copy.is_template = Falseroutine_copy.is_public = False++ # Update the start and end date+ routine_copy.start = datetime.date.today()+ routine_copy.end = routine_copy.start + routine.duration+routine_copy.save()# Copy the days