Generateurv2/backend/api/room/management/commands/add_challenger.py
2022-06-24 13:42:16 +02:00

35 lines
1.3 KiB
Python

from django.core.management.base import BaseCommand, CommandError
from room.models import Room
import string
import random
import faker
import uuid
class Command(BaseCommand):
help = 'Closes the specified poll for voting'
def add_arguments(self, parser):
parser.add_argument('roomId', nargs='+', type=str)
parser.add_argument('number', nargs='+', type=int)
def handle(self, *args, **options):
try:
room = Room.objects.get(id_code = options['roomId'][0])
except:
raise CommandError(f'Room "{options["roomId"][0]}" does not exist')
newMembers = []
for i in range(options['number'][0]):
while True:
code = ''.join(random.choices(string.ascii_uppercase, k=6))
if code not in [p['code'] for p in room.anonymousMembers]:
break
while True:
uid = uuid.uuid4()
if uid not in [p['clientId'] for p in room.anonymousMembers]:
break
fake = faker.Faker()
newMembers.append({'nick': fake.name(), 'clientId': str(uid), 'code': code, 'owner': False, 'online': True})
room.anonymousMembers = [*newMembers, *room.anonymousMembers]
room.save()
self.stdout.write(self.style.SUCCESS(
f'Successfully added {options["number"][0]} participants to room {options["roomId"][0]}'))