Self hosted FLOSS fitness/workout, nutrition and weight tracker
Move copy view to routine file
Roland Geidera00a79ec2169
diff --git a/wger/manager/api/views.py b/wger/manager/api/views.pyindex e437f3c06..99d023d17 100644--- a/wger/manager/api/views.py+++ b/wger/manager/api/views.py@@ -57,7 +57,6 @@ from wger.manager.api.serializers import (WorkoutSessionSerializer,WorkoutTemplateSerializer,)-from wger.manager.dataclasses import RoutineLogDatafrom wger.manager.models import (Day,MaxRepsConfig,@@ -73,7 +72,6 @@ from wger.manager.models import (Slot,SlotEntry,WeightConfig,- Workout,WorkoutLog,WorkoutSession,)diff --git a/wger/manager/tests/test_copy_workout.py b/wger/manager/tests/test_copy_routine.pysimilarity index 93%rename from wger/manager/tests/test_copy_workout.pyrename to wger/manager/tests/test_copy_routine.pyindex 8b4232f39..54adf6213 100644--- a/wger/manager/tests/test_copy_workout.py+++ b/wger/manager/tests/test_copy_routine.py@@ -26,9 +26,9 @@ from wger.manager.models import Routinelogger = logging.getLogger(__name__)-class CopyWorkoutTestCase(WgerTestCase):+class CopyRoutineTestCase(WgerTestCase):"""- Tests copying a workout or template+ Tests copying a routine or template"""def copy_routine_and_assert(self):@@ -38,7 +38,7 @@ class CopyWorkoutTestCase(WgerTestCase):# Copy the routinecount_before = Routine.objects.count()- self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))+ self.client.get(reverse('manager:routine:copy', kwargs={'pk': '3'}))count_after = Routine.objects.count()self.assertGreater(count_after, count_before)@@ -108,7 +108,7 @@ class CopyWorkoutTestCase(WgerTestCase):Test copying a workout (not template) from another user"""self.user_login('admin')- response = self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))+ response = self.client.get(reverse('manager:routine:copy', kwargs={'pk': '3'}))self.assertEqual(response.status_code, 403)def test_copy_template_no_public_other_user(self):@@ -120,7 +120,7 @@ class CopyWorkoutTestCase(WgerTestCase):routine.save()self.user_login('admin')- response = self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))+ response = self.client.get(reverse('manager:routine:copy', kwargs={'pk': '3'}))self.assertEqual(response.status_code, 403)def test_copy_template_no_public_owner_user(self):@@ -132,7 +132,7 @@ class CopyWorkoutTestCase(WgerTestCase):routine.save()self.user_login('test')- response = self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))+ response = self.client.get(reverse('manager:routine:copy', kwargs={'pk': '3'}))self.assertEqual(response.status_code, 200)def test_copy_template_public_other_user(self):@@ -145,5 +145,5 @@ class CopyWorkoutTestCase(WgerTestCase):routine.save()self.user_login('admin')- response = self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))+ response = self.client.get(reverse('manager:routine:copy', kwargs={'pk': '3'}))self.assertEqual(response.status_code, 200)diff --git a/wger/manager/urls.py b/wger/manager/urls.pyindex c829812f3..e72df2442 100644--- a/wger/manager/urls.py+++ b/wger/manager/urls.py@@ -29,6 +29,7 @@ from wger.manager.views import (ical,log,pdf,+ routine,schedule,schedule_step,workout,@@ -80,11 +81,6 @@ patterns_templates = [# sub patterns for workoutspatterns_workout = [- path(- '<int:pk>/copy',- workout.copy_routine,- name='copy',- ),path('<int:pk>/make-template',workout.WorkoutMarkAsTemplateView.as_view(),@@ -125,23 +121,8 @@ patterns_workout = [pdf.workout_log,name='pdf-log',),- re_path(- r'^(?P<id>\d+)/pdf/table/(?P<images>[01]+)/(?P<comments>[01]+)/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,33})$',- pdf.workout_view,- name='pdf-table',- ), # JS!- re_path(- r'^(?P<id>\d+)/pdf/table/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,33})$',- pdf.workout_view,- name='pdf-table',- ),- re_path(- r'^(?P<id>\d+)/pdf/table/(?P<images>[01]+)/(?P<comments>[01]+)$',- pdf.workout_view,- name='pdf-table',- ),path(- '<int:id>/pdf/table',+ '<int:pk>/pdf/table',pdf.workout_view,name='pdf-table',),@@ -154,11 +135,21 @@ patterns_routine = [ReactView.as_view(login_required=True),name='overview',),+ path(+ 'add',+ ReactView.as_view(login_required=True),+ name='add',+ ),path('<int:pk>/view',ReactView.as_view(login_required=True),name='view',),+ path(+ '<int:pk>/copy',+ routine.copy_routine,+ name='copy',+ ),]# sub patterns for workout sessionsdiff --git a/wger/manager/views/routine.py b/wger/manager/views/routine.pynew file mode 100644index 000000000..c25f33562--- /dev/null+++ b/wger/manager/views/routine.py@@ -0,0 +1,96 @@+# -*- coding: utf-8 -*-++# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++# Standard Library+import copy+import logging+from typing import List++# Django+from django.contrib.auth.decorators import login_required+from django.http import (+ HttpResponseForbidden,+ HttpResponseRedirect,+)+from django.shortcuts import get_object_or_404++# wger+from wger.manager.models import (+ AbstractChangeConfig,+ Routine,+ SlotEntry,+)++logger = logging.getLogger(__name__)+++@login_required+def copy_routine(request, pk):+ """+ Makes a copy of a routine+ """+ routine = get_object_or_404(Routine, pk=pk)++ if request.user != routine.user:+ return HttpResponseForbidden()++ def copy_config(configs: List[AbstractChangeConfig], slot_entry: SlotEntry):+ for config in configs:+ config_copy = copy.copy(config)+ config_copy.pk = None+ config_copy.slot_entry = slot_entry+ config_copy.save()++ # Process request+ # Copy workout+ routine_copy: Routine = copy.copy(routine)+ routine_copy.pk = None+ routine_copy.user = request.user+ routine_copy.is_template = False+ routine_copy.is_public = False+ routine_copy.save()++ # Copy the days+ for day in routine.days.all():+ day_copy = copy.copy(day)+ day_copy.pk = None+ day_copy.routine = routine_copy+ day_copy.save()++ # Copy the slots+ for current_slot in day.slots.all():+ slot_copy = copy.copy(current_slot)+ slot_copy.pk = None+ slot_copy.day = day_copy+ slot_copy.save()++ # Copy the slot entries+ for current_entry in current_slot.entries.all():+ slot_entry_copy = copy.copy(current_entry)+ slot_entry_copy.pk = None+ slot_entry_copy.slot = slot_copy+ slot_entry_copy.save()++ copy_config(current_entry.weightconfig_set.all(), slot_entry_copy)+ copy_config(current_entry.maxweightconfig_set.all(), slot_entry_copy)+ copy_config(current_entry.repsconfig_set.all(), slot_entry_copy)+ copy_config(current_entry.maxrepsconfig_set.all(), slot_entry_copy)+ copy_config(current_entry.rirconfig_set.all(), slot_entry_copy)+ copy_config(current_entry.restconfig_set.all(), slot_entry_copy)+ copy_config(current_entry.maxrestconfig_set.all(), slot_entry_copy)+ copy_config(current_entry.setsconfig_set.all(), slot_entry_copy)++ return HttpResponseRedirect(routine_copy.get_absolute_url())diff --git a/wger/manager/views/workout.py b/wger/manager/views/workout.pyindex adf51a5e3..393168495 100644--- a/wger/manager/views/workout.py+++ b/wger/manager/views/workout.py@@ -43,6 +43,7 @@ from wger.manager.models import ()from wger.utils.generic_views import WgerFormMixin+logger = logging.getLogger(__name__)@@ -98,65 +99,6 @@ def template_view(request, pk):return render(request, 'workout/template_view.html', context)-@login_required-def copy_routine(request, pk):- """- Makes a copy of a routine- """- routine = get_object_or_404(Routine, pk=pk)-- if request.user != routine.user:- return HttpResponseForbidden()-- def copy_config(configs: List[AbstractChangeConfig], slot_entry: SlotEntry):- for config in configs:- config_copy = copy.copy(config)- config_copy.pk = None- config_copy.slot_entry = slot_entry- config_copy.save()-- # Process request- # Copy workout- routine_copy: Routine = copy.copy(routine)- routine_copy.pk = None- routine_copy.user = request.user- routine_copy.is_template = False- routine_copy.is_public = False- routine_copy.save()-- # Copy the days- for day in routine.days.all():- day_copy = copy.copy(day)- day_copy.pk = None- day_copy.routine = routine_copy- day_copy.save()-- # Copy the slots- for current_slot in day.slots.all():- slot_copy = copy.copy(current_slot)- slot_copy.pk = None- slot_copy.day = day_copy- slot_copy.save()-- # Copy the slot entries- for current_entry in current_slot.entries.all():- slot_entry_copy = copy.copy(current_entry)- slot_entry_copy.pk = None- slot_entry_copy.slot = slot_copy- slot_entry_copy.save()-- copy_config(current_entry.weightconfig_set.all(), slot_entry_copy)- copy_config(current_entry.maxweightconfig_set.all(), slot_entry_copy)- copy_config(current_entry.repsconfig_set.all(), slot_entry_copy)- copy_config(current_entry.maxrepsconfig_set.all(), slot_entry_copy)- copy_config(current_entry.rirconfig_set.all(), slot_entry_copy)- copy_config(current_entry.restconfig_set.all(), slot_entry_copy)- copy_config(current_entry.maxrestconfig_set.all(), slot_entry_copy)- copy_config(current_entry.setsconfig_set.all(), slot_entry_copy)-- return HttpResponseRedirect(routine_copy.get_absolute_url())--def make_workout(request, pk):workout = get_object_or_404(Workout.both, pk=pk)