Commit 0975dac4 authored by Håvard Farestveit's avatar Håvard Farestveit
Browse files

#33 moved code into separate functions


. 
parent b34acbf0
Pipeline #125618 passed with stage
in 27 seconds
......@@ -95,8 +95,24 @@ class OfferList(
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
def filter_on_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()
return query_set
def filter_on_category(self, query_set, query_params, 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)
return query_set
def get_queryset(self):
query_set = Offer.objects.none()
result = Offer.objects.none()
if self.request.user:
......@@ -106,20 +122,8 @@ class OfferList(
query_params = self.request.query_params
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()
# 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)
query_set = self.filter_on_status(query_set, query_params, user)
query_set = self.filter_on_category(query_set, query_params, user)
return query_set
return result
......
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