kpsul/cof/apps.py

37 lines
1.2 KiB
Python
Raw Normal View History

2017-02-22 20:00:32 +01:00
from django.apps import AppConfig
from django.db.models.signals import post_migrate
def setup_groups(sender, apps, **kwargs):
"""
Add the appropriate permissions to the "member" and "buro" groups after the
`post_migrate` signal since the permissions will only be inserted in the
database at the very end of the migrations.
"""
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
# Buro members have perms cof.* and gestion.*
buro, _ = Group.objects.get_or_create(name="cof_buro")
app_perms = Permission.objects.filter(
content_type__app_label__in=["cof", "gestion"]
)
buro.permissions.add(*app_perms)
# Members have perm cof.member
members, _ = Group.objects.get_or_create(name="cof_members")
perm = Permission.objects.get(
codename="member",
content_type__app_label="cof"
)
members.permissions.add(perm)
2017-02-22 20:00:32 +01:00
class COFConfig(AppConfig):
name = "cof"
verbose_name = "Application de gestion du COF"
def ready(self):
# https://docs.djangoproject.com/en/1.11/ref/signals/#post-migrate
post_migrate.connect(setup_groups, sender=self)