From 37586d7433969d4a06b8818338265463dd8d777e Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Mon, 11 Dec 2023 21:31:54 +0100 Subject: [PATCH] =?UTF-8?q?Opraven=C3=AD=20pr=C3=A1v?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TODO: je na těch ContentTypech navěšené ještě něco dalšího? Pro nastavení asi ne, ale co ostatní aplikace? --- various/migrations/0003_fix_permissions.py | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 various/migrations/0003_fix_permissions.py diff --git a/various/migrations/0003_fix_permissions.py b/various/migrations/0003_fix_permissions.py new file mode 100644 index 00000000..2364f160 --- /dev/null +++ b/various/migrations/0003_fix_permissions.py @@ -0,0 +1,41 @@ +# Generated by Django 3.2.23 on 2023-12-11 19:40 + +from django.db import migrations + +def oprav_prava_k_nastaveni(apps, schema_editor): + # Tohle je trošku hnus, nešlo by to snáz? + ContentType = apps.get_model('contenttypes', 'ContentType') + Permission = apps.get_model('auth', 'Permission') + Group = apps.get_model('auth', 'Group') + old_ct = ContentType.objects.get_by_natural_key('seminar', 'nastaveni') + new_ct = ContentType.objects.get_by_natural_key('various', 'nastaveni') + old_perms = Permission.objects.filter(content_type=old_ct) + new_perms = Permission.objects.filter(content_type=new_ct) + for g in Group.objects.filter(permissions__in=old_perms): + old_codenames = Permission.objects.filter(group__in=[g], content_type=old_ct).values('codename') + g.permissions.add(*new_perms.filter(codename__in=old_codenames)) + g.permissions.remove(*old_perms) + +def obnov_prava_k_nastaveni(apps, schema_editor): + ContentType = apps.get_model('contenttypes', 'ContentType') + Permission = apps.get_model('auth', 'Permission') + Group = apps.get_model('auth', 'Group') + old_ct = ContentType.objects.get_by_natural_key('seminar', 'nastaveni') + new_ct = ContentType.objects.get_by_natural_key('various', 'nastaveni') + old_perms = Permission.objects.filter(content_type=old_ct) + new_perms = Permission.objects.filter(content_type=new_ct) + for g in Group.objects.filter(permissions__in=old_perms): + new_codenames = Permission.objects.filter(group__in=[g], content_type=new_ct).values('codename') + g.permissions.add(*old_perms.filter(codename__in=new_codenames)) + g.permissions.remove(*new_perms) + + +class Migration(migrations.Migration): + + dependencies = [ + ('various', '0002_alter_nastaveni_options'), + ] + + operations = [ + migrations.RunPython(oprav_prava_k_nastaveni, obnov_prava_k_nastaveni), + ]