From 1f9eca8b7d32979124f3ec5487112a12a4dd25c9 Mon Sep 17 00:00:00 2001 From: virusdefender Date: Sat, 25 Nov 2017 12:54:29 +0800 Subject: [PATCH] fix initadmin script --- .travis.yml | 2 -- deploy/run.sh | 2 +- utils/management/commands/initadmin.py | 38 -------------------- utils/management/commands/initinstall.py | 17 --------- utils/management/commands/inituser.py | 44 ++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 58 deletions(-) delete mode 100644 utils/management/commands/initadmin.py delete mode 100644 utils/management/commands/initinstall.py create mode 100644 utils/management/commands/inituser.py diff --git a/.travis.yml b/.travis.yml index 7782d02..5a01bfa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,11 +9,9 @@ before_install: - docker run -it -d -e POSTGRES_DB=onlinejudge -e POSTGRES_USER=onlinejudge -e POSTGRES_PASSWORD=onlinejudge -p 127.0.0.1:5433:5432 postgres:10 install: - pip install -r deploy/requirements.txt - - mkdir log test_case upload - cp oj/custom_settings.example.py oj/custom_settings.py - echo "SECRET_KEY=\"`cat /dev/urandom | head -1 | md5sum | head -c 32`\"" >> oj/custom_settings.py - python manage.py migrate - - python manage.py initadmin script: - docker ps -a - flake8 . diff --git a/deploy/run.sh b/deploy/run.sh index 356ee94..bcf7476 100644 --- a/deploy/run.sh +++ b/deploy/run.sh @@ -21,7 +21,7 @@ n=0 while [ $n -lt 5 ] do python manage.py migrate --no-input && - python manage.py initinstall && + python manage.py inituser --username=root --password=rootroot --action=create_super_admin && break n=$(($n+1)) echo "Failed to migrate, going to retry..." diff --git a/utils/management/commands/initadmin.py b/utils/management/commands/initadmin.py deleted file mode 100644 index 78e8a45..0000000 --- a/utils/management/commands/initadmin.py +++ /dev/null @@ -1,38 +0,0 @@ -from django.core.management.base import BaseCommand - -from account.models import AdminType, ProblemPermission, User, UserProfile -from utils.shortcuts import rand_str # NOQA - - -class Command(BaseCommand): - def handle(self, *args, **options): - try: - admin = User.objects.get(username="root") - if admin.admin_type == AdminType.SUPER_ADMIN: - self.stdout.write(self.style.WARNING("Super admin user 'root' already exists, " - "would you like to reset it's password?\n" - "Input yes to confirm: ")) - if input() == "yes": - rand_password = "rootroot" - admin.save() - self.stdout.write(self.style.SUCCESS("Successfully created super admin user password.\n" - "Username: root\nPassword: %s\n" - "Remember to change password and turn on two factors auth " - "after installation." % rand_password)) - else: - self.stdout.write(self.style.SUCCESS("Nothing happened")) - else: - self.stdout.write(self.style.ERROR("User 'root' is not super admin.")) - except User.DoesNotExist: - user = User.objects.create(username="root", email="root@oj.com", admin_type=AdminType.SUPER_ADMIN, - problem_permission=ProblemPermission.ALL) - # for dev - # rand_password = rand_str(length=6) - rand_password = "rootroot" - user.set_password(rand_password) - user.save() - UserProfile.objects.create(user=user) - self.stdout.write(self.style.SUCCESS("Successfully created super admin user.\n" - "Username: root\nPassword: %s\n" - "Remember to change password and turn on two factors auth " - "after installation." % rand_password)) diff --git a/utils/management/commands/initinstall.py b/utils/management/commands/initinstall.py deleted file mode 100644 index bdb0f0a..0000000 --- a/utils/management/commands/initinstall.py +++ /dev/null @@ -1,17 +0,0 @@ -import os -from account.models import User -from django.core.management.base import BaseCommand - - -class Command(BaseCommand): - def handle(self, *args, **options): - if User.objects.exists(): - self.stdout.write(self.style.WARNING("Nothing happened\n")) - return - try: - if os.system("python manage.py initadmin") != 0: - self.stdout.write(self.style.ERROR("Failed to execute command 'initadmin'")) - exit(1) - self.stdout.write(self.style.SUCCESS("Done")) - except Exception as e: - self.stdout.write(self.style.ERROR("Failed to initialize, error: " + str(e))) diff --git a/utils/management/commands/inituser.py b/utils/management/commands/inituser.py new file mode 100644 index 0000000..c3f0827 --- /dev/null +++ b/utils/management/commands/inituser.py @@ -0,0 +1,44 @@ +from django.core.management.base import BaseCommand + +from account.models import AdminType, ProblemPermission, User, UserProfile +from utils.shortcuts import rand_str # NOQA + + +class Command(BaseCommand): + def add_arguments(self, parser): + parser.add_argument("--username", type=str) + parser.add_argument("--password", type=str) + parser.add_argument("--action", type=str) + + def handle(self, *args, **options): + username = options["username"] + password = options["password"] + action = options["action"] + + if not(username and password and action): + self.stdout.write(self.style.ERROR("Invalid args")) + exit(1) + + if action == "create_super_admin": + if User.objects.filter(username=username).exists(): + self.stdout.write(self.style.SUCCESS(f"User {username} exists, operation ignored")) + exit() + + user = User.objects.create(username=username, admin_type=AdminType.SUPER_ADMIN, + problem_permission=ProblemPermission.ALL) + user.set_password(password) + user.save() + UserProfile.objects.create(user=user) + + self.stdout.write(self.style.SUCCESS("User created")) + elif action == "reset": + try: + user = User.objects.get(username=username) + user.set_password(password) + user.save() + self.stdout.write(self.style.SUCCESS(f"Password is rested")) + except User.DoesNotExist: + self.stdout.write(self.style.ERROR(f"User {username} doesnot exist, operation ignored")) + exit(1) + else: + raise ValueError("Invalid action")