poulpe/event/signals.py

51 lines
1.5 KiB
Python
Raw Normal View History

2017-07-21 15:48:40 +02:00
from django.dispatch import receiver
2017-07-21 20:24:09 +02:00
from django.db.models.signals import post_save, post_migrate
from django.apps import apps
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
2017-07-21 15:48:40 +02:00
from event.models import Event
2017-07-21 20:24:09 +02:00
from users.models import GEGroup
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-07-21 20:24:09 +02:00
orgas = GEGroup.objects.create(
2017-07-21 15:48:40 +02:00
name="orga",
event=event
)
2017-07-21 20:24:09 +02:00
for perm in Permission.objects.filter(codename__contains="event_"):
assign_perm(perm.codename, orgas, event)
2017-07-21 15:48:40 +02:00
GEGroup.objects.create(
name="participants",
event=event,
)
2017-07-21 20:24:09 +02:00
@receiver(post_migrate)
def create_event_permissions(sender, **kwargs):
def event_specific_permissions():
opes = ['Add', 'Change', 'Delete']
models = [model.__name__.lower() for model in apps.get_models()
if getattr(model, 'needs_event_permissions', False)]
return [
('event_{}_{}'.format(op.lower(), model),
'{} {} for event'.format(op, model))
for op in opes
for model in models
]
content_type = ContentType.objects.get_for_model(Event)
for (code, verbose) in event_specific_permissions():
Permission.objects.get_or_create(
name=verbose,
content_type=content_type,
codename=code
)