Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

When doing the AJAX update with more than one exercise per set, manually set the exercise data, otherwise this doesn't work as expected

Roland Geider0d6b1a770045
diff --git a/manager/templates/setting/ajax_edit.html b/manager/templates/setting/ajax_edit.html
index d940b42fe..6b1d3d1e1 100644
--- a/manager/templates/setting/ajax_edit.html
+++ b/manager/templates/setting/ajax_edit.html
@@ -18,10 +18,20 @@ $(document).ready(function() {
{% csrf_token %}
<span class="ui-widget">
- <input type="hidden"
- name="exercises"
- value="{{exercise.id}}"
+ {% for current_exercise in set.exercises.all %}
+ {% if current_exercise.id == exercise.id %}
+ <input type="hidden"
+ name="current_exercise"
+ value="{{current_exercise.id}}"
id="set-{{set.id}}-exercercise-id-hidden">
+ {% endif %}
+
+ <input type="hidden"
+ name="exercises"
+ value="{{current_exercise.id}}">
+ {% endfor%}
+
+
<input type="text"
name="exercise_name"
value="{{exercise.name}}"
diff --git a/manager/views.py b/manager/views.py
index 8ede06487..36d2368b7 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -418,6 +418,7 @@ def edit_set(request, id, day_id, set_id=None):
# Process request
if request.method == 'POST':
+
set_form = SetForm(request.POST, instance=workout_set)
# If the data is valid, save and redirect
@@ -515,15 +516,22 @@ def api_edit_set(request):
# Process request
if request.method == 'POST':
- new_exercise_id = request.POST.get('exercises')
+ new_exercise_id = request.POST.get('current_exercise')
new_exercise = get_object_or_404(Exercise, pk=new_exercise_id)
+ # When there is more than one exercise per set, we need to manually set and replace
+ # the IDs here, otherwise they get lost
+ request_copy = request.POST
+ request_copy = request_copy.copy()
+
+ exercise_list = [ i for i in request_copy.getlist('exercises') if i != exercise_id]
+ request_copy.setlist('exercises', exercise_list)
+ request_copy.update({'exercises': new_exercise_id})
+
+
+ set_form = SetForm(request_copy, instance=workout_set)
- set_form = SetForm(request.POST, instance=workout_set)
if set_form.is_valid():
-
- # TODO: when there is more than one exercise per set, we need to
- # manually set them here, otherwise they get lost!
set_form.save()
else: