2016-01-26 08:39:30 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
2017-01-23 08:48:04 +00:00
|
|
|
|
2017-02-10 03:41:01 +00:00
|
|
|
from account.models import AdminType, ProblemPermission, User, UserProfile
|
2017-01-23 08:48:04 +00:00
|
|
|
from utils.shortcuts import rand_str # NOQA
|
2016-01-26 05:12:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
def handle(self, *args, **options):
|
2016-02-12 11:46:49 +00:00
|
|
|
try:
|
|
|
|
admin = User.objects.get(username="root")
|
2016-09-25 06:07:45 +00:00
|
|
|
if admin.admin_type == AdminType.SUPER_ADMIN:
|
2016-02-12 11:46:49 +00:00
|
|
|
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: "))
|
2017-01-23 08:01:56 +00:00
|
|
|
if input() == "yes":
|
2017-08-20 00:35:59 +00:00
|
|
|
# todo remove this in product env
|
2017-01-23 08:36:22 +00:00
|
|
|
# rand_password = rand_str(length=6)
|
|
|
|
rand_password = "rootroot"
|
2016-02-12 11:46:49 +00:00
|
|
|
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:
|
2017-02-10 03:41:01 +00:00
|
|
|
user = User.objects.create(username="root", email="root@oj.com", admin_type=AdminType.SUPER_ADMIN,
|
|
|
|
problem_permission=ProblemPermission.ALL)
|
2017-01-23 08:01:56 +00:00
|
|
|
# for dev
|
2017-01-23 08:36:22 +00:00
|
|
|
# rand_password = rand_str(length=6)
|
|
|
|
rand_password = "rootroot"
|
|
|
|
user.set_password(rand_password)
|
2016-02-12 11:46:49 +00:00
|
|
|
user.save()
|
2017-01-24 05:21:49 +00:00
|
|
|
UserProfile.objects.create(user=user, time_zone="Asia/Shanghai")
|
2016-02-12 11:46:49 +00:00
|
|
|
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))
|