From 335207048d660b7a2615b9a0be8def4ff31fc248 Mon Sep 17 00:00:00 2001 From: olechrib <olechrib@stud.ntnu.no> Date: Mon, 4 Apr 2022 19:04:30 +0200 Subject: [PATCH] long method in users/views refactored --- backend/secfit/users/views.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/backend/secfit/users/views.py b/backend/secfit/users/views.py index 84060f4..5949ca4 100644 --- a/backend/secfit/users/views.py +++ b/backend/secfit/users/views.py @@ -130,23 +130,28 @@ class OfferList( user = self.request.user # filtering by status (if provided) - status = query_params.get("status", None) - if status is not None and self.request is not None: - query_set = query_set.filter(status=status) - if query_params.get("status", None) is None: - query_set = Offer.objects.filter(Q(owner=user)).distinct() + filter_status(self, query_set, query_params, user) # filtering by category (sent or received) - category = query_params.get("category", None) - if category is not None and query_params is not None: - if category == "sent": - query_set = query_set.filter(owner=user) - elif category == "received": - query_set = query_set.filter(recipient=user) + filter_category(query_params, query_set, user) return query_set else: return result +def filter_status(self, query_set, query_params, user): + status = query_params.get("status", None) + if status is not None and self.request is not None: + query_set = query_set.filter(status=status) + if query_params.get("status", None) is None: + query_set = Offer.objects.filter(Q(owner=user)).distinct() + +def filter_category(query_params, query_set, user): + category = query_params.get("category", None) + if category is not None and query_params is not None: + if category == "sent": + query_set = query_set.filter(owner=user) + elif category == "received": + query_set = query_set.filter(recipient=user) class OfferDetail( mixins.RetrieveModelMixin, -- GitLab