Commit 3eb7442d authored by Christopher Løkken's avatar Christopher Løkken
Browse files

backend

parent b06451b6
Pipeline #172960 failed with stages
in 35 seconds
"""Serializers for the workouts application
"""
from numpy import add
from rest_framework import serializers
from rest_framework.serializers import HyperlinkedRelatedField
from workouts.models.workouts import Workout
......@@ -126,8 +127,6 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
Returns:
Workout: Updated Workout instance
"""
exercise_instances_data = validated_data.pop("exercise_instances")
exercise_instances = instance.exercise_instances
instance.name = validated_data.get("name", instance.name)
instance.notes = validated_data.get("notes", instance.notes)
......@@ -137,7 +136,16 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
instance.save()
# Handle ExerciseInstances
self.handle_exercise_instance(validated_data, instance)
# Handle WorkoutFiles
self.handle_workout_files(validated_data, instance)
return instance
def handle_exercise_instance(self, validated_data, instance):
exercise_instances_data = validated_data.pop("exercise_instances")
exercise_instances = instance.exercise_instances
# This updates existing exercise instances without adding or deleting object.
# zip() will yield n 2-tuples, where n is
# min(len(exercise_instance), len(exercise_instance_data))
......@@ -171,8 +179,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
len(exercise_instances.all())):
exercise_instances.all()[i].delete()
# Handle WorkoutFiles
def handle_workout_files(self, validated_data, instance):
if "files" in validated_data:
files_data = validated_data.pop("files")
files = instance.files
......@@ -193,8 +200,6 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
for i in range(len(files_data), len(files.all())):
files.all()[i].delete()
return instance
def get_owner_username(self, obj):
"""Returns the owning user's username
......
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