Commit d230a3f8 authored by Einar Uvsløkk's avatar Einar Uvsløkk
Browse files

Reduce complexity in WorkoutSerializer.update

Both flake8 and sonarlint reported high cognitive complexity in the
WorkoutSerializer.update method. The complexity is now reduced by using
a substitute algorithm strategy. Some variable names are renamed to
improve readability.
parent 55e3924c
......@@ -129,60 +129,74 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
Returns:
Workout: Updated Workout instance
"""
exercise_instances_data = validated_data.pop("exercise_instances")
exercise_instances = instance.exercise_instances
if data := validated_data.pop("exercise_instances"):
self.update_workout_exercises(instance, data)
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()
for attr in ["name", "notes", "visibility", "date"]:
if attr in validated_data:
setattr(instance, attr, validated_data[attr])
for exercise_instance, exercise_instance_data in zip(
exercise_instances.all(), exercise_instances_data
):
exercise_instance.exercise = exercise_instance_data.get(
"exercise", exercise_instance.exercise
)
exercise_instance.number = exercise_instance_data.get(
"number", exercise_instance.number
)
exercise_instance.sets = exercise_instance_data.get(
"sets", exercise_instance.sets
)
exercise_instance.save()
if data := validated_data.pop("files"):
self.update_workout_files(instance, data)
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.save()
return instance
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()
def update_workout_exercises(self, workout, data):
"""Updates the exercise instances associated with a workout.
if "files" in validated_data:
files_data = validated_data.pop("files")
files = instance.files
Args:
workout (Workout): Current workout object.
data (list): A list of validated exercise instance data.
"""
exercises = workout.exercise_instances.all()
for file, file_data in zip(files.all(), files_data):
file.file = file_data.get("file", file.file)
for exercise, exercise_data in zip(exercises, data):
for attr in ["exercise", "number", "sets"]:
if attr in exercise_data:
setattr(exercise, attr, exercise_data[attr])
exercise.save()
if len(files_data) > len(files.all()):
for i in range(len(files.all()), len(files_data)):
WorkoutFile.objects.create(
workout=instance,
owner=instance.owner,
file=files_data[i].get("file"),
)
num_data = len(data)
num_exercises = len(exercises)
if (diff := num_data - num_exercises) > 0:
for i in range(num_exercises, num_data):
ExerciseInstance.objects.create(
workout=workout,
**data[i],
)
elif diff < 0:
for exercise in exercises[:-diff]:
exercise.delete()
elif len(files_data) < len(files.all()):
for i in range(len(files_data), len(files.all())):
files.all()[i].delete()
def update_workout_files(self, workout, data):
"""Updates the files associated with a workout.
return instance
Args:
workout (Workout): Current workout object.
data (list): A list of validated file data.
"""
files = workout.files.all()
for file_obj, file_data in zip(files, data):
if file := file_data.get("file"):
file_obj.file = file
file_obj.save()
num_data = len(data)
num_files = len(files)
if (diff := num_data - num_files) > 0:
for i in range(num_files, num_data):
WorkoutFile.objects.create(
workout=workout,
owner=workout.owner,
**data[i],
)
elif diff < 0:
for file_obj in files[:-diff]:
file_obj.delete()
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