Remove autoadmin code and move it to the external django-autoadmin package
This commit is contained in:
@@ -5,8 +5,6 @@ import tempfile
|
||||
|
||||
from django import apps
|
||||
from django.conf import settings
|
||||
from django.contrib.auth import models as auth_models
|
||||
from django.contrib.auth.models import User
|
||||
from django.contrib.auth.signals import user_logged_in
|
||||
from django.db.models.signals import post_migrate, post_save
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
@@ -14,8 +12,7 @@ from django.utils.translation import ugettext_lazy as _
|
||||
from common import settings as common_settings
|
||||
|
||||
from .handlers import (
|
||||
auto_admin_account_passwd_change, user_locale_profile_session_config,
|
||||
user_locale_profile_create
|
||||
user_locale_profile_session_config, user_locale_profile_create
|
||||
)
|
||||
from .links import (
|
||||
link_about, link_admin_site, link_current_user_details,
|
||||
@@ -26,48 +23,15 @@ from .links import (
|
||||
from .menus import (
|
||||
menu_facet, menu_main, menu_secondary, menu_setup, menu_tools
|
||||
)
|
||||
from .models import (
|
||||
AnonymousUserSingleton, AutoAdminSingleton, UserLocaleProfile
|
||||
)
|
||||
from .settings import (
|
||||
AUTO_ADMIN_USERNAME, AUTO_ADMIN_PASSWORD, AUTO_CREATE_ADMIN,
|
||||
TEMPORARY_DIRECTORY
|
||||
)
|
||||
from .models import AnonymousUserSingleton, UserLocaleProfile
|
||||
from .settings import TEMPORARY_DIRECTORY
|
||||
from .utils import validate_path
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def create_superuser_and_anonymous_user(sender, **kwargs):
|
||||
"""
|
||||
From https://github.com/lambdalisue/django-qwert/blob/master/qwert/autoscript/__init__.py
|
||||
From http://stackoverflow.com/questions/1466827/ --
|
||||
|
||||
Prevent interactive question about wanting a superuser created. (This code
|
||||
has to go in this otherwise empty "models" module so that it gets processed by
|
||||
the "syncdb" command during database creation.)
|
||||
|
||||
Create our own admin super user automatically.
|
||||
"""
|
||||
if kwargs['app_config'].__class__ == CommonApp:
|
||||
AutoAdminSingleton.objects.get_or_create()
|
||||
AnonymousUserSingleton.objects.get_or_create()
|
||||
|
||||
if AUTO_CREATE_ADMIN:
|
||||
try:
|
||||
auth_models.User.objects.get(username=AUTO_ADMIN_USERNAME)
|
||||
except auth_models.User.DoesNotExist:
|
||||
logger.info('Creating super admin user -- login: %s, password: %s', AUTO_ADMIN_USERNAME, AUTO_ADMIN_PASSWORD)
|
||||
assert auth_models.User.objects.create_superuser(AUTO_ADMIN_USERNAME, 'autoadmin@autoadmin.com', AUTO_ADMIN_PASSWORD)
|
||||
admin = auth_models.User.objects.get(username=AUTO_ADMIN_USERNAME)
|
||||
# Store the auto admin password properties to display the first login message
|
||||
auto_admin_properties, created = AutoAdminSingleton.objects.get_or_create()
|
||||
auto_admin_properties.account = admin
|
||||
auto_admin_properties.password = AUTO_ADMIN_PASSWORD
|
||||
auto_admin_properties.password_hash = admin.password
|
||||
auto_admin_properties.save()
|
||||
else:
|
||||
logger.info('Super admin user already exists. -- login: %s', AUTO_ADMIN_USERNAME)
|
||||
def create_anonymous_user(sender, **kwargs):
|
||||
AnonymousUserSingleton.objects.get_or_create()
|
||||
|
||||
|
||||
class CommonApp(apps.AppConfig):
|
||||
@@ -90,10 +54,9 @@ class CommonApp(apps.AppConfig):
|
||||
menu_setup.bind_links(links=[link_admin_site])
|
||||
menu_tools.bind_links(links=[link_maintenance_menu])
|
||||
|
||||
post_migrate.connect(create_superuser_and_anonymous_user, dispatch_uid='create_superuser_and_anonymous_user')
|
||||
post_save.connect(auto_admin_account_passwd_change, dispatch_uid='auto_admin_account_passwd_change', sender=User)
|
||||
user_logged_in.connect(user_locale_profile_session_config, dispatch_uid='user_locale_profile_session_config', sender=User)
|
||||
post_save.connect(user_locale_profile_create, dispatch_uid='user_locale_profile_create', sender=User)
|
||||
post_migrate.connect(create_anonymous_user, dispatch_uid='create_anonymous_user', sender=self)
|
||||
user_logged_in.connect(user_locale_profile_session_config, dispatch_uid='user_locale_profile_session_config', sender=settings.AUTH_USER_MODEL)
|
||||
post_save.connect(user_locale_profile_create, dispatch_uid='user_locale_profile_create', sender=settings.AUTH_USER_MODEL)
|
||||
|
||||
if (not validate_path(TEMPORARY_DIRECTORY)) or (not TEMPORARY_DIRECTORY):
|
||||
setattr(common_settings, 'TEMPORARY_DIRECTORY', tempfile.mkdtemp())
|
||||
|
||||
Reference in New Issue
Block a user