django-orchestra-test/orchestra/contrib/accounts/management/__init__.py

33 lines
1.3 KiB
Python
Raw Normal View History

import sys
import textwrap
2017-06-16 13:33:41 +00:00
from django.contrib.auth import get_user_model, base_user
2015-05-04 14:19:58 +00:00
from django.core.exceptions import FieldError
from django.core.management import execute_from_command_line
2015-05-04 14:19:58 +00:00
from django.db import models
def create_initial_superuser(**kwargs):
2015-06-12 13:01:56 +00:00
if '--noinput' not in sys.argv and '--fake' not in sys.argv and '--fake-initial' not in sys.argv:
model = get_user_model()
if not model.objects.filter(is_superuser=True).exists():
sys.stdout.write(textwrap.dedent("""
It appears that you just installed Accounts application.
You can now create a superuser:
""")
)
2015-05-04 14:19:58 +00:00
from ..models import Account
try:
2017-06-16 13:33:41 +00:00
Account.systemusers.field.model.objects.filter(account_id=1).exists()
2015-05-04 14:19:58 +00:00
except FieldError:
# avoid creating a systemuser when systemuser table is not ready
Account.save = models.Model.save
2017-06-16 13:33:41 +00:00
old_init = base_user.AbstractBaseUser.__init__
def remove_is_staff(*args, **kwargs):
kwargs.pop('is_staff', None)
old_init(*args, **kwargs)
base_user.AbstractBaseUser.__init__ = remove_is_staff
manager = sys.argv[0]
execute_from_command_line(argv=[manager, 'createsuperuser'])