Commit 73d2454e authored by Håkon Finstad's avatar Håkon Finstad
Browse files

added workout difficulty

parent 069238a4
Pipeline #159182 failed with stages
in 2 minutes and 11 seconds
......@@ -100,15 +100,25 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
files_data = validated_data.pop("files")
workout = Workout.objects.create(**validated_data)
for exercise_instance_data in exercise_instances_data:
difficulty = 0
for exercise_instance_data in exercise_instances_data:
exercise = ExerciseInstance.objects.create(workout=workout, **exercise_instance_data)
difficulty += exercise.exercise.difficulty;
for file_data in files_data:
WorkoutFile.objects.create(
workout=workout, owner=workout.owner, file=file_data.get("file")
)
workout.difficulty = difficulty / len(exercise_instances_data)
workout.save()
return workout
def update(self, instance, validated_data):
"""Custom logic for updating a Workout with its ExerciseInstances and Workouts.
......@@ -129,7 +139,11 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
instance.notes = validated_data.get("notes", instance.notes)
instance.visibility = validated_data.get("visibility", instance.visibility)
instance.date = validated_data.get("date", instance.date)
instance.save()
difficulty = 0
# Handle ExerciseInstances
......@@ -148,21 +162,30 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
exercise_instance.sets = exercise_instance_data.get(
"sets", exercise_instance.sets
)
difficulty += exercise_instance.exercise.difficulty;
exercise_instance.save()
# If new exercise instances have been added to the workout, then create them
if len(exercise_instances_data) > len(exercise_instances.all()):
for i in range(len(exercise_instances.all()), len(exercise_instances_data)):
exercise_instance_data = exercise_instances_data[i]
ExerciseInstance.objects.create(
exercise = ExerciseInstance.objects.create(
workout=instance, **exercise_instance_data
)
difficulty += exercise.exercise.difficulty;
# Else if exercise instances have been removed from the workout, then delete them
elif len(exercise_instances_data) < len(exercise_instances.all()):
for i in range(len(exercise_instances_data), len(exercise_instances.all())):
exercise_instances.all()[i].delete()
exercise = exercise_instances.all()[i]
exercise.delete()
difficulty -= exercise.exercise.difficulty
instance.difficulty = difficulty / len(exercise_instances_data)
instance.save()
# Handle WorkoutFiles
if "files" in validated_data:
files_data = validated_data.pop("files")
......
......@@ -26,7 +26,7 @@ async function fetchWorkouts(ordering) {
rows[1].querySelectorAll("td")[1].textContent = localDate.toLocaleTimeString(); // Time
rows[2].querySelectorAll("td")[1].textContent = workout.owner_username; //Owner
rows[3].querySelectorAll("td")[1].textContent = workout.exercise_instances.length; // Exercises
rows[4].querySelectorAll("td")[1].textContent = workout.; // Difficulty
rows[4].querySelectorAll("td")[1].textContent = workout.difficulty; // Difficulty
container.appendChild(aWorkout);
});
......
......@@ -31,7 +31,7 @@
<a class="list-group-item list-group-item-action" id="list-public-workouts-list" data-bs-toggle="list" href="#list-public-workouts" role="tab" aria-controls="public">Public Workouts</a>
</div>
<div class="mt-1">Sort by: <a href="?ordering=date">Date</a> <a href="?ordering=owner">Owner</a> <a href="?ordering=name">Name</a> <a href="?ordering=difficulty">Difficulty</a>
<br>Currently sorting by: <span id="current-sort"></span>
<br>Currently sorting by: <span id="current-sort"></span> asd
</div>
<div class="list-group mt-1" id="div-content"></div>
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment