From cd3c4709199d3bee7079a46d6d4d628d206ab2b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Norman=20J=C3=A4ckel?= Date: Thu, 8 Sep 2016 11:40:58 +0200 Subject: [PATCH] Changed migration coding style. --- openslides/users/exceptions.py | 5 -- openslides/users/migrations/0004_groups.py | 62 ++++++++++++---------- openslides/users/signals.py | 2 +- 3 files changed, 35 insertions(+), 34 deletions(-) delete mode 100644 openslides/users/exceptions.py diff --git a/openslides/users/exceptions.py b/openslides/users/exceptions.py deleted file mode 100644 index 185aa383d..000000000 --- a/openslides/users/exceptions.py +++ /dev/null @@ -1,5 +0,0 @@ -from ..utils.exceptions import OpenSlidesError - - -class UsersError(OpenSlidesError): - pass diff --git a/openslides/users/migrations/0004_groups.py b/openslides/users/migrations/0004_groups.py index 99fefe4ec..262526b0d 100644 --- a/openslides/users/migrations/0004_groups.py +++ b/openslides/users/migrations/0004_groups.py @@ -2,47 +2,53 @@ # Generated by Django 1.9.7 on 2016-08-01 14:54 from __future__ import unicode_literals -from django.core.exceptions import ObjectDoesNotExist -from django.db import migrations +from django.db import migrations, models def migrate_groups_and_user_permissions(apps, schema_editor): """ This function migrates the database to the new groups logic: - - Rename Group 'Anonymous' (pk=1) to 'Default' - - Rename Group 'Registered users' (pk=2) to 'Previous group Registered' + - Rename group 'Anonymous' (pk=1) to 'Default' + - Rename group 'Registered users' (pk=2) to 'Previous group Registered' - Add all users who are not in any group to this group (pk=2) - - Add all permissions of 'Previous group Registered' to all other groups (exclude 'Default') + - Add all permissions of 'Previous group Registered' to all other groups (except 'Default') - But only do this migration if: - - there are groups in the database - - the name of the first group is 'Guests'. + But only run this migration if: + - there are groups in the database, + - the name of the first group is 'Guests', + - the name of the second group is 'Registered users'. """ + # Disconnect autoupdate. We do not want to trigger it here. + models.signals.post_save.disconnect(dispatch_uid='inform_changed_data_receiver') + User = apps.get_model('users', 'User') Group = apps.get_model('auth', 'Group') - if Group.objects.exists(): - try: - group_default = Group.objects.filter(pk=1) - if group_default.get().name == 'Guests': - group_default.update(name='Default') + try: + group_default = Group.objects.get(pk=1) + group_registered = Group.objects.get(pk=2) + except Group.DoesNotExist: + # One of the groups does not exist. Just do nothing. + pass + else: + if group_default.name == 'Guests' and group_registered.name == 'Registered users': + # Rename groups pk 1 and 2. + group_default.name = 'Default' + group_default.save() + group_registered.name = 'Previous group Registered' + group_registered.save() - group_old_registered = Group.objects.filter(pk=2) - group_old_registered.update(name='Previous group Registered') - group_old_registered = group_old_registered.get() + # Move users without groups to group pk 2. + users = User.objects.all() + for user in users: + if not user.groups.exists(): + user.groups.add(group_registered) - users = User.objects.all() - for user in users: - if not user.groups.exists(): - user.groups.add(group_old_registered.pk) - - groups = Group.objects.filter(pk__gt=2) - for group in groups: - for permission in group_old_registered.permissions.all(): - group.permissions.add(permission) - except ObjectDoesNotExist: - # If the first or second group doesn't exists, just pass this migraition - pass + # Copy permissions of group pk 2 to all other groups except pk 1. + groups = Group.objects.filter(pk__gt=2) + for group in groups: + for permission in group_registered.permissions.all(): + group.permissions.add(permission) class Migration(migrations.Migration): diff --git a/openslides/users/signals.py b/openslides/users/signals.py index 9700f718c..8d6437764 100644 --- a/openslides/users/signals.py +++ b/openslides/users/signals.py @@ -12,7 +12,7 @@ def create_builtin_groups_and_admin(**kwargs): """ # Check whether there are groups in the database. if Group.objects.exists(): - # Do completely nothing if there are already some of our groups in the database. + # Do completely nothing if there are already some groups in the database. return permission_strings = (