Self hosted FLOSS fitness/workout, nutrition and weight tracker
Show a dialog to set the goal/description when copying a workout
Roland Geider9240e52ca7d0
diff --git a/manager/views.py b/manager/views.pyindex 850d52de6..4690afeae 100644--- a/manager/views.py+++ b/manager/views.py@@ -25,11 +25,6 @@ from django.shortcuts import get_object_or_404from django.http import HttpResponsefrom django.http import HttpResponseRedirectfrom django.http import HttpResponseForbidden-from django.forms import ModelForm-from django.forms import EmailField-from django.forms.models import modelformset_factory-from django.forms import SelectMultiple-from django.forms import ValidationErrorfrom django.core.context_processors import csrffrom django.core.exceptions import ObjectDoesNotExistfrom django.core.urlresolvers import reverse@@ -37,6 +32,15 @@ from django.core.urlresolvers import reverse_lazyfrom django.utils.translation import ugettext as _from django.utils.translation import ugettext_lazy+from django.forms import Form+from django.forms import ModelForm+from django.forms import EmailField+from django.forms import CharField+from django.forms import SelectMultiple+from django.forms import ValidationError+from django.forms.models import modelformset_factory++from django.contrib.auth import authenticatefrom django.contrib.auth import login as django_loginfrom django.contrib.auth import logout as django_logout@@ -45,11 +49,9 @@ from django.contrib.auth.models import User as Django_Userfrom django.contrib.auth.forms import AuthenticationFormfrom django.contrib.auth.forms import UserCreationFormfrom django.contrib.auth.forms import PasswordChangeForm-from django.contrib.auth.views import login as django_loginviewfrom django.contrib.auth.views import password_change as django_pwchange-from django.views.generic import DeleteViewfrom django.views.generic import CreateViewfrom django.views.generic import UpdateView@@ -360,6 +362,12 @@ def view_workout(request, id):template_data,context_instance=RequestContext(request))++class WorkoutCopyForm(Form):+ comment = CharField(max_length = 100,+ help_text = _('The goal or description of the new workout.'),+ required = False)+@login_requireddef copy_workout(request, pk):"""@@ -368,47 +376,69 @@ def copy_workout(request, pk):workout = get_object_or_404(TrainingSchedule, pk=pk, user=request.user)- # Copy workout- days = workout.day_set.all()-- workout_copy = workout- workout_copy.pk = None- workout_copy.save()-- # Copy the days- for day in days:- sets = day.set_set.all()+ # Process request+ if request.method == 'POST':+ workout_form = WorkoutCopyForm(request.POST)- day_copy = day- day_copy.pk = None- day_copy.training = workout_copy- day_copy.save()+ if workout_form.is_valid():- # Copy the sets- for current_set in sets:- current_set_id = current_set.id- exercises = current_set.exercises.all()-- current_set_copy = current_set- current_set_copy.pk = None- current_set_copy.exerciseday = day_copy- current_set_copy.save()+ # Copy workout+ days = workout.day_set.all()- # Exercises has Many2Many relationship- current_set_copy.exercises = exercises+ workout_copy = workout+ workout_copy.pk = None+ workout_copy.comment = workout_form.cleaned_data['comment']+ workout_copy.save()- # Go through the exercises- for exercise in exercises:- settings = exercise.setting_set.filter(set_id=current_set_id)-- # Copy the settings- for setting in settings:- setting_copy = setting- setting_copy.pk = None- setting_copy.set = current_set_copy- setting_copy.save()+ # Copy the days+ for day in days:+ sets = day.set_set.all()++ day_copy = day+ day_copy.pk = None+ day_copy.training = workout_copy+ day_copy.save()++ # Copy the sets+ for current_set in sets:+ current_set_id = current_set.id+ exercises = current_set.exercises.all()++ current_set_copy = current_set+ current_set_copy.pk = None+ current_set_copy.exerciseday = day_copy+ current_set_copy.save()++ # Exercises has Many2Many relationship+ current_set_copy.exercises = exercises++ # Go through the exercises+ for exercise in exercises:+ settings = exercise.setting_set.filter(set_id=current_set_id)++ # Copy the settings+ for setting in settings:+ setting_copy = setting+ setting_copy.pk = None+ setting_copy.set = current_set_copy+ setting_copy.save()++ return HttpResponseRedirect(reverse('manager.views.view_workout', kwargs= {'id': workout.id}))+ else:+ workout_form = WorkoutCopyForm({'comment' : workout.comment})++ template_data = {}+ template_data.update(csrf(request))+ template_data['title'] = _('Copy workout')+ template_data['form'] = workout_form+ template_data['form_action'] = reverse('workout-copy', kwargs= {'pk': workout.id})+ template_data['form_fields'] = [workout_form['comment']]++ return render_to_response('form.html',+ template_data,+ context_instance=RequestContext(request))+- return HttpResponseRedirect(reverse('manager.views.view_workout', kwargs= {'id': workout.id}))@login_required