Move the auto admin creation from the post_sync signal to the post_migrate signal, only deletec auto admin properties when password has been really changed

This commit is contained in:
Roberto Rosario
2012-06-28 11:35:58 -04:00
parent 2ac89baa8c
commit f503045aa1

View File

@@ -2,12 +2,14 @@ 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.contrib.auth.management import create_superuser
from django.dispatch import receiver
from django.db.models.signals import post_syncdb, post_save
from django.db.models.signals import post_save
from navigation.api import register_links, register_top_menu
@@ -35,7 +37,7 @@ register_links(['about_view', 'license_view'], [about_view, license_view], menu_
register_top_menu('about', link={'text': _(u'about'), 'view': 'about_view', 'famfam': 'information'}, position=-1)
@receiver(post_syncdb, dispatch_uid='create_superuser', sender=auth_models)
@receiver(post_migrate, dispatch_uid='create_superuser')#, sender=auth_models)
def create_superuser(sender, **kwargs):
"""
From https://github.com/lambdalisue/django-qwert/blob/master/qwert/autoscript/__init__.py
@@ -48,7 +50,7 @@ def create_superuser(sender, **kwargs):
Create our own admin super user automatically.
"""
if AUTO_CREATE_ADMIN:
if AUTO_CREATE_ADMIN and kwargs['app'] == 'common':
try:
auth_models.User.objects.get(username=AUTO_ADMIN_USERNAME)
except auth_models.User.DoesNotExist:
@@ -58,8 +60,9 @@ def create_superuser(sender, **kwargs):
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)
auto_admin_properties = AutoAdminSingleton.objects.get()
auto_admin_properties.auto_admin_account = admin
auto_admin_properties.auto_admin_password = AUTO_ADMIN_PASSWORD
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
@@ -68,7 +71,7 @@ def create_superuser(sender, **kwargs):
@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.objects.get()
if instance == auto_admin_properties.auto_admin_account:
if instance == auto_admin_properties.account and instance.password != auto_admin_properties.password_hash:
auto_admin_properties.delete(force=True)