poulpe/event/signals.py

35 lines
1.1 KiB
Python
Raw Normal View History

2017-07-21 15:48:40 +02:00
from django.dispatch import receiver
2017-09-19 10:46:26 +02:00
from django.db.models.signals import post_save
2017-07-21 20:24:09 +02:00
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
2017-07-21 20:24:09 +02:00
from guardian.shortcuts import assign_perm
2017-07-21 15:48:40 +02:00
@receiver(post_save, sender=Event)
def create_groups_for_event(sender, **kwargs):
event, created = kwargs["instance"], kwargs["created"]
if created:
orgas = EventGroup.objects.create(
2017-08-17 15:10:46 +02:00
name="{}_orgas".format(event.slug),
2017-07-21 15:48:40 +02:00
event=event
)
2017-09-19 10:46:26 +02:00
# 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)
2017-07-21 20:24:09 +02:00
EventGroup.objects.create(
2017-08-17 15:10:46 +02:00
name="{}_participants".format(event.slug),
2017-07-21 15:48:40 +02:00
event=event,
)