Commit 11f308b1 authored by and's avatar and
Browse files

Code Smell Long Methods Backend

Refactored long methods in file views.py in users folder and serializers.py in workouts folder.
Parts of the code in the methods are extracted to new methods.
parent 9c3cde63
Pipeline #128118 passed with stage
in 54 seconds
......@@ -98,28 +98,38 @@ class OfferList(
result = Offer.objects.none()
if self.request.user:
qs = Offer.objects.filter(
query_set = Offer.objects.filter(
Q(owner=self.request.user) | Q(recipient=self.request.user)
).distinct()
qp = self.request.query_params
u = self.request.user
s = qp.get("status", None)
if s is not None and self.request is not None:
qs = qs.filter(status=s)
if qp.get("status", None) is None:
qs = Offer.objects.filter(Q(owner=u)).distinct()
c = qp.get("category", None)
if c is not None and qp is not None:
if c == "sent":
qs = qs.filter(owner=u)
elif c == "received":
qs = qs.filter(recipient=u)
return qs
query_parameter = self.request.query_params
user = self.request.user
query_set = self.check_status(query_parameter, query_set, user)
query_set = self.check_category(query_parameter, query_set, user)
return query_set
else:
return result
def check_status(self, query_parameter, query_set, user):
status = query_parameter.get("status", None)
if status is not None and self.request is not None:
query_set = query_set.filter(status=s)
if status is None:
query_set = Offer.objects.filter(Q(owner=u)).distinct()
return query_set
def check_category(self, query_parameter, query_set, user):
category = query_parameter.get("category", None)
if category is not None and query_parameter is not None:
if category == "sent":
query_set = query_set.filter(owner=u)
elif category == "received":
query_set = query_set.filter(recipient=u)
return query_set
class OfferDetail(
mixins.RetrieveModelMixin,
......
......@@ -140,6 +140,15 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
instance.date = validated_data.get("date", instance.date)
instance.save()
self.update_exercise_instance(exercise_instances_data, exercise_instances)
self.update_or_delete_exercise(
exercise_instances_data, exercise_instances, instance
)
self.iterate_workout_files(instance, validated_data)
return instance
def update_exercise_instance(self, exercise_instances_data, exercise_instances):
for exercise_instance, exercise_instance_data in zip(
exercise_instances.all(), exercise_instances_data
):
......@@ -154,6 +163,9 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
)
exercise_instance.save()
def update_or_delete_exercise(
self, exercise_instances_data, exercise_instances, instance
):
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]
......@@ -165,6 +177,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
for i in range(len(exercise_instances_data), len(exercise_instances.all())):
exercise_instances.all()[i].delete()
def iterate_workout_files(self, instance, validated_data):
if "files" in validated_data:
files_data = validated_data.pop("files")
files = instance.files
......@@ -184,8 +197,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