diff --git a/src/fellchensammlung/apps.py b/src/fellchensammlung/apps.py index 128faa4..2996d50 100644 --- a/src/fellchensammlung/apps.py +++ b/src/fellchensammlung/apps.py @@ -1,6 +1,10 @@ from django.apps import AppConfig - +from .tools.signals import ensure_groups +from django.db.models.signals import post_migrate class FellchensammlungConfig(AppConfig): default_auto_field = 'django.db.models.BigAutoField' name = 'fellchensammlung' + + def ready(self): + post_migrate.connect(ensure_groups, sender=self) diff --git a/src/fellchensammlung/migrations/0001_initial.py b/src/fellchensammlung/migrations/0001_initial.py index 767a4e0..fc78115 100644 --- a/src/fellchensammlung/migrations/0001_initial.py +++ b/src/fellchensammlung/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.3 on 2024-04-07 09:32 +# Generated by Django 5.0.3 on 2024-04-12 20:02 import datetime import django.contrib.auth.models @@ -118,6 +118,9 @@ class Migration(migrations.Migration): ('group_only', models.BooleanField(default=False, verbose_name='Only group adoption')), ('photos', models.ManyToManyField(blank=True, to='fellchensammlung.image')), ], + options={ + 'permissions': [('create_active_adoption_notice', 'Can create an active adoption notice')], + }, ), migrations.CreateModel( name='Member', @@ -142,6 +145,9 @@ class Migration(migrations.Migration): ('adoption_notice', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='fellchensammlung.adoptionnotice')), ('reported_broken_rules', models.ManyToManyField(blank=True, to='fellchensammlung.rule')), ], + options={ + 'permissions': [], + }, ), migrations.CreateModel( name='ModerationAction', diff --git a/src/fellchensammlung/models.py b/src/fellchensammlung/models.py index 07c0d64..1e9b63d 100644 --- a/src/fellchensammlung/models.py +++ b/src/fellchensammlung/models.py @@ -9,8 +9,6 @@ from django.db.models.signals import post_save from django.contrib.auth.models import Group from django.contrib.auth.models import AbstractUser - - from fellchensammlung.tools import misc @@ -70,6 +68,11 @@ class RescueOrganization(models.Model): class AdoptionNotice(models.Model): + class Meta: + permissions = [ + ("create_active_adoption_notice", "Can create an active adoption notice"), + ] + def __str__(self): return f"{self.name}" @@ -205,6 +208,9 @@ class Rule(models.Model): class Report(models.Model): + class Meta: + permissions = [] + ACTION_TAKEN = "action taken" NO_ACTION_TAKEN = "no action taken" WAITING = "waiting" diff --git a/src/fellchensammlung/tools/signals.py b/src/fellchensammlung/tools/signals.py new file mode 100644 index 0000000..055d227 --- /dev/null +++ b/src/fellchensammlung/tools/signals.py @@ -0,0 +1,29 @@ +def _get_perms(permission_name_list): + from django.contrib.auth.models import Permission + permissions = [] + for permission_name in permission_name_list: + print(f"{permission_name}") + permission = Permission.objects.get(codename=permission_name) + permissions.append(permission) + return permissions + + +def ensure_groups(sender, **kwargs): + from django.contrib.auth.models import Group + + member_permissions = [] + coordinator_permissions = ["create_active_adoption_notice"] + moderator_permissions = coordinator_permissions + ["view_report", "add_moderationaction", "change_user"] + admin_permissions = moderator_permissions + + admins, created = Group.objects.get_or_create(name="Admins") + admins.permissions.set(_get_perms(admin_permissions)) + + moderators, created = Group.objects.get_or_create(name="Moderators") + moderators.permissions.set(_get_perms(moderator_permissions)) + + coordinators, created = Group.objects.get_or_create(name="Coordinators") + coordinators.permissions.set(_get_perms(coordinator_permissions)) + + members, created = Group.objects.get_or_create(name="Members") + members.permissions.set(_get_perms(member_permissions))