Cleanup auto admin user creation using receiver decorator

This commit is contained in:
Roberto Rosario
2012-01-07 23:28:26 -04:00
parent 33513d34a5
commit df5f50f5d4

View File

@@ -6,6 +6,8 @@ from django.utils.translation import ugettext_lazy as _
from django.contrib.auth import models as auth_models
from django.contrib.auth.management import create_superuser
from django.db.models import signals
from django.dispatch import receiver
from django.db.models.signals import post_syncdb
from navigation.api import register_links, register_top_menu
@@ -31,34 +33,37 @@ register_links(['about_view', 'changelog_view', 'license_view'], [about_view, ch
register_top_menu('about', link={'text': _(u'about'), 'view': 'about_view', 'famfam': 'information'}, position=-1)
if common_settings.AUTO_CREATE_ADMIN:
# 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 test user automatically.
#if common_settings.AUTO_CREATE_ADMIN:
# # 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.
def create_testuser(app, created_models, verbosity, **kwargs):
@receiver(post_syncdb, dispatch_uid='create_superuser', sender=auth_models)
#def create_superuser(app, created_models, verbosity, **kwargs):
def create_superuser(sender, **kwargs):
if common_settings.AUTO_CREATE_ADMIN:
USERNAME = common_settings.AUTO_ADMIN_USERNAME
PASSWORD = common_settings.AUTO_ADMIN_PASSWORD
try:
auth_models.User.objects.get(username=USERNAME)
except auth_models.User.DoesNotExist:
print '*' * 80
print 'Creating test user -- login: %s, password: %s' % (USERNAME, PASSWORD)
print 'Creating super admin user -- login: %s, password: %s' % (USERNAME, PASSWORD)
print '*' * 80
assert auth_models.User.objects.create_superuser(USERNAME, 'x@x.com', PASSWORD)
else:
print 'Test user already exists. -- login: %s, password: %s' % (USERNAME, PASSWORD)
signals.post_syncdb.disconnect(
create_superuser,
sender=auth_models,
dispatch_uid='django.contrib.auth.management.create_superuser')
signals.post_syncdb.connect(create_testuser,
sender=auth_models, dispatch_uid='common.models.create_testuser')
print 'Super admin user already exists. -- login: %s, password: %s' % (USERNAME, PASSWORD)
#signals.post_syncdb.disconnect(
# create_superuser,
# sender=auth_models,
# dispatch_uid='django.contrib.auth.management.create_superuser')
#signals.post_syncdb.connect(create_testuser,
# sender=auth_models, dispatch_uid='common.models.create_testuser')
if (validate_path(common_settings.TEMPORARY_DIRECTORY) == False) or (not common_settings.TEMPORARY_DIRECTORY):
setattr(common_settings, 'TEMPORARY_DIRECTORY', tempfile.mkdtemp())