NDOJ/judge/management/commands/adduser.py

45 lines
1.4 KiB
Python
Raw Permalink Normal View History

2020-01-21 06:35:58 +00:00
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
class Command(BaseCommand):
2022-05-14 17:57:27 +00:00
help = "creates a user"
2020-01-21 06:35:58 +00:00
def add_arguments(self, parser):
2022-05-14 17:57:27 +00:00
parser.add_argument("name", help="username")
parser.add_argument("email", help="email, not necessary to be resolvable")
parser.add_argument("password", help="password for the user")
parser.add_argument(
"language",
nargs="?",
default=settings.DEFAULT_USER_LANGUAGE,
help="default language ID for user",
)
2020-01-21 06:35:58 +00:00
2022-05-14 17:57:27 +00:00
parser.add_argument(
"--superuser",
action="store_true",
default=False,
help="if specified, creates user with superuser privileges",
)
parser.add_argument(
"--staff",
action="store_true",
default=False,
help="if specified, creates user with staff privileges",
)
2020-01-21 06:35:58 +00:00
def handle(self, *args, **options):
2022-05-14 17:57:27 +00:00
usr = User(username=options["name"], email=options["email"], is_active=True)
usr.set_password(options["password"])
usr.is_superuser = options["superuser"]
usr.is_staff = options["staff"]
2020-01-21 06:35:58 +00:00
usr.save()
profile = Profile(user=usr)
2022-05-14 17:57:27 +00:00
profile.language = Language.objects.get(key=options["language"])
2020-01-21 06:35:58 +00:00
profile.save()