From b081401d57d1866d8cca95452355c9e322895288 Mon Sep 17 00:00:00 2001 From: FinnStutzenstein Date: Thu, 8 Mar 2018 16:52:05 +0100 Subject: [PATCH] Adding new permission to groups during migration from 2.1.1 to 2.2 --- .../migrations/0003_auto_20170818_1202.py | 6 ++++ .../migrations/0005_auto_20170412_1258.py | 6 ++++ openslides/utils/migrations.py | 36 +++++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 openslides/utils/migrations.py diff --git a/openslides/agenda/migrations/0003_auto_20170818_1202.py b/openslides/agenda/migrations/0003_auto_20170818_1202.py index 719058023..30a872214 100644 --- a/openslides/agenda/migrations/0003_auto_20170818_1202.py +++ b/openslides/agenda/migrations/0003_auto_20170818_1202.py @@ -4,6 +4,9 @@ from __future__ import unicode_literals from django.db import migrations +from openslides.utils.migrations import \ + add_permission_to_groups_based_on_existing_permission + class Migration(migrations.Migration): @@ -24,4 +27,7 @@ class Migration(migrations.Migration): ) }, ), + migrations.RunPython(add_permission_to_groups_based_on_existing_permission( + 'can_manage', 'item', 'agenda', 'can_manage_list_of_speakers', 'Can manage list of speakers' + )), ] diff --git a/openslides/core/migrations/0005_auto_20170412_1258.py b/openslides/core/migrations/0005_auto_20170412_1258.py index 18e58bb0d..68871ce08 100644 --- a/openslides/core/migrations/0005_auto_20170412_1258.py +++ b/openslides/core/migrations/0005_auto_20170412_1258.py @@ -4,6 +4,9 @@ from __future__ import unicode_literals from django.db import migrations +from openslides.utils.migrations import \ + add_permission_to_groups_based_on_existing_permission + class Migration(migrations.Migration): @@ -22,4 +25,7 @@ class Migration(migrations.Migration): ) }, ), + migrations.RunPython(add_permission_to_groups_based_on_existing_permission( + 'can_manage_config', 'configstore', 'core', 'can_manage_logos', 'Can manage logos' + )), ] diff --git a/openslides/utils/migrations.py b/openslides/utils/migrations.py new file mode 100644 index 000000000..81854463b --- /dev/null +++ b/openslides/utils/migrations.py @@ -0,0 +1,36 @@ +from django.contrib.auth.models import Permission +from django.contrib.contenttypes.models import ContentType + + +def add_permission_to_groups_based_on_existing_permission(codename, model, app_label, new_codename, new_name): + """ + Creates the new permission given by new_codename and new_name to all groups, + that have the base permission. This base permission is given by codename, model + and app_label. The new permission will have the same content type as the base + permission. The migration just runs, if the base permission and content type do + exist, so this does not run for a fresh database. + """ + + def function(apps, schema_editor): + content_type = ContentType.objects.filter(model=model, app_label=app_label) + base_perm = Permission.objects.filter(codename=codename, content_type=content_type) + + if len(base_perm) is 1 and len(content_type) is 1: + # get the actual content type and base permission + base_perm = base_perm.get() + content_type = content_type.get() + + # Save groups. list() is necessary to evaluate the database query right now. + groups = list(base_perm.group_set.all()) + + # Create new permission + perm = Permission.objects.create( + codename=new_codename, + name=new_name, + content_type=content_type) + + # Add this permission to all groups + for group in groups: + group.permissions.add(perm) + group.save() + return function