Make is_cof a property and start developing permissions.

Rmeove the is_cof flag to check permission and start implementing
a group and a permission for the respective cof members.
XXX. note: migrations do NOT add old is_cof members to the new group
(as actually they're just on the tests…)
This commit is contained in:
Michele Orrù 2017-02-11 18:48:13 +01:00
parent a2b8dee022
commit ee1f29b17d
3 changed files with 53 additions and 6 deletions

View file

@ -170,10 +170,11 @@ class UserProfileAdmin(UserAdmin):
list_display = ('profile_num',) + UserAdmin.list_display \
+ ('profile_login_clipper', 'profile_phone', 'profile_occupation',
'profile_mailing_cof', 'profile_mailing_bda',
'profile_mailing_bda_revente', 'is_cof', 'is_buro', )
'profile_mailing_bda_revente', 'is_buro', )
list_display_links = ('username', 'email', 'first_name', 'last_name')
list_filter = UserAdmin.list_filter \
+ ('profile__cof__is_cof',
+ (
# 'profile__cof__is_cof',
'profile__cof__is_buro',
'profile__cof__mailing',
'profile__cof__mailing_bda')

View file

@ -6,7 +6,7 @@ from __future__ import unicode_literals
from django.db import models
from django.dispatch import receiver
from django.contrib.auth.models import User
from django.contrib.auth.models import Group, Permission, User
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import python_2_unicode_compatible
import django.utils.six as six
@ -43,7 +43,6 @@ class CofProfile(models.Model):
mailing = models.BooleanField("Recevoir les mails COF", default=False)
# XXX. remove the following and use django auth.
is_buro = models.BooleanField("Membre du Burô", default=False)
is_cof = models.BooleanField("Membre du COF", default=False)
# XXX. remove the following and put in a BDA profile
mailing_bda = models.BooleanField("Recevoir les mails BdA", default=False)
mailing_bda_revente = models.BooleanField(
@ -55,12 +54,25 @@ class CofProfile(models.Model):
_("Remarques et précisions pour les petits cours"),
blank=True, default="")
# is_cof = models.BooleanField("Membre du COF", default=False)
@property
def is_cof(self):
return self.profile.user.has_perm('cof.member')
@is_cof.setter
def is_cof(self, really):
if really:
g = Group.objects.get(name='cof_members')
self.groups.add(g)
class Meta:
verbose_name = "Profil COF"
verbose_name_plural = "Profils COF"
def __str__(self):
return self.user.username
return self.profile.user.username
@python_2_unicode_compatible

View file

@ -1,3 +1,37 @@
from django.contrib.auth.models import Group, Permission, User
from django.contrib.contenttypes.models import ContentType
from django.test import TestCase
# Create your tests here.
from cof.models import CofProfile
from gestion.tests import create_profile
def create_cofprofile(username):
p = create_profile(username)
return CofProfile.objects.create(profile=p)
class TestCofProfile(TestCase):
def test_str(self):
# creates a group of cof members
group = Group.objects.create(name='cof_members')
# create a specific permission for all COF members.
ct = ContentType.objects.get(app_label='cof', model='CofProfile')
perm = Permission.objects.create(name='Cof Member',
codename='member',
content_type=ct)
# bind the two mutherfucker.
group.permissions = [perm]
# now test it for real
cofp = create_cofprofile('foo')
# XXX. should by default new CofProfiles be COF members?
self.assertFalse(cofp.profile.user.has_perm('cof.member'))
# adding/removing a user from the group should impact the
# permission
cofp.profile.user.groups.add(group)
cofp.save()
cofp = CofProfile.objects.first()
self.assertTrue(cofp.profile.user.has_perm('cof.member'))