Remove autoadmin code and move it to the external django-autoadmin package

This commit is contained in:
Roberto Rosario
2015-06-14 03:51:36 -04:00
parent f3beb3b5e7
commit 37b4f42afe
10 changed files with 44 additions and 103 deletions

View File

@@ -11,8 +11,8 @@
{% block content_plain %}
<div class="row">
<div class="col-xs-10 col-xs-offset-1 col-sm-8 col-sm-offset-2 col-md-6 col-md-offset-3 col-lg-4 col-lg-offset-4">
{% auto_admin_properties %}
{% if auto_admin_properties.account %}
{% autoadmin_properties %}
{% if autoadmin_properties.account %}
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">{% trans "First time login" %}</h3>
@@ -20,9 +20,9 @@
<div class="panel-body">
<p>{% trans 'You have just finished installing <strong>Mayan EDMS</strong>, congratulations!' %}</p>
<p>{% trans 'Login using the following credentials:' %}</p>
<p>{% blocktrans with auto_admin_properties.account as account %}Username: <strong>{{ account }}</strong>{% endblocktrans %}</p>
<p>{% blocktrans with auto_admin_properties.account.email as email %}Email: <strong>{{ email }}</strong>{% endblocktrans %}</p>
<p>{% blocktrans with auto_admin_properties.password as password %}Password: <strong>{{ password }}</strong>{% endblocktrans %}</p>
<p>{% blocktrans with autoadmin_properties.account as account %}Username: <strong>{{ account }}</strong>{% endblocktrans %}</p>
<p>{% blocktrans with autoadmin_properties.account.email as email %}Email: <strong>{{ email }}</strong>{% endblocktrans %}</p>
<p>{% blocktrans with autoadmin_properties.password as password %}Password: <strong>{{ password }}</strong>{% endblocktrans %}</p>
<p>{% trans 'Be sure to change the password to increase security and to disable this message.' %}</p>
</div>
</div>

View File

@@ -2,7 +2,6 @@ from __future__ import unicode_literals
from django.contrib import admin
from .models import AutoAdminSingleton, SharedUploadedFile
from .models import SharedUploadedFile
admin.site.register(AutoAdminSingleton)
admin.site.register(SharedUploadedFile)

View File

@@ -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())

View File

@@ -3,20 +3,10 @@ from __future__ import unicode_literals
from django.conf import settings
from .models import (
AnonymousUserSingleton, AutoAdminSingleton, UserLocaleProfile
AnonymousUserSingleton, UserLocaleProfile
)
def auto_admin_account_passwd_change(sender, instance, **kwargs):
auto_admin_properties = AutoAdminSingleton.objects.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.account = None
auto_admin_properties.password = None
auto_admin_properties.password_hash = None
auto_admin_properties.save()
def user_locale_profile_session_config(sender, request, user, **kwargs):
if hasattr(request, 'session'):
user_locale_profile, created = UserLocaleProfile.objects.get_or_create(user=user)

View File

@@ -10,7 +10,8 @@ from django.utils.crypto import get_random_string
class Command(management.BaseCommand):
help = 'Gets Mayan EDMS ready to be used (initializes database, creates a secret key, etc).'
def _generate_secret_key(self):
@staticmethod
def _generate_secret_key():
chars = 'abcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*(-_=+)'
return get_random_string(50, chars)
@@ -26,7 +27,8 @@ class Command(management.BaseCommand):
'',
'from .base import *',
'',
"SECRET_KEY = '{0}'".format(self._generate_secret_key()),
"SECRET_KEY = '{0}'".format(Command._generate_secret_key()),
'',
]))
management.call_command('syncdb', migrate=True, interactive=False)
management.call_command('migrate', interactive=False)
management.call_command('createautoadmin', interactive=False)

View File

@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('common', '0002_auto_20150608_1902'),
]
operations = [
migrations.RemoveField(
model_name='autoadminsingleton',
name='account',
),
migrations.DeleteModel(
name='AutoAdminSingleton',
),
]

View File

@@ -3,7 +3,6 @@ from __future__ import unicode_literals
from pytz import common_timezones
from django.conf import settings
from django.contrib.auth.models import User
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _, ugettext
@@ -32,15 +31,6 @@ class AnonymousUserSingleton(SingletonModel):
verbose_name = verbose_name_plural = _('Anonymous user')
class AutoAdminSingleton(SingletonModel):
account = models.ForeignKey(User, null=True, blank=True, related_name='auto_admin_account', verbose_name=_('Account'))
password = models.CharField(null=True, blank=True, verbose_name=_('Password'), max_length=128)
password_hash = models.CharField(null=True, blank=True, verbose_name=_('Password hash'), max_length=128)
class Meta:
verbose_name = verbose_name_plural = _('Auto admin properties')
@python_2_unicode_compatible
class SharedUploadedFile(models.Model):
file = models.FileField(upload_to=upload_to, storage=shared_storage_backend, verbose_name=_('File'))
@@ -61,7 +51,7 @@ class SharedUploadedFile(models.Model):
@python_2_unicode_compatible
class UserLocaleProfile(models.Model):
user = models.OneToOneField(User, related_name='locale_profile', verbose_name=_('User'))
user = models.OneToOneField(settings.AUTH_USER_MODEL, related_name='locale_profile', verbose_name=_('User'))
timezone = models.CharField(choices=zip(common_timezones, common_timezones), max_length=48, verbose_name=_('Timezone'))
language = models.CharField(choices=settings.LANGUAGES, max_length=8, verbose_name=_('Language'))

View File

@@ -1,6 +1,5 @@
from __future__ import unicode_literals
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from smart_settings.api import register_setting
@@ -15,30 +14,6 @@ TEMPORARY_DIRECTORY = register_setting(
exists=True
)
register_setting(
namespace='common',
module='common.settings',
name='AUTO_CREATE_ADMIN',
global_name='COMMON_AUTO_CREATE_ADMIN',
default=True,
)
register_setting(
namespace='common',
module='common.settings',
name='AUTO_ADMIN_USERNAME',
global_name='COMMON_AUTO_ADMIN_USERNAME',
default='admin',
)
register_setting(
namespace='common',
module='common.settings',
name='AUTO_ADMIN_PASSWORD',
global_name='COMMON_AUTO_ADMIN_PASSWORD',
default=User.objects.make_random_password(),
)
register_setting(
namespace='common',
module='common.settings',

View File

@@ -53,6 +53,7 @@ INSTALLED_APPS = (
'django.contrib.staticfiles',
# 3rd party
'actstream',
'autoadmin',
'compressor',
'corsheaders',
'djcelery',

View File

@@ -6,6 +6,7 @@ celery==3.1.18
cssmin==0.2.0
django-activity-stream==0.5.1
django-autoadmin==1.0.1
django-celery==3.1.16
django-compressor==1.5
django-cors-headers==1.1.0
@@ -14,7 +15,6 @@ django-pagination==1.0.7
django-model-utils==2.2
django-mptt==0.7.4
django-rest-swagger==0.2.0
django-solo==1.1.0
django-suit==0.2.13
django-widget-tweaks==1.3
djangorestframework==2.4.4