From 64af0b162f768748fe2e9612f5f1fb1f43a523fc Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Sun, 16 Jun 2013 12:00:57 +0200 Subject: [PATCH] Create admin-user after syncdb --- openslides/main.py | 21 +----- openslides/participant/api.py | 15 ++++ openslides/participant/signals.py | 12 ++- .../utils/management/commands/syncdb.py | 9 ++- tests/agenda/test_list_of_speakers.py | 2 +- tests/agenda/tests.py | 8 +- tests/assignment/test_views.py | 2 +- tests/mediafile/tests.py | 8 +- tests/motion/test_views.py | 2 +- tests/participant/test_models.py | 63 +++++++++++++++- tests/participant/test_views.py | 24 +++--- tests/test_participant.py | 73 ------------------- 12 files changed, 115 insertions(+), 124 deletions(-) delete mode 100644 tests/test_participant.py diff --git a/openslides/main.py b/openslides/main.py index 71f0dbdf9..41db39a2b 100644 --- a/openslides/main.py +++ b/openslides/main.py @@ -182,11 +182,10 @@ def _main(opts, database_path=None): # Create Database if necessary if not database_exists() or opts.syncdb: run_syncdb() - create_or_reset_admin_user() # Reset Admin elif opts.reset_admin: - create_or_reset_admin_user() + reset_admin_user() if opts.backupdb: backup_database(opts.backupdb) @@ -294,23 +293,11 @@ def run_syncdb(): execute_from_command_line(argv) -def create_or_reset_admin_user(): +def reset_admin_user(): # can't be imported in global scope as it already requires # the settings module during import - from openslides.participant.models import User - try: - admin = User.objects.get(username="admin") - print("Password for user admin was reset to 'admin'") - except User.DoesNotExist: - admin = User() - admin.username = 'admin' - admin.last_name = 'Administrator' - print("Created default admin user") - - admin.is_superuser = True - admin.default_password = 'admin' - admin.set_password(admin.default_password) - admin.save() + from openslides.participant.api import create_or_reset_admin_user + create_or_reset_admin_user() def backup_database(dest_path): diff --git a/openslides/participant/api.py b/openslides/participant/api.py index 303cbbec3..9534fb26d 100644 --- a/openslides/participant/api.py +++ b/openslides/participant/api.py @@ -113,3 +113,18 @@ def get_registered_group(): Returns the group 'Registered' (pk=2). """ return Group.objects.get(pk=2) + + +def create_or_reset_admin_user(): + group_staff = Group.objects.get(pk=4) + try: + admin = User.objects.get(username="admin") + except User.DoesNotExist: + admin = User() + admin.username = 'admin' + admin.last_name = 'Administrator' + + admin.default_password = 'admin' + admin.set_password(admin.default_password) + admin.save() + admin.groups.add(group_staff) diff --git a/openslides/participant/signals.py b/openslides/participant/signals.py index b504b04dd..7e792d0b4 100644 --- a/openslides/participant/signals.py +++ b/openslides/participant/signals.py @@ -21,6 +21,7 @@ from openslides.config.signals import config_signal from openslides.config.api import ConfigVariable, ConfigPage from .models import Group +from .api import create_or_reset_admin_user @receiver(config_signal, dispatch_uid='setup_participant_config_page') @@ -62,10 +63,14 @@ def setup_participant_config_page(sender, **kwargs): participant_sort_users_by_first_name)) -@receiver(post_database_setup, dispatch_uid='participant_create_builtin_groups') -def create_builtin_groups(sender, **kwargs): +@receiver(post_database_setup, dispatch_uid='participant_create_builtin_groups_and_admin') +def create_builtin_groups_and_admin(sender, **kwargs): """ + Creates the buildin groups and the admin user. + Creates the builtin groups: Anonymous, Registered, Delegates and Staff. + + Creates the builtin user: admin. """ # Check whether the group pks 1 to 4 are free if Group.objects.filter(pk__in=range(1, 5)).exists(): @@ -123,3 +128,6 @@ def create_builtin_groups(sender, **kwargs): group_staff = Group.objects.create(name=ugettext_noop('Staff'), pk=4) group_staff.permissions.add(perm_7, perm_9, perm_10, perm_10a, perm_11, perm_12, perm_13, perm_14, perm_15, perm_15a, perm_16) + + # Admin user + create_or_reset_admin_user() diff --git a/openslides/utils/management/commands/syncdb.py b/openslides/utils/management/commands/syncdb.py index 48c9ce0ab..dd876c5b8 100644 --- a/openslides/utils/management/commands/syncdb.py +++ b/openslides/utils/management/commands/syncdb.py @@ -24,11 +24,12 @@ class Command(_Command): Calls Django's syncdb command but always in non-interactive mode. After this it sends our post_database_setup signal. """ - interactive = kwargs.pop('interactive') - return_value = super(Command, self).handle_noargs(*args, interactive=False, **kwargs) + interactive = kwargs.get('interactive', False) + kwargs['interactive'] = False + return_value = super(Command, self).handle_noargs(*args, **kwargs) post_database_setup.send(sender=self) + if interactive: print('Interactive mode (e. g. creating a superuser) is not possibile ' - 'via this command. To create a superuser use the --reset-admin ' - 'option of the main script.') + 'in OpenSlides. A superuser is automaticly created.') return return_value diff --git a/tests/agenda/test_list_of_speakers.py b/tests/agenda/test_list_of_speakers.py index a1dd79aaa..f9ddc35b3 100644 --- a/tests/agenda/test_list_of_speakers.py +++ b/tests/agenda/test_list_of_speakers.py @@ -78,7 +78,7 @@ class ListOfSpeakerModelTests(TestCase): class SpeakerViewTestCase(TestCase): def setUp(self): # Admin - self.admin = User.objects.create_superuser('admin', 'admin@admin.admin', 'admin') + self.admin = User.objects.get(pk=1) self.admin_client = Client() self.admin_client.login(username='admin', password='admin') diff --git a/tests/agenda/tests.py b/tests/agenda/tests.py index 5092e6fb0..0a9d2bb7b 100644 --- a/tests/agenda/tests.py +++ b/tests/agenda/tests.py @@ -101,14 +101,10 @@ class ViewTest(TestCase): self.item2 = Item.objects.create(title='item2') self.refreshItems() - self.admin, created = User.objects.get_or_create(username='testadmin') + self.admin = User.objects.get(pk=1) self.anonym, created = User.objects.get_or_create(username='testanonym') - self.admin.reset_password('default') self.anonym.reset_password('default') - self.admin.is_superuser = True - self.admin.save() - def refreshItems(self): self.item1 = Item.objects.get(pk=self.item1.id) self.item2 = Item.objects.get(pk=self.item2.id) @@ -116,7 +112,7 @@ class ViewTest(TestCase): @property def adminClient(self): c = Client() - c.login(username='testadmin', password='default') + c.login(username='admin', password='admin') return c @property diff --git a/tests/assignment/test_views.py b/tests/assignment/test_views.py index 70b7d13fb..cad0d0f7e 100644 --- a/tests/assignment/test_views.py +++ b/tests/assignment/test_views.py @@ -19,7 +19,7 @@ from openslides.participant.models import User, Group class AssignmentViewTestCase(TestCase): def setUp(self): # Admin - self.admin = User.objects.create_superuser('admin', 'admin@admin.admin', 'admin') + self.admin = User.objects.get(pk=1) self.admin_client = Client() self.admin_client.login(username='admin', password='admin') diff --git a/tests/mediafile/tests.py b/tests/mediafile/tests.py index 89b84e288..7b8177aac 100644 --- a/tests/mediafile/tests.py +++ b/tests/mediafile/tests.py @@ -36,9 +36,7 @@ class MediafileTest(TestCase): perm_3 = Permission.objects.get(content_type=ct, codename='can_manage') # Setup three different users - self.manager = User.objects.create(username='mediafile_test_manager') - self.manager.reset_password('default') - self.manager.user_permissions.add(perm_1, perm_2, perm_3) + self.manager = User.objects.get(pk=1) self.vip_user = User.objects.create(username='mediafile_test_vip_user') self.vip_user.reset_password('default') self.vip_user.user_permissions.add(perm_1, perm_2) @@ -69,7 +67,7 @@ class MediafileTest(TestCase): Helper function to login all three test users. """ client_manager = Client() - client_manager.login(username='mediafile_test_manager', password='default') + client_manager.login(username='admin', password='admin') client_vip_user = Client() client_vip_user.login(username='mediafile_test_vip_user', password='default') client_normal_user = Client() @@ -91,7 +89,7 @@ class MediafileTest(TestCase): clients = self.login_clients() response = clients['client_manager'].get('/mediafile/new/') self.assertContains(response, '---------', status_code=200) - self.assertContains(response, '', status_code=200) + self.assertContains(response, '', status_code=200) self.assertTemplateUsed(response, 'mediafile/mediafile_form.html') response = clients['client_vip_user'].get('/mediafile/new/') self.assertNotContains(response, '