From 826af36faefeb874143e040344bebcb996178793 Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Wed, 12 Mar 2025 11:20:17 +0100 Subject: [PATCH] fix loading tournaments --- tournaments/models/tournament.py | 24 ++++++----- tournaments/urls.py | 1 + tournaments/views.py | 70 +++++++++++++++++++++++++++----- 3 files changed, 74 insertions(+), 21 deletions(-) diff --git a/tournaments/models/tournament.py b/tournaments/models/tournament.py index 270530a..17cf9cd 100644 --- a/tournaments/models/tournament.py +++ b/tournaments/models/tournament.py @@ -995,17 +995,19 @@ class Tournament(models.Model): return self.hide_teams_weight def is_build_and_not_empty(self): - if hasattr(self, '_prefetched_objects_cache'): - # Use prefetched data if available - has_group_stages = 'groupstage_set' in self._prefetched_objects_cache and len(self.groupstage_set.all()) > 0 - has_rounds = 'round_set' in self._prefetched_objects_cache and len(self.round_set.all()) > 0 - has_team_registrations = 'teamregistration_set' in self._prefetched_objects_cache and len(self.teamregistration_set.all()) >= 4 - else: - # Fall back to database queries if not prefetched - has_group_stages = self.groupstage_set.count() > 0 - has_rounds = self.round_set.count() > 0 - has_team_registrations = self.teamregistration_set.count() >= 4 - + # if hasattr(self, '_prefetched_objects_cache'): + # # Use prefetched data if available + # has_group_stages = 'groupstage_set' in self._prefetched_objects_cache and len(self.groupstage_set.all()) > 0 + # has_rounds = 'round_set' in self._prefetched_objects_cache and len(self.round_set.all()) > 0 + # has_team_registrations = 'teamregistration_set' in self._prefetched_objects_cache and len(self.teamregistration_set.all()) >= 4 + # else: + # # Fall back to database queries if not prefetched + # has_group_stages = self.groupstage_set.count() > 0 + # has_rounds = self.round_set.count() > 0 + # has_team_registrations = self.teamregistration_set.count() >= 4 + has_group_stages = self.groupstage_set.count() > 0 + has_rounds = self.round_set.count() > 0 + has_team_registrations = self.teamregistration_set.count() >= 4 return (has_group_stages or has_rounds) and has_team_registrations def day_duration_formatted(self): diff --git a/tournaments/urls.py b/tournaments/urls.py index 2bede6a..8ea531c 100644 --- a/tournaments/urls.py +++ b/tournaments/urls.py @@ -7,6 +7,7 @@ from . import views urlpatterns = [ path('reset///', views.CustomPasswordResetConfirmView.as_view(), name='password_reset_confirm'), path("", views.index, name="index"), + path('new/', views.index_new, name='index_new'), path("tournaments/", views.tournaments, name="tournaments"), path("clubs/", views.clubs, name="clubs"), path("clubs//", views.club, name="club"), diff --git a/tournaments/views.py b/tournaments/views.py index df8b489..3dec1d1 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -57,6 +57,7 @@ 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 @@ -107,12 +108,61 @@ from .forms import CustomPasswordChangeForm def index(request): club_id = request.GET.get('club') - tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True, 50) + future = future_tournaments(club_id) + live = live_tournaments(club_id) + finished = finished_tournaments(club_id) + + club = None + if club_id: + club = get_object_or_404(Club, pk=club_id) + + return render( + request, + "tournaments/tournaments.html", + { + 'future': future[:10], + 'live': live[:10], + 'ended': finished[:10], + 'club': club, + } + ) + +def tournaments_query(query, club_id, ascending): + queries = [query, Q(is_private=False, is_deleted=False, event__club__isnull=False)] + + club = None + if club_id: + club = get_object_or_404(Club, pk=club_id) + q_club = Q(event__club=club) + queries.append(q_club) + + 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()] + +def live_tournaments(club_id): + tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True) + 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) + return [t for t in tournaments if t.display_tournament() and t.starts_in_the_future()] + + +def index_new(request): + + club_id = request.GET.get('club') + tournaments = tournaments_query_new(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 = tournaments_query_new(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()] @@ -137,7 +187,7 @@ def index(request): } ) -def tournaments_query(query, club_id, ascending, limit=None): +def tournaments_query_new(query, club_id, ascending, limit=None): queries = [query, Q(is_private=False, is_deleted=False, event__club__isnull=False)] club = None @@ -162,12 +212,12 @@ def tournaments_query(query, club_id, ascending, limit=None): return queryset -def finished_tournaments(club_id, limit=None): - clean_ended_tournaments = tournaments_query(Q(end_date__isnull=False), club_id, False, limit) +def finished_tournaments_new(club_id, limit=None): + clean_ended_tournaments = tournaments_query_new(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( + ended_tournaments = tournaments_query_new( Q(end_date__isnull=True, start_date__lt=one_day_ago), club_id, False, @@ -183,12 +233,12 @@ def finished_tournaments(club_id, limit=None): return all_tournaments -def live_tournaments(club_id, limit=None): - tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True, limit) +def live_tournaments_new(club_id, limit=None): + tournaments = tournaments_query_new(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, limit=None): - tournaments = tournaments_query(Q(end_date__isnull=True), club_id, True, limit) +def future_tournaments_new(club_id, limit=None): + tournaments = tournaments_query_new(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):