From bf95b6dc70a1856d93af5760ad02840bd225f233 Mon Sep 17 00:00:00 2001 From: Ihsan Ullah Date: Thu, 11 May 2023 22:56:01 +0500 Subject: [PATCH 1/3] search condition added --- src/apps/api/views/competitions.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/apps/api/views/competitions.py b/src/apps/api/views/competitions.py index eb74f1002..26cdd9406 100644 --- a/src/apps/api/views/competitions.py +++ b/src/apps/api/views/competitions.py @@ -46,6 +46,9 @@ class CompetitionViewSet(ModelViewSet): def get_queryset(self): qs = super().get_queryset() + # Filter for search bar + search_query = self.request.query_params.get('search') + if self.request.user.is_authenticated: # filter by competition_type first, 'competition' by default @@ -82,7 +85,8 @@ def get_queryset(self): # `participating_in` is true when this is called from "Benchmarks I'm in" # `mine` and `participating_in` are none when this is called either from Search bar # or from competition detail page - if (not mine) and (not participating_in): + # if (not mine) and (not participating_in): + if (search_query): # User is logged in then filter # competitions which this user owns # or @@ -97,12 +101,16 @@ def get_queryset(self): (Q(published=True) & ~Q(created_by=self.request.user)) | (Q(participants__user=self.request.user) & Q(participants__status="approved")) ).distinct() + else: - # if user is not authenticated only filter published/public competitions - qs = qs.filter(Q(published=True)) + if (search_query): + # if user is not authenticated only filter published/public competitions + qs = qs.filter(Q(published=True)) # On GETs lets optimize the query to reduce DB calls if self.request.method == 'GET': + + logger.warning("\n\nGET\n\n") qs = qs.select_related('created_by') if self.action != 'list': qs = qs.select_related('created_by') @@ -125,7 +133,6 @@ def get_queryset(self): ), distinct=True) ) - search_query = self.request.query_params.get('search') # search_query is true when called from searchbar if search_query: qs = qs.filter(Q(title__icontains=search_query) | Q(description__icontains=search_query)) From 7bc2e69e45e8d933747dbc31fb0866a61730d628 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Pav=C3=A3o?= Date: Thu, 11 May 2023 20:00:07 +0200 Subject: [PATCH 2/3] Update competitions.py --- src/apps/api/views/competitions.py | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/src/apps/api/views/competitions.py b/src/apps/api/views/competitions.py index 26cdd9406..22793169a 100644 --- a/src/apps/api/views/competitions.py +++ b/src/apps/api/views/competitions.py @@ -45,21 +45,15 @@ class CompetitionViewSet(ModelViewSet): def get_queryset(self): qs = super().get_queryset() - # Filter for search bar search_query = self.request.query_params.get('search') - - if self.request.user.is_authenticated: - + if self.request.user.is_authenticated: # user is logged in # filter by competition_type first, 'competition' by default competition_type = self.request.query_params.get('type', Competition.COMPETITION) - if competition_type != 'any' and self.detail is False: qs = qs.filter(competition_type=competition_type) - # Filter to only see competitions you own mine = self.request.query_params.get('mine', None) - if mine: # either competition is mine # or @@ -67,26 +61,19 @@ def get_queryset(self): qs = Competition.objects.filter( (Q(created_by=self.request.user)) | (Q(collaborators__in=[self.request.user])) - ) - participating_in = self.request.query_params.get('participating_in', None) - if participating_in: qs = qs.filter(participants__user=self.request.user, participants__status="approved") - participant_status_query = CompetitionParticipant.objects.filter( competition=OuterRef('pk'), user=self.request.user ).values_list('status')[:1] qs = qs.annotate(participant_status=Subquery(participant_status_query)) - # `mine` is true when this is called from "Benchmarks I'm Running" # `participating_in` is true when this is called from "Benchmarks I'm in" - # `mine` and `participating_in` are none when this is called either from Search bar - # or from competition detail page - # if (not mine) and (not participating_in): - if (search_query): + # `search_query` is true when this is called from the search bar + if search_query: # User is logged in then filter # competitions which this user owns # or @@ -101,10 +88,9 @@ def get_queryset(self): (Q(published=True) & ~Q(created_by=self.request.user)) | (Q(participants__user=self.request.user) & Q(participants__status="approved")) ).distinct() - else: + # if user is not authenticated only show public competitions in the search if (search_query): - # if user is not authenticated only filter published/public competitions qs = qs.filter(Q(published=True)) # On GETs lets optimize the query to reduce DB calls From c8c30184cc138d4a5a3139fd9ac8318268e2d1c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adrien=20Pav=C3=A3o?= Date: Thu, 11 May 2023 20:05:41 +0200 Subject: [PATCH 3/3] Update competitions.py --- src/apps/api/views/competitions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/apps/api/views/competitions.py b/src/apps/api/views/competitions.py index 22793169a..4f6288da9 100644 --- a/src/apps/api/views/competitions.py +++ b/src/apps/api/views/competitions.py @@ -47,7 +47,8 @@ def get_queryset(self): qs = super().get_queryset() # Filter for search bar search_query = self.request.query_params.get('search') - if self.request.user.is_authenticated: # user is logged in + # If user is logged in + if self.request.user.is_authenticated: # filter by competition_type first, 'competition' by default competition_type = self.request.query_params.get('type', Competition.COMPETITION) if competition_type != 'any' and self.detail is False: