Self hosted FLOSS fitness/workout, nutrition and weight tracker
Remove some unused views
Roland Geider87a585dfd2e4
diff --git a/wger/core/templates/navigation.html b/wger/core/templates/navigation.htmlindex 2a6785d5f..722cd12b6 100644--- a/wger/core/templates/navigation.html+++ b/wger/core/templates/navigation.html@@ -22,14 +22,14 @@{# Workout tab #}{# #}<li class="nav-item dropdown">- <a href="{% url 'manager:workout:overview' %}"+ <a href="{% url 'manager:routine:overview' %}"rel="nofollow"class="nav-link dropdown-toggle text-white {% if active_tab == 'workout' %}active{% endif %}"data-bs-toggle="dropdown">{% translate "Training" %} <bclass="caret"></b></a><ul class="dropdown-menu"><li>- <a class="dropdown-item" href="{% url 'manager:workout:overview' %}"+ <a class="dropdown-item" href="{% url 'manager:routine:overview' %}"rel="nofollow">{% if not user.is_authenticated %}<span class="{% fa_class 'lock' %}"></span>diff --git a/wger/manager/api/views.py b/wger/manager/api/views.pyindex 5b9845f5f..e437f3c06 100644--- a/wger/manager/api/views.py+++ b/wger/manager/api/views.py@@ -247,9 +247,9 @@ class WorkoutViewSet(viewsets.ModelViewSet):"""# REST API generationif getattr(self, 'swagger_fake_view', False):- return Workout.objects.none()+ return Routine.objects.none()- return Workout.objects.filter(user=self.request.user)+ return Routine.objects.filter(user=self.request.user)def perform_create(self, serializer):"""@@ -301,9 +301,9 @@ class UserWorkoutTemplateViewSet(viewsets.ReadOnlyModelViewSet):"""# REST API generationif getattr(self, 'swagger_fake_view', False):- return Workout.objects.none()+ return Routine.objects.none()- return Workout.templates.filter(user=self.request.user)+ return Routine.templates.filter(user=self.request.user)def perform_create(self, serializer):"""@@ -326,7 +326,7 @@ class PublicWorkoutTemplateViewSet(viewsets.ModelViewSet):"""Only allow access to appropriate objects"""- return Workout.templates.filter(is_public=True)+ return Routine.templates.filter(is_public=True)def perform_create(self, serializer):"""@@ -373,7 +373,7 @@ class WorkoutSessionViewSet(WgerOwnerObjectModelViewSet):"""Return objects to check for ownership permission"""- return [(Workout, 'workout')]+ return [(Routine, 'workout')]class ScheduleStepViewSet(WgerOwnerObjectModelViewSet):@@ -405,7 +405,7 @@ class ScheduleStepViewSet(WgerOwnerObjectModelViewSet):"""Return objects to check for ownership permission"""- return [(Workout, 'workout'), (Schedule, 'schedule')]+ return [(Routine, 'workout'), (Schedule, 'schedule')]class ScheduleViewSet(viewsets.ModelViewSet):@@ -478,7 +478,7 @@ class WorkoutLogViewSet(WgerOwnerObjectModelViewSet):"""Return objects to check for ownership permission"""- return [(Workout, 'workout')]+ return [(Routine, 'workout')]class RoutineDayViewSet(WgerOwnerObjectModelViewSet):diff --git a/wger/manager/tests/test_copy_workout.py b/wger/manager/tests/test_copy_workout.pyindex d03c11ddc..8b4232f39 100644--- a/wger/manager/tests/test_copy_workout.py+++ b/wger/manager/tests/test_copy_workout.py@@ -19,9 +19,8 @@ import loggingfrom django.urls import reverse# wger-from wger.core.models import UserProfilefrom wger.core.tests.base_testcase import WgerTestCase-from wger.manager.models import Workout+from wger.manager.models import Routinelogger = logging.getLogger(__name__)@@ -32,70 +31,62 @@ class CopyWorkoutTestCase(WgerTestCase):Tests copying a workout or template"""- def copy_workout(self):+ def copy_routine_and_assert(self):"""- Helper function to test copying workouts+ Helper function to test copying routines"""- # Open the copy workout form- response = self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))- self.assertEqual(response.status_code, 200)-- # Copy the workout- count_before = Workout.objects.count()- self.client.post(- reverse('manager:workout:copy', kwargs={'pk': '3'}),- {'name': 'A copied workout'},- )- count_after = Workout.objects.count()+ # Copy the routine+ count_before = Routine.objects.count()+ self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))+ count_after = Routine.objects.count()self.assertGreater(count_after, count_before)self.assertEqual(count_after, 4)- self.assertTemplateUsed('workout/view.html')-- # Test accessing the copied workout- response = self.client.get(reverse('manager:workout:view', kwargs={'pk': 4}))- self.assertEqual(response.status_code, 200)- workout_original = Workout.objects.get(pk=3)- workout_copy = Workout.objects.get(pk=4)+ routine_original = Routine.objects.get(pk=3)+ routine_copy = Routine.objects.get(pk=4)- days_original = workout_original.day_set.all()- days_copy = workout_copy.day_set.all()+ days_original = routine_original.days.all()+ days_copy = routine_copy.days.all()# Test that the different attributes and objects are correctly copied over- for i in range(0, workout_original.day_set.count()):+ for i in range(0, routine_original.days.count()):+ self.assertEqual(days_original[i].order, days_copy[i].order)+ self.assertEqual(days_original[i].name, days_copy[i].name)self.assertEqual(days_original[i].description, days_copy[i].description)-- for j in range(0, days_original[i].day.count()):- self.assertEqual(days_original[i].day.all()[j], days_copy[i].day.all()[j])-- sets_original = days_original[i].set_set.all()- sets_copy = days_copy[i].set_set.all()-- for j in range(days_original[i].set_set.count()):- self.assertEqual(sets_original[j].sets, sets_copy[j].sets)- self.assertEqual(sets_original[j].order, sets_copy[j].order)- self.assertEqual(sets_original[j].comment, sets_copy[j].comment)-- bases_original = sets_original[j].exercise_bases- bases_copy = sets_copy[j].exercise_bases-- for k in range(len(sets_original[j].exercise_bases)):- self.assertEqual(bases_original[k], bases_copy[k])-- settings_original = sets_original[j].setting_set.all()- settings_copy = sets_copy[j].setting_set.all()-- for l in range(settings_original.count()):- setting_copy = settings_copy[l]- setting_orig = settings_original[l]-- self.assertEqual(setting_orig.repetition_unit, setting_copy.repetition_unit)- self.assertEqual(setting_orig.weight_unit, setting_copy.weight_unit)- self.assertEqual(setting_orig.reps, setting_copy.reps)- self.assertEqual(setting_orig.weight, setting_copy.weight)- self.assertEqual(setting_orig.rir, setting_copy.rir)+ self.assertEqual(days_original[i].type, days_copy[i].type)+ self.assertEqual(days_original[i].is_rest, days_copy[i].is_rest)+ self.assertEqual(+ days_original[i].need_logs_to_advance, days_copy[i].need_logs_to_advance+ )++ slots_original = days_original[i].slots.all()+ slots_copy = days_copy[i].slots.all()++ for j in range(days_original[i].slots.count()):+ self.assertEqual(slots_original[j].order, slots_copy[j].order)+ self.assertEqual(slots_original[j].comment, slots_copy[j].comment)++ slot_entries_original = slots_original[j].entries.all()+ slot_entries_copy = slots_copy[j].entries.all()++ for l in range(slot_entries_original.count()):+ entry_copy = slot_entries_copy[l]+ entry_orig = slot_entries_original[l]++ self.assertEqual(entry_orig.exercise_id, entry_copy.exercise_id)+ self.assertEqual(entry_orig.repetition_unit_id, entry_copy.repetition_unit_id)+ self.assertEqual(entry_orig.repetition_rounding, entry_copy.repetition_rounding)+ self.assertEqual(entry_orig.weight_unit_id, entry_copy.weight_unit_id)+ self.assertEqual(entry_orig.weight_rounding, entry_copy.weight_rounding)+ self.assertEqual(entry_orig.type, entry_copy.type)++ # TODO: check the rest of the config objects+ configs_orig = entry_orig.setsconfig_set.all()+ configs_copy = entry_copy.setsconfig_set.all()+ for m in range(entry_orig.setsconfig_set.count()):+ self.assertEqual(configs_orig[m].value, configs_copy[m].value)def test_copy_workout_owner(self):"""@@ -103,15 +94,14 @@ class CopyWorkoutTestCase(WgerTestCase):"""self.user_login('test')- self.copy_workout()+ self.copy_routine_and_assert()def test_copy_workout(self):"""Test copying a workout (not template)"""self.user_login('test')- response = self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))- self.assertEqual(response.status_code, 200)+ self.copy_routine_and_assert()def test_copy_workout_other(self):"""@@ -125,9 +115,9 @@ class CopyWorkoutTestCase(WgerTestCase):"""Test copying a workout template that is not marked as public and belongs to another user"""- workout = Workout.objects.get(pk=3)- workout.is_template = True- workout.save()+ routine = Routine.objects.get(pk=3)+ routine.is_template = True+ routine.save()self.user_login('admin')response = self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))@@ -137,9 +127,9 @@ class CopyWorkoutTestCase(WgerTestCase):"""Test copying a workout template that is not marked as public and belongs to the current user"""- workout = Workout.objects.get(pk=3)- workout.is_template = True- workout.save()+ routine = Routine.objects.get(pk=3)+ routine.is_template = True+ routine.save()self.user_login('test')response = self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))@@ -149,10 +139,10 @@ class CopyWorkoutTestCase(WgerTestCase):"""Test copying a workout template that is marked as public and belongs to another user"""- workout = Workout.objects.get(pk=3)- workout.is_template = True- workout.is_public = True- workout.save()+ routine = Routine.objects.get(pk=3)+ routine.is_template = True+ routine.is_public = True+ routine.save()self.user_login('admin')response = self.client.get(reverse('manager:workout:copy', kwargs={'pk': '3'}))diff --git a/wger/manager/urls.py b/wger/manager/urls.pyindex d0881a6bc..0173de856 100644--- a/wger/manager/urls.py+++ b/wger/manager/urls.py@@ -35,7 +35,6 @@ from wger.manager.views import (workout_session,)-# sub patterns for workout logspatterns_log = [path(@@ -81,42 +80,16 @@ patterns_templates = [# sub patterns for workoutspatterns_workout = [- path(- 'overview',- ReactView.as_view(login_required=True),- name='overview',- ),- path(- 'add',- workout.add,- name='add',- ),path('<int:pk>/copy',- workout.copy_workout,+ workout.copy_routine,name='copy',),- path(- '<int:pk>/edit',- workout.WorkoutEditView.as_view(),- name='edit',- ),path('<int:pk>/make-template',workout.WorkoutMarkAsTemplateView.as_view(),name='make-template',),- path(- '<int:pk>/delete',- workout.WorkoutDeleteView.as_view(),- name='delete',- ),- path(- '<int:pk>/view',- # ReactView.as_view(login_required=True),- ReactView.as_view(),- name='view',- ),re_path(r'^calendar/(?P<username>[\w.@+-]+)$',log.calendar,@@ -203,8 +176,7 @@ patterns_routine = [),path('<int:pk>/view',- # ReactView.as_view(login_required=True),- workout.view,+ ReactView.as_view(login_required=True),name='view',),]@@ -333,7 +305,7 @@ patterns_step = [urlpatterns = [path('', include((patterns_workout, 'workout'), namespace='workout')),- path('routine/', include((patterns_routine, 'routine'), namespace='routine')),+ path('', include((patterns_routine, 'routine'), namespace='routine')),path('template/', include((patterns_templates, 'template'), namespace='template')),path('log/', include((patterns_log, 'log'), namespace='log')),path('session/', include((patterns_session, 'session'), namespace='session')),diff --git a/wger/manager/views/workout.py b/wger/manager/views/workout.pyindex 5241bf59c..adf51a5e3 100644--- a/wger/manager/views/workout.py+++ b/wger/manager/views/workout.py@@ -17,6 +17,7 @@# Standard Libraryimport copyimport logging+from typing import List# Djangofrom django.contrib.auth.decorators import login_required@@ -29,45 +30,22 @@ from django.shortcuts import (get_object_or_404,render,)-from django.template.context_processors import csrf-from django.urls import (- reverse,- reverse_lazy,-)-from django.utils.text import slugify-from django.utils.translation import (- gettext as _,- gettext_lazy,-)-from django.views.generic import (- DeleteView,- UpdateView,-)--# Third Party-from crispy_forms.helper import FormHelper-from crispy_forms.layout import Submit+from django.utils.translation import gettext as _+from django.views.generic import UpdateView# wger-from wger.manager.forms import (- WorkoutCopyForm,- WorkoutForm,- WorkoutMakeTemplateForm,-)+from wger.manager.forms import WorkoutMakeTemplateFormfrom wger.manager.models import (- Schedule,+ AbstractChangeConfig,+ Routine,+ SlotEntry,Workout,- WorkoutLog,-)-from wger.utils.generic_views import (- WgerDeleteMixin,- WgerFormMixin,)-from wger.utils.helpers import make_token-+from wger.utils.generic_views import WgerFormMixinlogger = logging.getLogger(__name__)+# ************************# Workout functions# ************************@@ -101,30 +79,6 @@ def public_template_overview(request):)-def view(request, pk):- """- Show the workout with the given ID- """- workout = get_object_or_404(Workout, pk=pk)- user = workout.user- is_owner = request.user == user-- if not is_owner and not user.userprofile.ro_access:- return HttpResponseForbidden()-- uid, token = make_token(user)-- context = {- 'workout': workout,- 'uid': uid,- 'token': token,- 'is_owner': is_owner,- 'owner_user': user,- }-- return render(request, 'workout/view.html', context)--@login_required()def template_view(request, pk):"""@@ -145,73 +99,62 @@ def template_view(request, pk):@login_required-def copy_workout(request, pk):+def copy_routine(request, pk):"""- Makes a copy of a workout+ Makes a copy of a routine"""- workout = get_object_or_404(Workout.both, pk=pk)+ routine = get_object_or_404(Routine, pk=pk)- if not workout.is_public and request.user != workout.user:+ 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- if request.method == 'POST':- workout_form = WorkoutCopyForm(request.POST)-- if workout_form.is_valid():- # Copy workout- workout_copy: Workout = copy.copy(workout)- workout_copy.pk = None- workout_copy.name = workout_form.cleaned_data['name']- workout_copy.user = request.user- workout_copy.is_template = False- workout_copy.is_public = False- workout_copy.save()-- # Copy the days- for day in workout.day_set.all():- day_copy = copy.copy(day)- day_copy.pk = None- day_copy.training = workout_copy- day_copy.save()- for i in day.day.all():- day_copy.day.add(i)- day_copy.save()-- # Copy the sets- for current_set in day.set_set.all():- current_set_copy = copy.copy(current_set)- current_set_copy.pk = None- current_set_copy.exerciseday = day_copy- current_set_copy.save()-- # Copy the settings- for current_setting in current_set.setting_set.all():- setting_copy = copy.copy(current_setting)- setting_copy.pk = None- setting_copy.set = current_set_copy- setting_copy.save()-- return HttpResponseRedirect(workout_copy.get_absolute_url())- else:- workout_form = WorkoutCopyForm({'name': workout.name, 'description': workout.description})- workout_form.helper = FormHelper()- workout_form.helper.form_id = slugify(request.path)- workout_form.helper.form_method = 'post'- workout_form.helper.form_action = request.path- workout_form.helper.add_input(- Submit('submit', _('Save'), css_class='btn-success btn-block')- )- workout_form.helper.form_class = 'wger-form'-- template_data = {}- template_data.update(csrf(request))- template_data['title'] = _('Copy workout')- template_data['form'] = workout_form- template_data['form_fields'] = [workout_form['name']]- template_data['submit_text'] = _('Copy')-- return render(request, 'form.html', template_data)+ # 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):@@ -227,53 +170,12 @@ def make_workout(request, pk):return HttpResponseRedirect(workout.get_absolute_url())-@login_required-def add(request):- """- Add a new workout and redirect to its page- """- workout = Workout()- workout.user = request.user- workout.save()-- return HttpResponseRedirect(workout.get_absolute_url())---class WorkoutDeleteView(WgerDeleteMixin, LoginRequiredMixin, DeleteView):- """- Generic view to delete a workout routine- """-- model = Workout- success_url = reverse_lazy('manager:workout:overview')- messages = gettext_lazy('Successfully deleted')-- def get_context_data(self, **kwargs):- context = super(WorkoutDeleteView, self).get_context_data(**kwargs)- context['title'] = _('Delete {0}?').format(self.object)- return context---class WorkoutEditView(WgerFormMixin, LoginRequiredMixin, UpdateView):- """- Generic view to update an existing workout routine- """-- model = Workout- form_class = WorkoutForm-- def get_context_data(self, **kwargs):- context = super(WorkoutEditView, self).get_context_data(**kwargs)- context['title'] = _('Edit {0}').format(self.object)- return context--class WorkoutMarkAsTemplateView(WgerFormMixin, LoginRequiredMixin, UpdateView):"""Generic view to update an existing workout routine"""- model = Workout+ model = Routineform_class = WorkoutMakeTemplateFormdef get_context_data(self, **kwargs):