Move app initialization code from __init__.py to post_init.py

This commit is contained in:
Roberto Rosario
2012-09-27 01:48:31 -04:00
parent 3b895d7560
commit 1d8266fe5d
2 changed files with 13 additions and 61 deletions

View File

@@ -1,70 +1,11 @@
from __future__ import absolute_import from __future__ import absolute_import
import tempfile
from south.signals import post_migrate
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth import models as auth_models
from django.contrib.auth.models import User
from django.dispatch import receiver
from django.conf import settings from django.conf import settings
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from navigation import Link
from navigation.api import bind_links, register_top_menu
from .settings import (AUTO_CREATE_ADMIN, AUTO_ADMIN_USERNAME,
AUTO_ADMIN_PASSWORD, TEMPORARY_DIRECTORY)
from .links import (link_password_change, link_current_user_details,
link_current_user_edit, link_about, link_license, link_admin_site)
from .models import AutoAdminSingleton
from .debug import insert_pdb_exception_hook from .debug import insert_pdb_exception_hook
from .literals import PAGE_SIZE_LETTER, PAGE_ORIENTATION_PORTRAIT
if getattr(settings, 'DEBUG_ON_EXCEPTION', False): if getattr(settings, 'DEBUG_ON_EXCEPTION', False):
insert_pdb_exception_hook() insert_pdb_exception_hook()
bind_links(['about_view', 'license_view'], [link_about, link_license], menu_name='secondary_menu')
bind_links(['current_user_details', 'current_user_edit', 'password_change_view'], [link_current_user_details, link_current_user_edit, link_password_change], menu_name='secondary_menu')
register_top_menu('about', link=link_about, position=-1)
@receiver(post_migrate, dispatch_uid='create_superuser')
def create_superuser(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 AUTO_CREATE_ADMIN and kwargs['app'] == 'common':
try:
auth_models.User.objects.get(username=AUTO_ADMIN_USERNAME)
except auth_models.User.DoesNotExist:
print '*' * 80
print 'Creating super admin user -- login: %s, password: %s' % (AUTO_ADMIN_USERNAME, AUTO_ADMIN_PASSWORD)
print '*' * 80
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 = AutoAdminSingleton.objects.get()
auto_admin_properties.account = admin
auto_admin_properties.password = AUTO_ADMIN_PASSWORD
auto_admin_properties.password_hash = admin.password
auto_admin_properties.save()
else:
print 'Super admin user already exists. -- login: %s' % AUTO_ADMIN_USERNAME
@receiver(post_save, dispatch_uid='auto_admin_account_passwd_change', sender=User)
def auto_admin_account_passwd_change(sender, instance, **kwargs):
auto_admin_properties = AutoAdminSingleton.singleton.get()
if instance == auto_admin_properties.account and instance.password != auto_admin_properties.password_hash:
# Only delete the auto admin properties when the password has been changed
auto_admin_properties.delete(force=True)

View File

@@ -2,10 +2,21 @@ from __future__ import absolute_import
import tempfile import tempfile
from .utils import validate_path from django.utils.translation import ugettext_lazy as _
import common.settings as common_settings
from navigation.api import bind_links, register_top_menu
from .utils import validate_path
from .links import (link_password_change, link_current_user_details,
link_current_user_edit, link_about, link_license, link_admin_site)
from .settings import TEMPORARY_DIRECTORY from .settings import TEMPORARY_DIRECTORY
import common.settings as common_settings
if (validate_path(getattr(common_settings, 'TEMPORARY_DIRECTORY')) == False) or (not getattr(common_settings, 'TEMPORARY_DIRECTORY')): if (validate_path(getattr(common_settings, 'TEMPORARY_DIRECTORY')) == False) or (not getattr(common_settings, 'TEMPORARY_DIRECTORY')):
setattr(common_settings, 'TEMPORARY_DIRECTORY', tempfile.mkdtemp()) setattr(common_settings, 'TEMPORARY_DIRECTORY', tempfile.mkdtemp())
bind_links(['about_view', 'license_view'], [link_about, link_license], menu_name='secondary_menu')
bind_links(['current_user_details', 'current_user_edit', 'password_change_view'], [link_current_user_details, link_current_user_edit, link_password_change], menu_name='secondary_menu')
register_top_menu('about', link=link_about, position=-1)