From 586de4431fc70d120d8fc6f056436aeac1c9ea4d Mon Sep 17 00:00:00 2001 From: Razmig Sarkissian Date: Fri, 5 Sep 2025 15:44:20 +0200 Subject: [PATCH] Remove Broadcasted Auto Event Template --- .../broadcast/broadcasted_auto_event.html | 263 ------------------ tournaments/views.py | 79 +++++- 2 files changed, 72 insertions(+), 270 deletions(-) delete mode 100644 tournaments/templates/tournaments/broadcast/broadcasted_auto_event.html diff --git a/tournaments/templates/tournaments/broadcast/broadcasted_auto_event.html b/tournaments/templates/tournaments/broadcast/broadcasted_auto_event.html deleted file mode 100644 index cf8a9f7..0000000 --- a/tournaments/templates/tournaments/broadcast/broadcasted_auto_event.html +++ /dev/null @@ -1,263 +0,0 @@ - - - {% load static %} - {% load qr_code %} - - - {% include 'tournaments/broadcast/base_head.html' %} - - - - Broadcast - - - - - - - - -
-
- -
- - -
-
-
- - - - - - - - -
- -
-
- -
- - diff --git a/tournaments/views.py b/tournaments/views.py index d9f7475..3e392e0 100644 --- a/tournaments/views.py +++ b/tournaments/views.py @@ -532,11 +532,29 @@ def automatic_broadcast(request, tournament_id): def automatic_broadcast_event(request, event_id): event = get_object_or_404(Event, pk=event_id) - tournaments = Tournament.objects.filter(event=event).order_by('start_date') + now = timezone.now() - # Filter tournaments that have broadcast content - tournaments_with_content = [] - for tournament in tournaments: + # Initial time windows + past_cutoff = now - timedelta(hours=4) + future_cutoff = now + timedelta(hours=4) + + # Get recent/current tournaments (started but not too old) + recent_tournaments = Tournament.objects.filter( + event=event, + start_date__gte=past_cutoff, + start_date__lte=now + ).order_by('start_date') + + # Get near-future tournaments + future_tournaments = Tournament.objects.filter( + event=event, + start_date__gt=now, + start_date__lte=future_cutoff + ).order_by('start_date') + + # Filter recent tournaments that have content + recent_with_content = [] + for tournament in recent_tournaments: try: content = tournament.broadcast_content() has_content = ( @@ -546,12 +564,59 @@ def automatic_broadcast_event(request, event_id): len(content.get('rankings', [])) > 0 ) if has_content: - tournaments_with_content.append(tournament) + recent_with_content.append(tournament) except: - # Skip tournaments that error when getting content continue - tournament_ids = [str(tournament.id) for tournament in tournaments_with_content] + tournaments_to_display = recent_with_content[:] + + # If we have 0 or 1 recent tournaments, extend the search + if len(recent_with_content) <= 1: + needed = 2 - len(recent_with_content) + + # First, try to add future tournaments (within 4 hours) + added_from_future = 0 + for tournament in future_tournaments: + if added_from_future >= needed: + break + tournaments_to_display.append(tournament) + added_from_future += 1 + + # If still not enough, extend past search (beyond 4 hours) + if len(tournaments_to_display) < 2: + extended_past_tournaments = Tournament.objects.filter( + event=event, + start_date__lt=past_cutoff # Older than 4 hours ago + ).order_by('-start_date') # Most recent first + + still_needed = 2 - len(tournaments_to_display) + for tournament in extended_past_tournaments[:still_needed]: + # Check if this past tournament has content + try: + content = tournament.broadcast_content() + has_content = ( + len(content.get('matches', [])) > 0 or + len(content.get('group_stages', [])) > 0 or + len(content.get('summons', [])) > 0 or + len(content.get('rankings', [])) > 0 + ) + if has_content: + tournaments_to_display.append(tournament) + except: + continue + + # If still not enough, extend future search (beyond 4 hours) + if len(tournaments_to_display) < 2: + extended_future_tournaments = Tournament.objects.filter( + event=event, + start_date__gt=future_cutoff # More than 4 hours in the future + ).order_by('start_date') + + still_needed = 2 - len(tournaments_to_display) + for tournament in extended_future_tournaments[:still_needed]: + tournaments_to_display.append(tournament) + + tournament_ids = [str(tournament.id) for tournament in tournaments_to_display] # Create QR code URL for the event broadcast qr_code_url_val = reverse('automatic-broadcast-event', args=[event_id])