feat: Ensure correct groups are creeated with permissions

This commit is contained in:
moanos [he/him] 2024-04-12 22:23:49 +02:00
parent 1a2878330e
commit 4767fb4e76
4 changed files with 49 additions and 4 deletions

View File

@ -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)

View File

@ -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',

View File

@ -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"

View File

@ -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))