Commit 1538a5ab authored by olavhdi's avatar olavhdi
Browse files

#33, also mccabe of 12 to 7

parent 10d45d73
......@@ -102,38 +102,72 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
for exercise_instance_data in exercise_instances_data:
ExerciseInstance.objects.create(workout=workout, **exercise_instance_data)
for file_data in validated_data:
for file_data in validated_files:
WorkoutFile.objects.create(
workout=workout, owner=workout.owner, file=file_data.get("file")
)
return workout
def update(self, instance, validated_data):
"""Custom logic for updating a Workout with its ExerciseInstances and Workouts.
@staticmethod
def delete_files(instance, data):
""""Removes the selected data from the instanced object
This is needed because each object in both exercise_instances and files must be iterated
over and handled individually.
Args:
instance: Data object
data: Data to remove
"""
for i in range(len(data), len(instance.all())):
instance.all()[i].delete()
@staticmethod
def create_workout_files(instance, files, validated_files):
"""Creates the workout files
Args:
instance (Workout): Current Workout object
validated_data: Contains data for validated fields
instance: Workout instance
files: Collection of files
validated_files: Data for validated fields
Returns:
Workout: Updated Workout instance
instance: Workout instance
"""
exercise_instances_data = validated_data.pop("exercise_instances")
exercise_instances = instance.exercise_instances
for i in range(len(files.all()), len(validated_files)):
WorkoutFile.objects.create(
workout=instance,
owner=instance.owner,
file=validated_files[i].get("file"),
)
return instance
instance.name = validated_data.get("name", instance.name)
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()
@staticmethod
def create_exercise_instance(instance, exercise_instances, exercise_instances_data):
"""Creates the workout files
# Handle ExerciseInstances
Args:
instance: Workout instance
exercise_instances: Collection of workouts
exercise_instances_data: Data for workout fields
Returns:
instance: Workout instance
"""
for i in range(len(exercise_instances.all()), len(exercise_instances_data)):
exercise_instance_data = exercise_instances_data[i]
ExerciseInstance.objects.create(
workout=instance, **exercise_instance_data
)
return instance
@staticmethod
def update_exercise_instances(exercise_instances, exercise_instances_data):
"""Updates existing exercise instances without adding or deleting object
Args:
exercise_instances: Collection of workouts
exercise_instances_data: Data for workout fields
"""
# 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))
for exercise_instance, exercise_instance_data in zip(
......@@ -150,17 +184,37 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
)
exercise_instance.save()
def update(self, instance, validated_data):
"""Custom logic for updating a Workout with its ExerciseInstances and Workouts.
This is needed because each object in both exercise_instances and files must be iterated
over and handled individually.
Args:
instance (Workout): Current Workout object
validated_data: Contains data for validated fields
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)
instance.visibility = validated_data.get("visibility", instance.visibility)
instance.date = validated_data.get("date", instance.date)
instance.save()
# Handle ExerciseInstances
self.update_exercise_instances(exercise_instances, exercise_instances_data)
# 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(
workout=instance, **exercise_instance_data
)
instance = self.create_exercise_instance(instance, exercise_instances, exercise_instances_data)
# 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()
self.delete_files(exercise_instances_data, exercise_instances)
# Handle WorkoutFiles
......@@ -172,17 +226,12 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
file.file = file_data.get("file", file.file)
# If new files have been added, creating new WorkoutFiles
if len(files_data) > len(files.all()):
for i in range(len(files.all()), len(validated_files)):
WorkoutFile.objects.create(
workout=instance,
owner=instance.owner,
file=validated_files[i].get("file"),
)
if len(validated_files) > len(files.all()):
instance = self.create_workout_files(instance, files, validated_files)
# Else if files have been removed, delete WorkoutFiles
elif len(validated_files) < len(files.all()):
for i in range(len(validated_files), len(files.all())):
files.all()[i].delete()
self.delete_files(files, validated_files)
return instance
......
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