Create permissions and signals

This commit is contained in:
Ludovic Stephan 2017-07-21 20:24:09 +02:00
parent 6ece994f1c
commit e499281a1d
6 changed files with 51 additions and 3 deletions

View file

@ -16,6 +16,7 @@ class Equipment(EventSpecificMixin, models.Model):
related_name="equipment", related_name="equipment",
through="EquipmentAttribution", through="EquipmentAttribution",
) )
needs_event_permissions = True
class Meta: class Meta:
verbose_name = _("matériel") verbose_name = _("matériel")
@ -30,6 +31,7 @@ class EquipmentAttribution(models.Model):
activity = models.ForeignKey(Activity) activity = models.ForeignKey(Activity)
amount = models.PositiveSmallIntegerField(_("quantité attribuée")) amount = models.PositiveSmallIntegerField(_("quantité attribuée"))
remarks = models.TextField(_("remarques concernant l'attribution")) remarks = models.TextField(_("remarques concernant l'attribution"))
needs_event_permissions = True
class Meta: class Meta:
verbose_name = _("attribution de matériel") verbose_name = _("attribution de matériel")

View file

@ -58,6 +58,7 @@ INSTALLED_APPS = [
'channels', 'channels',
'bootstrapform', 'bootstrapform',
'widget_tweaks', 'widget_tweaks',
'guardian',
] ]
MIDDLEWARE_CLASSES = [ MIDDLEWARE_CLASSES = [
@ -121,6 +122,11 @@ CHANNEL_LAYERS = {
} }
} }
AUTHENTICATION_BACKEND = (
'django.contrib.auth.backends.ModelBackend',
'guardian.backends.ObjectPermissionBackend',
)
# Password validation # Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [ AUTH_PASSWORD_VALIDATORS = [

View file

@ -3,3 +3,6 @@ from django.apps import AppConfig
class EventConfig(AppConfig): class EventConfig(AppConfig):
name = 'event' name = 'event'
def ready(self):
from . import signals

View file

@ -61,6 +61,7 @@ class Place(EventSpecificMixin, models.Model):
max_length=200, max_length=200,
) )
description = models.TextField(blank=True) description = models.TextField(blank=True)
needs_event_permissions = True
class Meta: class Meta:
verbose_name = _("lieu") verbose_name = _("lieu")
@ -92,6 +93,7 @@ class ActivityTag(EventSpecificMixin, models.Model):
validators=[color_regex], validators=[color_regex],
help_text=_("Rentrer une couleur en hexadécimal"), help_text=_("Rentrer une couleur en hexadécimal"),
) )
needs_event_permissions = True
class Meta: class Meta:
verbose_name = _("tag") verbose_name = _("tag")
@ -152,6 +154,8 @@ class AbstractActivityTemplate(SubscriptionMixin, models.Model):
class ActivityTemplate(AbstractActivityTemplate): class ActivityTemplate(AbstractActivityTemplate):
needs_event_permissions = True
class Meta: class Meta:
verbose_name = _("template activité") verbose_name = _("template activité")
verbose_name_plural = _("templates activité") verbose_name_plural = _("templates activité")
@ -173,6 +177,7 @@ class Activity(AbstractActivityTemplate):
beginning = models.DateTimeField(_("heure de début")) beginning = models.DateTimeField(_("heure de début"))
end = models.DateTimeField(_("heure de fin")) end = models.DateTimeField(_("heure de fin"))
needs_event_permissions = True
def get_herited(self, attrname): def get_herited(self, attrname):
inherited_fields = [f.name for f in ActivityTemplate._meta.get_fields()] inherited_fields = [f.name for f in ActivityTemplate._meta.get_fields()]

View file

@ -1,19 +1,50 @@
from django.dispatch import receiver from django.dispatch import receiver
from django.db.models.signals import post_save 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
from event.models import Event from event.models import Event
from shared.models import GEGroup from users.models import GEGroup
from guardian.shortcuts import assign_perm
@receiver(post_save, sender=Event) @receiver(post_save, sender=Event)
def create_groups_for_event(sender, **kwargs): def create_groups_for_event(sender, **kwargs):
event, created = kwargs["instance"], kwargs["created"] event, created = kwargs["instance"], kwargs["created"]
if created: if created:
GEGroup.objects.create( orgas = GEGroup.objects.create(
name="orga", name="orga",
event=event event=event
) )
for perm in Permission.objects.filter(codename__contains="event_"):
assign_perm(perm.codename, orgas, event)
GEGroup.objects.create( GEGroup.objects.create(
name="participants", name="participants",
event=event, event=event,
) )
@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
)

View file

@ -7,6 +7,7 @@ django-bootstrap-form==3.2.1
django-widget-tweaks django-widget-tweaks
django-notifications django-notifications
django-contrib-comments django-contrib-comments
django-guardian
# Production specific # Production specific
daphne daphne