Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add first version of workout copy function

Roland Geider178f118ef32c
diff --git a/manager/templates/workout/view.html b/manager/templates/workout/view.html
index e555d16f6..7364644bf 100644
--- a/manager/templates/workout/view.html
+++ b/manager/templates/workout/view.html
@@ -139,6 +139,18 @@ $(document).ready(function() {
</a>
</p>
+<p>
+<a href="{% url workout-copy workout.id %}">
+ <img src="{% static 'images/icons/edit.svg' %}"
+ width="32"
+ height="32"
+ alt="{% trans 'Copy workout' %}"
+ title="{% trans 'Copy workout' %}">
+ {% trans "Copy workout" %}
+</a>
+</p>
+
+
<p>
<a href="{% url workout-delete workout.id %}"
class="modal-dialog">
diff --git a/manager/urls.py b/manager/urls.py
index 82ea07e6d..3953de18c 100644
--- a/manager/urls.py
+++ b/manager/urls.py
@@ -25,6 +25,10 @@ urlpatterns = patterns('manager.views',
# Workout
url(r'^workout/overview$', 'overview'),
url(r'^workout/add$', 'add'),
+ url(r'^workout/(?P<pk>\d+)/copy/$',
+ 'copy_workout',
+ name = 'workout-copy'),
+
url(r'^workout/(?P<pk>\d+)/edit/$',
login_required(WorkoutEditView.as_view()),
name = 'workout-edit'),
diff --git a/manager/views.py b/manager/views.py
index a2c3cf414..5d857b8ee 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -357,6 +357,59 @@ def view_workout(request, id):
template_data,
context_instance=RequestContext(request))
+@login_required
+def copy_workout(request, pk):
+ """
+ Makes a copy of a workout
+ """
+ template_data = {}
+ template_data['active_tab'] = WORKOUT_TAB
+
+ workout = get_object_or_404(TrainingSchedule, pk=pk, user=request.user)
+ template_data['workout'] = workout
+
+ # 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()
+
+ 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}))
+
@login_required
def add(request):