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( ...@@ -98,28 +98,38 @@ class OfferList(
result = Offer.objects.none() result = Offer.objects.none()
if self.request.user: if self.request.user:
qs = Offer.objects.filter( query_set = Offer.objects.filter(
Q(owner=self.request.user) | Q(recipient=self.request.user) Q(owner=self.request.user) | Q(recipient=self.request.user)
).distinct() ).distinct()
qp = self.request.query_params query_parameter = self.request.query_params
u = self.request.user user = self.request.user
s = qp.get("status", None) query_set = self.check_status(query_parameter, query_set, user)
if s is not None and self.request is not None: query_set = self.check_category(query_parameter, query_set, user)
qs = qs.filter(status=s)
if qp.get("status", None) is None: return query_set
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
else: else:
return result 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( class OfferDetail(
mixins.RetrieveModelMixin, mixins.RetrieveModelMixin,
......
...@@ -140,6 +140,15 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer): ...@@ -140,6 +140,15 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
instance.date = validated_data.get("date", instance.date) instance.date = validated_data.get("date", instance.date)
instance.save() 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( for exercise_instance, exercise_instance_data in zip(
exercise_instances.all(), exercise_instances_data exercise_instances.all(), exercise_instances_data
): ):
...@@ -154,6 +163,9 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer): ...@@ -154,6 +163,9 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
) )
exercise_instance.save() exercise_instance.save()
def update_or_delete_exercise(
self, exercise_instances_data, exercise_instances, instance
):
if len(exercise_instances_data) > len(exercise_instances.all()): if len(exercise_instances_data) > len(exercise_instances.all()):
for i in range(len(exercise_instances.all()), len(exercise_instances_data)): for i in range(len(exercise_instances.all()), len(exercise_instances_data)):
exercise_instance_data = exercise_instances_data[i] exercise_instance_data = exercise_instances_data[i]
...@@ -165,6 +177,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer): ...@@ -165,6 +177,7 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
for i in range(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_instances.all()[i].delete()
def iterate_workout_files(self, instance, validated_data):
if "files" in validated_data: if "files" in validated_data:
files_data = validated_data.pop("files") files_data = validated_data.pop("files")
files = instance.files files = instance.files
...@@ -184,8 +197,6 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer): ...@@ -184,8 +197,6 @@ class WorkoutSerializer(serializers.HyperlinkedModelSerializer):
for i in range(len(files_data), len(files.all())): for i in range(len(files_data), len(files.all())):
files.all()[i].delete() files.all()[i].delete()
return instance
def get_owner_username(self, obj): def get_owner_username(self, obj):
"""Returns the owning user's username. """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