Added options to automatically create an admin user during syncdb

This commit is contained in:
Roberto Rosario
2011-07-07 23:53:15 -04:00
parent f3d3af0552
commit 038a0ea041
2 changed files with 58 additions and 1 deletions

View File

@@ -1,9 +1,13 @@
import tempfile
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 navigation.api import register_links
from common.conf import settings as common_settings
from navigation.api import register_links
TEMPORARY_DIRECTORY = common_settings.TEMPORARY_DIRECTORY \
if common_settings.TEMPORARY_DIRECTORY else tempfile.mkdtemp()
@@ -17,3 +21,32 @@ current_user_details = {'text': _(u'user details'), 'view': 'current_user_detail
current_user_edit = {'text': _(u'edit details'), 'view': 'current_user_edit', 'famfam': 'vcard_edit'}
register_links(['current_user_details', 'current_user_edit', 'password_change_view'], [current_user_details, current_user_edit, password_change_view], menu_name='secondary_menu')
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.
def create_testuser(app, created_models, verbosity, **kwargs):
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 '*' * 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')

View File

@@ -31,3 +31,27 @@ DEFAULT_PAGE_ORIENTATION = register_setting(
global_name=u'COMMON_DEFAULT_PAGE_ORIENTATION',
default=PAGE_ORIENTATION_PORTRAIT,
)
register_setting(
namespace=u'common',
module=u'common.conf.settings',
name=u'AUTO_CREATE_ADMIN',
global_name=u'COMMON_AUTO_CREATE_ADMIN',
default=True,
)
register_setting(
namespace=u'common',
module=u'common.conf.settings',
name=u'AUTO_ADMIN_USERNAME',
global_name=u'COMMON_AUTO_ADMIN_USERNAME',
default=u'admin',
)
register_setting(
namespace=u'common',
module=u'common.conf.settings',
name=u'AUTO_ADMIN_PASSWORD',
global_name=u'COMMON_AUTO_ADMIN_PASSWORD',
default=u'admin',
)