fix initadmin script

This commit is contained in:
virusdefender
2017-11-25 12:54:29 +08:00
parent cf40deb97c
commit 1f9eca8b7d
5 changed files with 45 additions and 58 deletions

View File

@@ -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))

View File

@@ -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)))

View File

@@ -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")