Revert c894d35 --- Fuck django-guardian

This reverts commit c894d35932.
This commit is contained in:
Ludovic Stephan 2017-09-19 11:48:39 +02:00
parent c894d35932
commit 392e5cf144
4 changed files with 44 additions and 76 deletions

View file

@ -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'},
),
]

View file

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

View file

@ -1,5 +1,5 @@
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.apps import apps
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission from django.contrib.auth.models import Permission
@ -16,19 +16,36 @@ def create_groups_for_event(sender, **kwargs):
event=event event=event
) )
# Models having event-specific permissions for perm in Permission.objects.filter(
models = [model for model in apps.get_models() content_type=ContentType.objects.get_for_model(Event),
if getattr(model, 'needs_event_permissions', False)] codename__contains="event_"):
assign_perm(perm.codename, orgas, event)
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)
EventGroup.objects.create( EventGroup.objects.create(
name="{}_participants".format(event.slug), name="{}_participants".format(event.slug),
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

@ -160,6 +160,10 @@ class ActivityTagColorTest(TestCase):
class EventPermissionTest(TestCase): class EventPermissionTest(TestCase):
@classmethod @classmethod
def setUpTestData(cls): 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( cls.root = User.objects.create_superuser(
username="root", username="root",
email="toto@toto.io", email="toto@toto.io",
@ -174,21 +178,6 @@ class EventPermissionTest(TestCase):
ending_date=timezone.now() + timedelta(days=1), 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): def test_event_groups(self):
groups = EventGroup.objects.filter( groups = EventGroup.objects.filter(
event=self.event event=self.event
@ -196,18 +185,20 @@ class EventPermissionTest(TestCase):
self.assertEqual(groups.count(), 2) self.assertEqual(groups.count(), 2)
def test_individual_perms(self): def test_individual_perms(self):
for perm in self.perms: assign_perm("event_add_place", self.user_perm, self.event)
assign_perm(perm, self.user_perm, self.event) self.assertTrue(self.user_perm.has_perm("event_add_place", self.event))
self.assertTrue(self.user_perm.has_perm(perm, self.event)) self.assertFalse(self.user_noperm.has_perm("event_add_place",
self.assertFalse(self.user_noperm.has_perm(perm, self.event)) self.event))
def test_group_perms(self): def test_group_perms(self):
orgas = EventGroup.objects.get( orgas = EventGroup.objects.get(
name="{}_orgas".format(self.event.slug), name="{}_orgas".format(self.event.slug),
) )
self.user_groupperm.groups.add(orgas) self.user_groupperm.groups.add(orgas)
for perm in self.perms: for perm in Permission.objects.filter(
self.assertTrue(self.user_perm.has_perm(perm, content_type=ContentType.objects.get_for_model(Event),
self.event)) codename__contains="event_"):
self.assertFalse(self.user_noperm.has_perm(perm, self.assertTrue(self.user_groupperm.has_perm(perm.codename,
self.event)) self.event))
self.assertFalse(self.user_groupnoperm.has_perm(perm.codename,
self.event))