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
|
2017-08-17 14:23:13 +02:00
|
|
|
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:
|
2017-08-17 14:23:13 +02:00
|
|
|
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
|
|
|
|
2017-08-17 14:23:13 +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,
|
|
|
|
)
|