poulpe/event/signals.py
2017-09-19 10:46:26 +02:00

34 lines
1.1 KiB
Python

from django.dispatch import receiver
from django.db.models.signals import post_save
from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
from event.models import Event, EventGroup
from guardian.shortcuts import assign_perm
@receiver(post_save, sender=Event)
def create_groups_for_event(sender, **kwargs):
event, created = kwargs["instance"], kwargs["created"]
if created:
orgas = EventGroup.objects.create(
name="{}_orgas".format(event.slug),
event=event
)
# Models having event-specific permissions
models = [model for model in apps.get_models()
if getattr(model, 'needs_event_permissions', False)]
for model in models:
for perm in Permission.objects.filter(
content_type=ContentType.objects.get_for_model(model)
):
print(perm.codename)
assign_perm(perm.codename, orgas, event)
EventGroup.objects.create(
name="{}_participants".format(event.slug),
event=event,
)