Scrap useless perms

This commit is contained in:
Ludovic Stephan 2017-09-19 10:46:26 +02:00
parent 0b1641a002
commit c894d35932
4 changed files with 76 additions and 44 deletions

View file

@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-09-19 08:45
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('event', '0002_auto_20170817_1221'),
]
operations = [
migrations.AlterModelOptions(
name='activity',
options={'default_permissions': ('add', 'delete', 'change', 'view'), 'verbose_name': 'activité', 'verbose_name_plural': 'activités'},
),
migrations.AlterModelOptions(
name='activitytag',
options={'default_permissions': ('add', 'delete', 'change', 'view'), 'verbose_name': 'tag', 'verbose_name_plural': 'tags'},
),
migrations.AlterModelOptions(
name='activitytemplate',
options={'default_permissions': ('add', 'delete', 'change', 'view'), 'verbose_name': 'template activité', 'verbose_name_plural': 'templates activité'},
),
migrations.AlterModelOptions(
name='event',
options={'default_permissions': ('add', 'delete', 'change', 'view'), 'verbose_name': 'évènement', 'verbose_name_plural': 'évènements'},
),
migrations.AlterModelOptions(
name='place',
options={'default_permissions': ('add', 'delete', 'change', 'view'), 'verbose_name': 'lieu', 'verbose_name_plural': 'lieux'},
),
]

View file

@ -47,6 +47,7 @@ class Event(SubscriptionMixin, models.Model):
class Meta:
verbose_name = _("évènement")
verbose_name_plural = _("évènements")
default_permissions = ('add', 'delete', 'change', 'view')
def __str__(self):
return self.title
@ -86,6 +87,7 @@ class Place(EventSpecificMixin, models.Model):
class Meta:
verbose_name = _("lieu")
verbose_name_plural = _("lieux")
default_permissions = ('add', 'delete', 'change', 'view')
def __str__(self):
return self.name
@ -113,6 +115,7 @@ class ActivityTag(EventSpecificMixin, models.Model):
class Meta:
verbose_name = _("tag")
verbose_name_plural = _("tags")
default_permissions = ('add', 'delete', 'change', 'view')
def __str__(self):
return self.name
@ -178,6 +181,7 @@ class ActivityTemplate(AbstractActivityTemplate):
class Meta:
verbose_name = _("template activité")
verbose_name_plural = _("templates activité")
default_permissions = ('add', 'delete', 'change', 'view')
def __str__(self):
return self.title
@ -225,6 +229,7 @@ class Activity(AbstractActivityTemplate):
class Meta:
verbose_name = _("activité")
verbose_name_plural = _("activités")
default_permissions = ('add', 'delete', 'change', 'view')
def __str__(self):
return self.get_herited('title')

View file

@ -1,5 +1,5 @@
from django.dispatch import receiver
from django.db.models.signals import post_save, post_migrate
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
@ -16,36 +16,19 @@ def create_groups_for_event(sender, **kwargs):
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(Event),
codename__contains="event_"):
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,
)
@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

@ -160,10 +160,6 @@ class ActivityTagColorTest(TestCase):
class EventPermissionTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user_perm = User.objects.create(username="userperm")
cls.user_noperm = User.objects.create(username="usernoperm")
cls.user_groupperm = User.objects.create(username="usergroupperm")
cls.user_groupnoperm = User.objects.create(username="usergroupnoperm")
cls.root = User.objects.create_superuser(
username="root",
email="toto@toto.io",
@ -178,6 +174,21 @@ class EventPermissionTest(TestCase):
ending_date=timezone.now() + timedelta(days=1),
)
models = [model for model in apps.get_models()
if getattr(model, 'needs_event_permissions', False)]
cls.perms = []
for model in models:
for perm in Permission.objects.filter(
content_type=ContentType.objects.get_for_model(model)
):
cls.perms.append(perm.codename)
def setUp(self):
self.user_perm = User.objects.create(username="userperm")
self.user_noperm = User.objects.create(username="usernoperm")
def test_event_groups(self):
groups = EventGroup.objects.filter(
event=self.event
@ -185,20 +196,18 @@ class EventPermissionTest(TestCase):
self.assertEqual(groups.count(), 2)
def test_individual_perms(self):
assign_perm("event_add_place", self.user_perm, self.event)
self.assertTrue(self.user_perm.has_perm("event_add_place", self.event))
self.assertFalse(self.user_noperm.has_perm("event_add_place",
self.event))
for perm in self.perms:
assign_perm(perm, self.user_perm, self.event)
self.assertTrue(self.user_perm.has_perm(perm, self.event))
self.assertFalse(self.user_noperm.has_perm(perm, self.event))
def test_group_perms(self):
orgas = EventGroup.objects.get(
name="{}_orgas".format(self.event.slug),
)
self.user_groupperm.groups.add(orgas)
for perm in Permission.objects.filter(
content_type=ContentType.objects.get_for_model(Event),
codename__contains="event_"):
self.assertTrue(self.user_groupperm.has_perm(perm.codename,
for perm in self.perms:
self.assertTrue(self.user_perm.has_perm(perm,
self.event))
self.assertFalse(self.user_groupnoperm.has_perm(perm.codename,
self.assertFalse(self.user_noperm.has_perm(perm,
self.event))