|
|
|
|
@ -57,7 +57,6 @@ from django.contrib.auth.forms import ( |
|
|
|
|
from django.contrib.auth.views import PasswordResetConfirmView |
|
|
|
|
from django.contrib.auth import get_user_model |
|
|
|
|
from django.contrib.auth.tokens import default_token_generator |
|
|
|
|
from django.db.models import Q |
|
|
|
|
from django.views.decorators.csrf import csrf_exempt |
|
|
|
|
from django.core.files.storage import default_storage |
|
|
|
|
from django.core.files.base import ContentFile |
|
|
|
|
@ -108,9 +107,20 @@ from .forms import CustomPasswordChangeForm |
|
|
|
|
def index(request): |
|
|
|
|
|
|
|
|
|
club_id = request.GET.get('club') |
|
|
|
|
future = future_tournaments(club_id) |
|
|
|
|
live = live_tournaments(club_id) |
|
|
|
|
finished = finished_tournaments(club_id) |
|
|
|
|
tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True, 50) |
|
|
|
|
display_tournament = [t for t in tournaments if t.display_tournament()] |
|
|
|
|
live = [t for t in display_tournament if t.supposedly_in_progress()] |
|
|
|
|
future = [t for t in display_tournament if t.starts_in_the_future()] |
|
|
|
|
|
|
|
|
|
clean_ended_tournaments = tournaments_query(Q(end_date__isnull=False), club_id, 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()] |
|
|
|
|
|
|
|
|
|
# Combine both lists |
|
|
|
|
finished = clean_ended_tournaments + ended_tournaments |
|
|
|
|
|
|
|
|
|
# Sort the combined list by start_date in descending order |
|
|
|
|
finished.sort(key=lambda t: t.start_date, reverse=True) |
|
|
|
|
|
|
|
|
|
club = None |
|
|
|
|
if club_id: |
|
|
|
|
@ -127,7 +137,7 @@ def index(request): |
|
|
|
|
} |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def tournaments_query(query, club_id, ascending): |
|
|
|
|
def tournaments_query(query, club_id, ascending, limit=None): |
|
|
|
|
queries = [query, Q(is_private=False, is_deleted=False, event__club__isnull=False)] |
|
|
|
|
|
|
|
|
|
club = None |
|
|
|
|
@ -139,18 +149,46 @@ def tournaments_query(query, club_id, ascending): |
|
|
|
|
sortkey = 'start_date' |
|
|
|
|
if not ascending: |
|
|
|
|
sortkey = '-start_date' |
|
|
|
|
return Tournament.objects.filter(*queries).order_by(sortkey) |
|
|
|
|
|
|
|
|
|
def finished_tournaments(club_id): |
|
|
|
|
ended_tournaments = tournaments_query(Q(is_private=False, is_deleted=False, event__club__isnull=False), club_id, False) |
|
|
|
|
return [t for t in ended_tournaments if t.display_tournament() and t.should_be_over()] |
|
|
|
|
queryset = Tournament.objects.filter(*queries).prefetch_related( |
|
|
|
|
'groupstage_set', |
|
|
|
|
'round_set', |
|
|
|
|
'teamregistration_set', |
|
|
|
|
).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 |
|
|
|
|
|
|
|
|
|
def finished_tournaments(club_id, limit=None): |
|
|
|
|
clean_ended_tournaments = tournaments_query(Q(end_date__isnull=False), club_id, False, limit) |
|
|
|
|
clean_ended_tournaments = [t for t in clean_ended_tournaments if t.display_tournament()] |
|
|
|
|
|
|
|
|
|
one_day_ago = timezone.now() - timedelta(days=1) |
|
|
|
|
ended_tournaments = tournaments_query( |
|
|
|
|
Q(end_date__isnull=True, start_date__lt=one_day_ago), |
|
|
|
|
club_id, |
|
|
|
|
False, |
|
|
|
|
limit |
|
|
|
|
) |
|
|
|
|
ended_tournaments = [t for t in ended_tournaments if t.display_tournament() and t.should_be_over()] |
|
|
|
|
|
|
|
|
|
# Combine both lists |
|
|
|
|
all_tournaments = clean_ended_tournaments + ended_tournaments |
|
|
|
|
|
|
|
|
|
# Sort the combined list by start_date in descending order |
|
|
|
|
all_tournaments.sort(key=lambda t: t.start_date, reverse=True) |
|
|
|
|
|
|
|
|
|
return all_tournaments |
|
|
|
|
|
|
|
|
|
def live_tournaments(club_id): |
|
|
|
|
tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True) |
|
|
|
|
def live_tournaments(club_id, limit=None): |
|
|
|
|
tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True, limit) |
|
|
|
|
return [t for t in tournaments if t.display_tournament() and t.supposedly_in_progress()] |
|
|
|
|
|
|
|
|
|
def future_tournaments(club_id): |
|
|
|
|
tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True) |
|
|
|
|
def future_tournaments(club_id, limit=None): |
|
|
|
|
tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True, limit) |
|
|
|
|
return [t for t in tournaments if t.display_tournament() and t.starts_in_the_future()] |
|
|
|
|
|
|
|
|
|
def tournament_info(request, tournament_id): |
|
|
|
|
|