Skip to content

armgit / wger

Public Imported from GitHub
0

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.py
index f62dd9922..c9a7bc813 100644
--- a/wger/manager/api/views.py
+++ b/wger/manager/api/views.py
@@ -19,7 +19,6 @@
from django.conf import settings
from django.core.cache import cache
from django.db.models import Q
-
# Third Party
from rest_framework import viewsets
from 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.py
index 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
+
@property
def 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.py
index 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 Library
import logging
@@ -22,7 +22,6 @@ from django.urls import reverse
from wger.core.tests.base_testcase import WgerTestCase
from 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.py
index 84e3d3cd9..0aab7bc44 100644
--- a/wger/manager/views/routine.py
+++ b/wger/manager/views/routine.py
@@ -16,6 +16,7 @@
# Standard Library
import copy
+import datetime
import logging
from 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 workout
routine_copy: Routine = copy.copy(routine)
routine_copy.pk = None
+ routine_copy.created = None
routine_copy.user = request.user
routine_copy.is_template = False
routine_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