From e53ccc4e374c17d4437cceae00bfb06c0858cda7 Mon Sep 17 00:00:00 2001 From: Mandel Olaiya Date: Sat, 9 May 2026 17:02:35 +0200 Subject: [PATCH] Grant ContactMessage perms to staff users --- ...ant_contactmessage_permissions_to_staff.py | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 mandelstudio/migrations/0006_grant_contactmessage_permissions_to_staff.py diff --git a/mandelstudio/migrations/0006_grant_contactmessage_permissions_to_staff.py b/mandelstudio/migrations/0006_grant_contactmessage_permissions_to_staff.py new file mode 100644 index 0000000..ea1a559 --- /dev/null +++ b/mandelstudio/migrations/0006_grant_contactmessage_permissions_to_staff.py @@ -0,0 +1,43 @@ +from django.db import migrations + + +def grant_contactmessage_permissions_to_staff(apps, schema_editor): + Permission = apps.get_model("auth", "Permission") + ContentType = apps.get_model("contenttypes", "ContentType") + + # Default Django user model in this project. + User = apps.get_model("auth", "User") + + content_type = ContentType.objects.get(app_label="mandelstudio", model="contactmessage") + perms = list( + Permission.objects.filter( + content_type=content_type, + codename__in=[ + "add_contactmessage", + "change_contactmessage", + "delete_contactmessage", + "view_contactmessage", + ], + ) + ) + if not perms: + return + + for user in User.objects.filter(is_staff=True, is_active=True).iterator(): + user.user_permissions.add(*perms) + + +class Migration(migrations.Migration): + dependencies = [ + ("mandelstudio", "0005_grant_contactmessage_permissions"), + ("contenttypes", "0002_remove_content_type_name"), + ("auth", "0012_alter_user_first_name_max_length"), + ] + + operations = [ + migrations.RunPython( + grant_contactmessage_permissions_to_staff, + reverse_code=migrations.RunPython.noop, + ), + ] +