fix loading tournaments

shop
Razmig Sarkissian 8 months ago
parent 190391a0bd
commit 0a4efc69d7
  1. 71
      tournaments/views.py

@ -106,61 +106,12 @@ from django.views.generic.edit import UpdateView
from .forms import CustomPasswordChangeForm from .forms import CustomPasswordChangeForm
def index(request): def index(request):
club_id = request.GET.get('club')
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):
now = timezone.now() now = timezone.now()
thirty_days_ago = now - timedelta(days=30) thirty_days_ago = now - timedelta(days=30)
thirty_days_future = now + timedelta(days=30) thirty_days_future = now + timedelta(days=30)
club_id = request.GET.get('club') club_id = request.GET.get('club')
tournaments = tournaments_query_new(Q(end_date__isnull=True, start_date__gte=thirty_days_ago, start_date__lte=thirty_days_future), club_id, True, 50) tournaments = tournaments_query(Q(end_date__isnull=True, start_date__gte=thirty_days_ago, start_date__lte=thirty_days_future), club_id, True, 50)
display_tournament = [t for t in tournaments if t.display_tournament()] display_tournament = [t for t in tournaments if t.display_tournament()]
live = [] live = []
future = [] future = []
@ -169,7 +120,7 @@ def index_new(request):
live.append(t) live.append(t)
elif t.starts_in_the_future(): elif t.starts_in_the_future():
future.append(t) future.append(t)
clean_ended_tournaments = tournaments_query_new(Q(end_date__isnull=False), club_id, False, 50) 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()] 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 = [t for t in display_tournament if t.should_be_over()]
@ -194,7 +145,7 @@ def index_new(request):
} }
) )
def tournaments_query_new(query, club_id, ascending, limit=None): def tournaments_query(query, club_id, ascending, limit=None):
queries = [query, Q(is_private=False, is_deleted=False, event__club__isnull=False)] queries = [query, Q(is_private=False, is_deleted=False, event__club__isnull=False)]
club = None club = None
@ -219,12 +170,12 @@ def tournaments_query_new(query, club_id, ascending, limit=None):
return queryset return queryset
def finished_tournaments_new(club_id, limit=None): def finished_tournaments(club_id, limit=None):
clean_ended_tournaments = tournaments_query_new(Q(end_date__isnull=False), club_id, False, limit) 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()] clean_ended_tournaments = [t for t in clean_ended_tournaments if t.display_tournament()]
one_day_ago = timezone.now() - timedelta(days=1) one_day_ago = timezone.now() - timedelta(days=1)
ended_tournaments = tournaments_query_new( ended_tournaments = tournaments_query(
Q(end_date__isnull=True, start_date__lt=one_day_ago), Q(end_date__isnull=True, start_date__lt=one_day_ago),
club_id, club_id,
False, False,
@ -240,12 +191,12 @@ def finished_tournaments_new(club_id, limit=None):
return all_tournaments return all_tournaments
def live_tournaments_new(club_id, limit=None): def live_tournaments(club_id, limit=None):
tournaments = tournaments_query_new(Q(end_date__isnull=True), club_id, True, limit) 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()] return [t for t in tournaments if t.display_tournament() and t.supposedly_in_progress()]
def future_tournaments_new(club_id, limit=None): def future_tournaments(club_id, limit=None):
tournaments = tournaments_query_new(Q(end_date__isnull=True), club_id, True, limit) 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()] return [t for t in tournaments if t.display_tournament() and t.starts_in_the_future()]
def tournament_info(request, tournament_id): def tournament_info(request, tournament_id):
@ -307,7 +258,7 @@ def tournaments(request):
tournaments = live_tournaments(club_id) tournaments = live_tournaments(club_id)
elif filter==2: elif filter==2:
title = 'Terminés' title = 'Terminés'
tournaments = finished_tournaments_new(club_id) tournaments = finished_tournaments(club_id)
return render( return render(
request, request,

Loading…
Cancel
Save