|
|
|
|
@ -1715,37 +1715,54 @@ def private_tournaments(request): |
|
|
|
|
thirty_days_ago = now - timedelta(days=30) |
|
|
|
|
thirty_days_future = now + timedelta(days=30) |
|
|
|
|
|
|
|
|
|
# Custom query for private tournaments |
|
|
|
|
queries = [ |
|
|
|
|
Q(is_private=True, is_deleted=False, event__club__isnull=False) |
|
|
|
|
] |
|
|
|
|
|
|
|
|
|
# Filter by date range for future tournaments |
|
|
|
|
future_query = queries.copy() |
|
|
|
|
future_query.append(Q(start_date__gt=now, start_date__lte=thirty_days_future)) |
|
|
|
|
|
|
|
|
|
# Filter for live tournaments |
|
|
|
|
live_query = queries.copy() |
|
|
|
|
live_query.append(Q(start_date__lte=now, end_date__isnull=True) | Q(start_date__lte=now, end_date__gt=now)) |
|
|
|
|
# Define a custom private tournaments query function |
|
|
|
|
def private_tournaments_query(query, ascending, limit=None): |
|
|
|
|
queries = [query, Q(is_private=True, is_deleted=False, event__club__isnull=False)] |
|
|
|
|
|
|
|
|
|
sortkey = 'start_date' |
|
|
|
|
if not ascending: |
|
|
|
|
sortkey = '-start_date' |
|
|
|
|
|
|
|
|
|
queryset = Tournament.objects.filter(*queries).prefetch_related( |
|
|
|
|
'group_stages', |
|
|
|
|
'rounds', |
|
|
|
|
'team_registrations', |
|
|
|
|
).order_by(sortkey) |
|
|
|
|
|
|
|
|
|
# Apply limit directly in the database query |
|
|
|
|
if limit is not None and isinstance(limit, int) and limit > 0: |
|
|
|
|
queryset = queryset[:limit] |
|
|
|
|
|
|
|
|
|
return queryset |
|
|
|
|
|
|
|
|
|
# Get all tournaments matching our criteria (similar to index but for private tournaments) |
|
|
|
|
tournaments = private_tournaments_query( |
|
|
|
|
Q(end_date__isnull=True, start_date__gte=thirty_days_ago, start_date__lte=thirty_days_future), |
|
|
|
|
True, 50 |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
# Filter for ended tournaments |
|
|
|
|
ended_query = queries.copy() |
|
|
|
|
ended_query.append(Q(end_date__lte=now, end_date__gte=thirty_days_ago)) |
|
|
|
|
# Filter tournaments that should be displayed |
|
|
|
|
display_tournament = [t for t in tournaments if t.display_tournament()] |
|
|
|
|
|
|
|
|
|
# Get the tournaments from the database |
|
|
|
|
future_tournaments = Tournament.objects.filter(*future_query).prefetch_related( |
|
|
|
|
'group_stages', 'rounds', 'team_registrations').order_by('start_date') |
|
|
|
|
# Categorize tournaments by status |
|
|
|
|
live = [] |
|
|
|
|
future = [] |
|
|
|
|
for t in display_tournament: |
|
|
|
|
if t.supposedly_in_progress(): |
|
|
|
|
live.append(t) |
|
|
|
|
elif t.starts_in_the_future(): |
|
|
|
|
future.append(t) |
|
|
|
|
|
|
|
|
|
live_tournaments = Tournament.objects.filter(*live_query).prefetch_related( |
|
|
|
|
'group_stages', 'rounds', 'team_registrations').order_by('start_date') |
|
|
|
|
# Get ended tournaments |
|
|
|
|
clean_ended_tournaments = private_tournaments_query(Q(end_date__isnull=False), False, 50) |
|
|
|
|
clean_ended_tournaments = [t for t in clean_ended_tournaments if t.display_tournament()] |
|
|
|
|
ended_tournaments = [t for t in display_tournament if t.should_be_over()] |
|
|
|
|
|
|
|
|
|
ended_tournaments = Tournament.objects.filter(*ended_query).prefetch_related( |
|
|
|
|
'group_stages', 'rounds', 'team_registrations').order_by('-end_date') |
|
|
|
|
# Combine both lists |
|
|
|
|
finished = clean_ended_tournaments + ended_tournaments |
|
|
|
|
|
|
|
|
|
# Filter tournaments that should be displayed |
|
|
|
|
future = [t for t in future_tournaments if t.display_tournament()] |
|
|
|
|
live = [t for t in live_tournaments if t.display_tournament()] |
|
|
|
|
ended = [t for t in ended_tournaments if t.display_tournament()] |
|
|
|
|
# Sort the combined list by start_date in descending order |
|
|
|
|
finished.sort(key=lambda t: t.sorting_finished_date(), reverse=True) |
|
|
|
|
|
|
|
|
|
return render( |
|
|
|
|
request, |
|
|
|
|
@ -1753,7 +1770,7 @@ def private_tournaments(request): |
|
|
|
|
{ |
|
|
|
|
'future': future[:10], |
|
|
|
|
'live': live[:10], |
|
|
|
|
'ended': ended[:10], |
|
|
|
|
'ended': finished[:10], |
|
|
|
|
'is_private_section': True, # Flag to indicate we're in the private tournaments section |
|
|
|
|
'section_title': 'Tournois privés', # Title for the private tournaments page |
|
|
|
|
} |
|
|
|
|
|