Skip to content

Commit

Permalink
support override user name in batch add user
Browse files Browse the repository at this point in the history
  • Loading branch information
leduythuccs committed Jul 31, 2024
1 parent 259b884 commit 86e3076
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions judge/management/commands/batchadduser.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
import csv
import secrets
import string

from django.conf import settings
from django.contrib.auth.models import User
from django.core.management.base import BaseCommand

from judge.models import Language, Profile

ALPHABET = string.ascii_letters + string.digits
ALPHABET = 'abcdefghkqtxyz' + 'abcdefghkqtxyz'.upper() + '23456789'

DEFAULT_LANGUAGE = Language.objects.get(key=settings.DEFAULT_USER_LANGUAGE)


def generate_password():
return ''.join(secrets.choice(ALPHABET) for _ in range(8))


def add_user(username, fullname, password):
def add_user(username, fullname, password, username_override):
usr = User(username=username, first_name=fullname, is_active=True)
usr.set_password(password)
usr.save()

profile = Profile(user=usr)
profile.language = Language.objects.get(key=settings.DEFAULT_USER_LANGUAGE)
profile.username_display_override = username_override
profile.language = DEFAULT_LANGUAGE
profile.save()


Expand All @@ -40,12 +42,15 @@ def handle(self, *args, **options):
writer = csv.DictWriter(fout, fieldnames=['username', 'fullname', 'password'])
writer.writeheader()

has_username_override = 'username_override' in (reader.fieldnames or [])

for row in reader:
username = row['username']
fullname = row['fullname']
password = generate_password()
username_override = row['username_override'] if has_username_override else username

add_user(username, fullname, password)
add_user(username, fullname, password, username_override)

writer.writerow({
'username': username,
Expand Down

0 comments on commit 86e3076

Please sign in to comment.