From 94937fc7cdbf47b85ea241f28a61d4a0e6945ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michele=20Orr=C3=B9?= Date: Sat, 11 Feb 2017 23:05:51 +0100 Subject: [PATCH] Add groups cof_members and cof_buro. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - remove is_buro from the database in the same way we did for is_cof - make a decent migration that *SHOULD* take into account is_cof for old databases. note, this has been tested only through unittests. - make unittests pass again accordin=gly fixing views. Note: we should make a method for filtering with specific group members, something like map(lambda x: x.profile.cof, filter… group…) --- cof/admin.py | 6 +-- cof/decorators.py | 30 +++---------- cof/forms.py | 2 +- cof/migrations/0009_generic_profiles.py | 60 ++++++++++++++++++++++--- cof/models.py | 29 +++++++----- cof/tests.py | 15 +------ cof/views.py | 8 ++-- 7 files changed, 88 insertions(+), 62 deletions(-) diff --git a/cof/admin.py b/cof/admin.py index fe531853..8af035b8 100644 --- a/cof/admin.py +++ b/cof/admin.py @@ -170,12 +170,12 @@ 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_buro', ) + 'profile_mailing_bda_revente', ) list_display_links = ('username', 'email', 'first_name', 'last_name') list_filter = UserAdmin.list_filter \ + ( - # 'profile__cof__is_cof', - 'profile__cof__is_buro', + # 'profile__cof__is_cof', + # 'profile__cof__is_buro', 'profile__cof__mailing', 'profile__cof__mailing_bda') search_fields = UserAdmin.search_fields + ('profile__phone',) diff --git a/cof/decorators.py b/cof/decorators.py index d7e70608..df6559b0 100644 --- a/cof/decorators.py +++ b/cof/decorators.py @@ -1,29 +1,9 @@ # -*- coding: utf-8 -*- -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals +from django.contrib.auth.decorators import permission_required -from django_cas_ng.decorators import user_passes_test +cof_required = permission_required('cof.member') +cof_required_customdenied = permission_required('cof.member', + login_url="cof-denied") - -def is_cof(user): - try: - profile = user.profile - return profile.is_cof - except: - return False - -cof_required = user_passes_test(lambda u: is_cof(u)) -cof_required_customdenied = user_passes_test(lambda u: is_cof(u), - login_url="cof-denied") - - -def is_buro(user): - try: - profile = user.profile - return profile.is_buro - except: - return False - -buro_required = user_passes_test(lambda u: is_buro(u)) +buro_required = permission_required('cof.buro') diff --git a/cof/forms.py b/cof/forms.py index cf6652c7..344595c6 100644 --- a/cof/forms.py +++ b/cof/forms.py @@ -226,7 +226,7 @@ class RegistrationCofProfileForm(forms.ModelForm): class Meta: model = CofProfile fields = [ - "num", "type_cotiz", "is_cof", "is_buro", + "num", "type_cotiz", "mailing", "mailing_bda", "mailing_bda_revente", "petits_cours_accept", "petits_cours_remarques" ] diff --git a/cof/migrations/0009_generic_profiles.py b/cof/migrations/0009_generic_profiles.py index c3fb1c13..8583f792 100644 --- a/cof/migrations/0009_generic_profiles.py +++ b/cof/migrations/0009_generic_profiles.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals +from django.contrib.auth.models import Group, Permission +from django.contrib.contenttypes.models import ContentType from django.db import migrations, models @@ -21,9 +23,37 @@ def create_profile(apps, schema_editor): p.save() +def preserve_perms(apps, schema_editor): + from django.contrib.auth.management import create_permissions + + apps.models_module = True + create_permissions(apps, verbosity=0) + apps.models_module = None + + CofProfile = apps.get_model("cof", "CofProfile") + memberp = Permission.objects.get(codename='member') + burop = Permission.objects.get(codename='buro') + + # creates the groups for COF members and + member = Group.objects.create(name='cof_members') + buro = Group.objects.create(name='cof_buro') + + # associate permissions to the respective groups. + buro.permissions = [burop, memberp] + member.permissions = [memberp] + + + for cofp in CofProfile.objects.filter(is_cof=True): + cofp.profile.user.groups.add(member) + for cofp in CofProfile.objects.filter(is_buro=True): + cofp.profile.user.groups.add(buro) + + def remove_profile(apps, schema_editor): raise NotImplementedError +fuckup_perms = remove_profile + class Migration(migrations.Migration): @@ -48,7 +78,24 @@ class Migration(migrations.Migration): ), preserve_default=False, ), + migrations.AlterModelOptions( + name='cofprofile', + options={ + 'permissions': (('member', 'Is a COF member'), + ('buro', 'Is part of COF staff') + ), + 'verbose_name': 'Profil COF', + 'verbose_name_plural': 'Profils COF'}, + ), migrations.RunPython(create_profile, remove_profile), + migrations.AlterField( + model_name='cofprofile', + name='profile', + field=models.OneToOneField( + to='gestion.Profile', + related_name='cof' + ), + ), migrations.RemoveField( model_name='cofprofile', name='comments', @@ -69,13 +116,14 @@ class Migration(migrations.Migration): model_name='cofprofile', name='phone', ), - migrations.AlterField( + migrations.RunPython(preserve_perms, fuckup_perms), + migrations.RemoveField( model_name='cofprofile', - name='profile', - field=models.OneToOneField( - to='gestion.Profile', - related_name='cof' - ), + name='is_cof', + ), + migrations.RemoveField( + model_name='cofprofile', + name='is_buro', ), migrations.RemoveField( model_name='cofprofile', diff --git a/cof/models.py b/cof/models.py index d05fec3a..2ff705fb 100644 --- a/cof/models.py +++ b/cof/models.py @@ -1,9 +1,4 @@ # -*- coding: utf-8 -*- - -from __future__ import division -from __future__ import print_function -from __future__ import unicode_literals - from django.db import models from django.dispatch import receiver from django.contrib.auth.models import Group, Permission, User @@ -41,8 +36,6 @@ class CofProfile(models.Model): max_length=choices_length( TYPE_COTIZ_CHOICES)) 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) # 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( @@ -54,6 +47,14 @@ class CofProfile(models.Model): _("Remarques et précisions pour les petits cours"), blank=True, default="") + class Meta: + verbose_name = "Profil COF" + verbose_name_plural = "Profils COF" + permissions = ( + ('member', 'Is a COF member'), + ('buro', 'Is part of COF staff'), + ) + # is_cof = models.BooleanField("Membre du COF", default=False) @property @@ -66,10 +67,18 @@ class CofProfile(models.Model): g = Group.objects.get(name='cof_members') self.groups.add(g) + # XXX. remove the following and use django auth. + #is_buro = models.BooleanField("Membre du Burô", default=False) + @property + def is_buro(self): + return self.profile.user.has_perm('cof.buro') + + @is_buro.setter + def is_buro(self, really): + if really: + g = Group.objects.get(name='cof_buro') + self.groups.add(g) - class Meta: - verbose_name = "Profil COF" - verbose_name_plural = "Profils COF" def __str__(self): return self.profile.user.username diff --git a/cof/tests.py b/cof/tests.py index 15106223..823b7aec 100644 --- a/cof/tests.py +++ b/cof/tests.py @@ -6,30 +6,19 @@ 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 + group = Group.objects.get(name='cof_members') cofp.profile.user.groups.add(group) cofp.save() diff --git a/cof/views.py b/cof/views.py index 8c5398c2..f876cf28 100644 --- a/cof/views.py +++ b/cof/views.py @@ -545,7 +545,7 @@ def export_members(request): response['Content-Disposition'] = 'attachment; filename=membres_cof.csv' writer = unicodecsv.writer(response) - for profile in CofProfile.objects.filter(is_cof=True).all(): + for profile in CofProfile.objects.filter(profile__user__groups__name='cof_members').all(): user = profile.user bits = [profile.num, user.username, user.first_name, user.last_name, user.email, profile.phone, profile.occupation, @@ -654,7 +654,7 @@ def utile_bda(request): @buro_required def liste_bdadiff(request): titre = "BdA diffusion" - personnes = CofProfile.objects.filter(mailing_bda=True, is_cof=True).all() + personnes = CofProfile.objects.filter(mailing_bda=True, profile__user__groups__name='cof_members').all() return render(request, "liste_mails.html", {"titre": titre, "personnes": personnes}) @@ -663,7 +663,7 @@ def liste_bdadiff(request): def liste_bdarevente(request): titre = "BdA revente" personnes = CofProfile.objects.filter(mailing_bda_revente=True, - is_cof=True).all() + profile__user__groups__name='cof_members').all() return render(request, "liste_mails.html", {"titre": titre, "personnes": personnes}) @@ -671,7 +671,7 @@ def liste_bdarevente(request): @buro_required def liste_diffcof(request): titre = "Diffusion COF" - personnes = CofProfile.objects.filter(mailing_cof=True, is_cof=True).all() + personnes = CofProfile.objects.filter(mailing_cof=True, user__groups__name='cof_members').all() return render(request, "liste_mails.html", {"titre": titre, "personnes": personnes})