diff --git a/src/apps/api/views/competitions.py b/src/apps/api/views/competitions.py index 342e237ce..ef90394a4 100644 --- a/src/apps/api/views/competitions.py +++ b/src/apps/api/views/competitions.py @@ -69,6 +69,23 @@ def get_queryset(self): ).values_list('status')[:1] qs = qs.annotate(participant_status=Subquery(participant_status_query)) + # new condition for search bar + # `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 from Search bar + if (not mine) and (not participating_in): + # User is logged in + # filter his own competitions + # or + # filter published competitions by other users + qs = qs.filter( + (Q(created_by=self.request.user)) | + (Q(published=True) & ~Q(created_by=self.request.user)) + ) + else: + # 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': qs = qs.select_related('created_by') diff --git a/src/factories.py b/src/factories.py index a4e23ea3e..d0c47716c 100644 --- a/src/factories.py +++ b/src/factories.py @@ -58,7 +58,7 @@ class Meta: title = factory.Sequence(lambda n: f'Competition {n}') created_by = factory.SubFactory(UserFactory) logo = factory.django.ImageField() - published = factory.LazyAttribute(lambda n: random.choice([True, False])) + published = factory.LazyAttribute(lambda n: random.choice([True, True])) description = factory.Faker('paragraph') created_when = factory.Faker('date_time_between', start_date='-5y', end_date='now', tzinfo=UTC)