From 37b4f42afe4fcadf6b1712ed5fab5b3cfcc4d5fb Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Sun, 14 Jun 2015 03:51:36 -0400 Subject: [PATCH] Remove autoadmin code and move it to the external django-autoadmin package --- .../templates/appearance/login.html | 10 ++-- mayan/apps/common/admin.py | 3 +- mayan/apps/common/apps.py | 53 +++---------------- mayan/apps/common/handlers.py | 12 +---- .../management/commands/initialsetup.py | 8 +-- .../migrations/0003_auto_20150614_0723.py | 21 ++++++++ mayan/apps/common/models.py | 12 +---- mayan/apps/common/settings.py | 25 --------- mayan/settings/base.py | 1 + requirements/common.txt | 2 +- 10 files changed, 44 insertions(+), 103 deletions(-) create mode 100644 mayan/apps/common/migrations/0003_auto_20150614_0723.py diff --git a/mayan/apps/appearance/templates/appearance/login.html b/mayan/apps/appearance/templates/appearance/login.html index 694b74066a..42ab6e8364 100644 --- a/mayan/apps/appearance/templates/appearance/login.html +++ b/mayan/apps/appearance/templates/appearance/login.html @@ -11,8 +11,8 @@ {% block content_plain %}
- {% auto_admin_properties %} - {% if auto_admin_properties.account %} + {% autoadmin_properties %} + {% if autoadmin_properties.account %}

{% trans "First time login" %}

@@ -20,9 +20,9 @@

{% trans 'You have just finished installing Mayan EDMS, congratulations!' %}

{% trans 'Login using the following credentials:' %}

-

{% blocktrans with auto_admin_properties.account as account %}Username: {{ account }}{% endblocktrans %}

-

{% blocktrans with auto_admin_properties.account.email as email %}Email: {{ email }}{% endblocktrans %}

-

{% blocktrans with auto_admin_properties.password as password %}Password: {{ password }}{% endblocktrans %}

+

{% blocktrans with autoadmin_properties.account as account %}Username: {{ account }}{% endblocktrans %}

+

{% blocktrans with autoadmin_properties.account.email as email %}Email: {{ email }}{% endblocktrans %}

+

{% blocktrans with autoadmin_properties.password as password %}Password: {{ password }}{% endblocktrans %}

{% trans 'Be sure to change the password to increase security and to disable this message.' %}

diff --git a/mayan/apps/common/admin.py b/mayan/apps/common/admin.py index a5ad7b9106..4347d84e34 100644 --- a/mayan/apps/common/admin.py +++ b/mayan/apps/common/admin.py @@ -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) diff --git a/mayan/apps/common/apps.py b/mayan/apps/common/apps.py index 1543771f24..08b8a5f454 100644 --- a/mayan/apps/common/apps.py +++ b/mayan/apps/common/apps.py @@ -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()) diff --git a/mayan/apps/common/handlers.py b/mayan/apps/common/handlers.py index 94bc9df528..84c576845a 100644 --- a/mayan/apps/common/handlers.py +++ b/mayan/apps/common/handlers.py @@ -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) diff --git a/mayan/apps/common/management/commands/initialsetup.py b/mayan/apps/common/management/commands/initialsetup.py index 95812b797c..ed654d99d7 100644 --- a/mayan/apps/common/management/commands/initialsetup.py +++ b/mayan/apps/common/management/commands/initialsetup.py @@ -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) diff --git a/mayan/apps/common/migrations/0003_auto_20150614_0723.py b/mayan/apps/common/migrations/0003_auto_20150614_0723.py new file mode 100644 index 0000000000..438d705a9f --- /dev/null +++ b/mayan/apps/common/migrations/0003_auto_20150614_0723.py @@ -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', + ), + ] diff --git a/mayan/apps/common/models.py b/mayan/apps/common/models.py index 1300e7e3bc..7fafed0bca 100644 --- a/mayan/apps/common/models.py +++ b/mayan/apps/common/models.py @@ -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')) diff --git a/mayan/apps/common/settings.py b/mayan/apps/common/settings.py index b7ed5a434a..6b962637eb 100644 --- a/mayan/apps/common/settings.py +++ b/mayan/apps/common/settings.py @@ -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', diff --git a/mayan/settings/base.py b/mayan/settings/base.py index bfafacae0b..39a2bcefaa 100644 --- a/mayan/settings/base.py +++ b/mayan/settings/base.py @@ -53,6 +53,7 @@ INSTALLED_APPS = ( 'django.contrib.staticfiles', # 3rd party 'actstream', + 'autoadmin', 'compressor', 'corsheaders', 'djcelery', diff --git a/requirements/common.txt b/requirements/common.txt index c47564e403..6dc91c24b5 100644 --- a/requirements/common.txt +++ b/requirements/common.txt @@ -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