添加initinstall命令

This commit is contained in:
zema1
2017-11-07 19:04:41 +08:00
parent c16543c830
commit 2d00ed802d
7 changed files with 45 additions and 20 deletions

View File

@@ -1,4 +1,3 @@
import os
from django.core.management.base import BaseCommand
from account.models import AdminType, ProblemPermission, User, UserProfile
@@ -10,17 +9,16 @@ class Command(BaseCommand):
try:
admin = User.objects.get(username="root")
if admin.admin_type == AdminType.SUPER_ADMIN:
if os.environ.get("OJ_ENV") != "production":
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))
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:

View File

@@ -0,0 +1,17 @@
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)))