diff --git a/server/management/commands/open_or_close_tournament_registrations.py b/server/management/commands/open_or_close_tournament_registrations.py index 68fe3cc6..67f25c82 100644 --- a/server/management/commands/open_or_close_tournament_registrations.py +++ b/server/management/commands/open_or_close_tournament_registrations.py @@ -3,13 +3,11 @@ from django.core.management.base import BaseCommand from django.utils.timezone import now -from server.tournament.models import Tournament +from server.tournament.models import Registration, Tournament class Command(BaseCommand): - help = ( - "Check if registrations are open or closed today, and update tournament status accordingly" - ) + help = "Check if registrations are open or closed today, and update tournament status accordingly. Also update registration's player teams on day of tournament" def handle(self, *args: Any, **options: Any) -> None: today = now().date() @@ -26,3 +24,13 @@ def handle(self, *args: Any, **options: Any) -> None: if team_registrations_closed_today.count() > 0: team_registrations_closed_today.update(status=Tournament.Status.SCHEDULING) + + tournaments_that_starts_today = Tournament.objects.filter( + event__start_date__exact=today, status=Tournament.Status.LIVE + ) + + for tournament in tournaments_that_starts_today: + registrations = Registration.objects.filter(event=tournament.event) + + for reg in registrations: + reg.player.teams.add(reg.team) diff --git a/server/migrations/0091_backfill_team_to_player_from_registrations.py b/server/migrations/0091_backfill_team_to_player_from_registrations.py new file mode 100644 index 00000000..67fc2c38 --- /dev/null +++ b/server/migrations/0091_backfill_team_to_player_from_registrations.py @@ -0,0 +1,29 @@ +from django.db import migrations +from django.db.backends.base.schema import BaseDatabaseSchemaEditor +from django.db.migrations.state import StateApps + + +def add_team_to_player_from_registrations( + apps: StateApps, schema_editor: BaseDatabaseSchemaEditor +) -> None: + SeriesRegistration = apps.get_model("server", "SeriesRegistration") # noqa: N806 + series_registrations = SeriesRegistration.objects.all() + + Registration = apps.get_model("server", "Registration") # noqa: N806 + registrations = Registration.objects.all() + + for series_registration in series_registrations: + series_registration.player.teams.add(series_registration.team) + + for registration in registrations: + registration.player.teams.add(registration.team) + + +class Migration(migrations.Migration): + dependencies = [("server", "0090_alter_player_teams")] + + operations = [ + migrations.RunPython( + code=add_team_to_player_from_registrations, reverse_code=migrations.RunPython.noop + ) + ]