from django.core.management.base import BaseCommand, CommandError from accounts.models import CustomUser from games.models import EventGame from submissions.models import Scope class Command(BaseCommand): help = "Manually create a Scope (match) and assign users to it." def add_arguments(self, parser): parser.add_argument("event_game_id", type=int) parser.add_argument("name", type=str) parser.add_argument( "--user", dest="usernames", action="append", default=[], help="Username to assign to this scope. Repeatable.", ) def handle(self, *args, **options): try: event_game = EventGame.objects.get(id=options["event_game_id"]) except EventGame.DoesNotExist: raise CommandError(f"No EventGame with id={options['event_game_id']}") usernames = options["usernames"] users = list(CustomUser.objects.filter(username__in=usernames)) missing = set(usernames) - {u.username for u in users} if missing: raise CommandError(f"Unknown usernames: {', '.join(sorted(missing))}") scope = Scope.objects.create(event_game=event_game, name=options["name"]) scope.assigned_users.set(users) self.stdout.write( self.style.SUCCESS( f"Created scope '{scope.name}' (id={scope.id}) " f"with {len(users)} assigned user(s)." ) )