def copy_workout(request, pk): ''' Makes a copy of a workout ''' workout = get_object_or_404(Workout, pk=pk, user=request.user) # Process request if request.method == 'POST': workout_form = WorkoutCopyForm(request.POST) if workout_form.is_valid(): # Copy workout days = workout.day_set.all() workout_copy = workout workout_copy.pk = None workout_copy.comment = workout_form.cleaned_data['comment'] workout_copy.save() # Copy the days for day in days: sets = day.set_set.all() day_copy = day days_of_week = [i for i in day.day.all()] day_copy.pk = None day_copy.training = workout_copy day_copy.save() for i in days_of_week: day_copy.day.add(i) 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('workout-view', 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']] template_data['submit_text'] = _('Copy') return render(request, 'form.html', template_data)
def copy_workout(request, pk): ''' Makes a copy of a workout ''' 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() # Process request if request.method == 'POST': workout_form = WorkoutCopyForm(request.POST) if workout_form.is_valid(): # Copy workout days = workout.day_set.all() workout_copy = workout workout_copy.pk = None workout_copy.comment = workout_form.cleaned_data['comment'] workout_copy.user = request.user workout_copy.save() # Copy the days for day in days: sets = day.set_set.all() day_copy = day days_of_week = [i for i in day.day.all()] day_copy.pk = None day_copy.training = workout_copy day_copy.save() for i in days_of_week: day_copy.day.add(i) 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:workout:view', kwargs={'pk': 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('manager:workout:copy', kwargs={'pk': workout.id}) template_data['form_fields'] = [workout_form['comment']] template_data['submit_text'] = _('Copy') template_data[ 'extend_template'] = 'base_empty.html' if request.is_ajax( ) else 'base.html' return render(request, 'form.html', template_data)
def copy_workout(request, pk): """ Makes a copy of a workout """ 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() # Process request if request.method == 'POST': workout_form = WorkoutCopyForm(request.POST) if workout_form.is_valid(): # Copy workout days_original = workout.day_set.all() workout_copy = copy.copy(workout) workout_copy.pk = None workout_copy.name = workout_form.cleaned_data['name'] workout_copy.user = request.user workout_copy.save() # Copy the days for day in days_original: sets = day.set_set.all() days_of_week = [i for i in day.day.all()] day_copy = copy.copy(day) day_copy.pk = None day_copy.training = workout_copy day_copy.save() for i in days_of_week: day_copy.day.add(i) day_copy.save() # Copy the sets for current_set in sets: 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(reverse('manager:workout:view', kwargs={'pk': workout_copy.id})) 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)
def copy_workout(request, pk): """ Makes a copy of a workout """ 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() # Process request if request.method == "POST": workout_form = WorkoutCopyForm(request.POST) if workout_form.is_valid(): # Copy workout days = workout.day_set.all() workout_copy = workout workout_copy.pk = None workout_copy.comment = workout_form.cleaned_data["comment"] workout_copy.user = request.user workout_copy.save() # Copy the days for day in days: sets = day.set_set.all() day_copy = day days_of_week = [i for i in day.day.all()] day_copy.pk = None day_copy.training = workout_copy day_copy.save() for i in days_of_week: day_copy.day.add(i) 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:workout:view", kwargs={"pk": 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("manager:workout:copy", kwargs={"pk": workout.id}) template_data["form_fields"] = [workout_form["comment"]] template_data["submit_text"] = _("Copy") template_data["extend_template"] = "base_empty.html" if request.is_ajax() else "base.html" return render(request, "form.html", template_data)
def copy_workout(request, pk): ''' Makes a copy of a workout ''' workout = get_object_or_404(Workout, pk=pk, user=request.user) # Process request if request.method == 'POST': workout_form = WorkoutCopyForm(request.POST) if workout_form.is_valid(): # Copy workout days = workout.day_set.all() workout_copy = workout workout_copy.pk = None workout_copy.comment = workout_form.cleaned_data['comment'] workout_copy.save() # Copy the days for day in days: sets = day.set_set.all() day_copy = day days_of_week = [i for i in day.day.all()] day_copy.pk = None day_copy.training = workout_copy day_copy.save() for i in days_of_week: day_copy.day.add(i) 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('wger.manager.views.workout.view', 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))
def copy_workout(request, pk): """ Makes a copy of a workout """ workout = get_object_or_404(Workout.both, pk=pk) if not workout.is_public and request.user != workout.user: return HttpResponseForbidden() # 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)