From 392e5cf14477a2ecb484e34f8c2261f2421f1d08 Mon Sep 17 00:00:00 2001 From: Ludovic Stephan Date: Tue, 19 Sep 2017 11:48:39 +0200 Subject: [PATCH] Revert c894d35 --- Fuck django-guardian This reverts commit c894d359324eab0c6fab607d6c4afe74fbc23dc2. --- event/migrations/0003_auto_20170919_0845.py | 35 ------------------ event/models.py | 5 --- event/signals.py | 41 +++++++++++++++------ event/tests.py | 39 ++++++++------------ 4 files changed, 44 insertions(+), 76 deletions(-) delete mode 100644 event/migrations/0003_auto_20170919_0845.py diff --git a/event/migrations/0003_auto_20170919_0845.py b/event/migrations/0003_auto_20170919_0845.py deleted file mode 100644 index 69b9a20..0000000 --- a/event/migrations/0003_auto_20170919_0845.py +++ /dev/null @@ -1,35 +0,0 @@ -# -*- 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'}, - ), - ] diff --git a/event/models.py b/event/models.py index 76af063..c328688 100644 --- a/event/models.py +++ b/event/models.py @@ -47,7 +47,6 @@ 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 @@ -87,7 +86,6 @@ 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 @@ -115,7 +113,6 @@ 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 @@ -181,7 +178,6 @@ 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 @@ -229,7 +225,6 @@ 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') diff --git a/event/signals.py b/event/signals.py index e601e9a..edac92b 100644 --- a/event/signals.py +++ b/event/signals.py @@ -1,5 +1,5 @@ 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 @@ -16,19 +16,36 @@ 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(model) - ): - print(perm.codename) - assign_perm(perm.codename, orgas, event) + for perm in Permission.objects.filter( + content_type=ContentType.objects.get_for_model(Event), + codename__contains="event_"): + 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 + ) diff --git a/event/tests.py b/event/tests.py index b6eaa5c..cac64c8 100644 --- a/event/tests.py +++ b/event/tests.py @@ -160,6 +160,10 @@ 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", @@ -174,21 +178,6 @@ 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 @@ -196,18 +185,20 @@ class EventPermissionTest(TestCase): self.assertEqual(groups.count(), 2) def test_individual_perms(self): - 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)) + 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)) def test_group_perms(self): orgas = EventGroup.objects.get( name="{}_orgas".format(self.event.slug), ) self.user_groupperm.groups.add(orgas) - for perm in self.perms: - self.assertTrue(self.user_perm.has_perm(perm, - self.event)) - self.assertFalse(self.user_noperm.has_perm(perm, - self.event)) + 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, + self.event)) + self.assertFalse(self.user_groupnoperm.has_perm(perm.codename, + self.event))