Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/apps/api/views/competitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down