Set the permissions for buro and members

BDS + COF

The permissions assignation is triggered by the `post_migrate` signal since the
permission table will only be populated after the migrations have been applied.
This commit is contained in:
Martin Pépin 2017-06-24 20:57:27 +01:00
parent a1ffb630c0
commit b68590ffd7
6 changed files with 65 additions and 52 deletions

View file

@ -1,6 +1,36 @@
from django.apps import AppConfig
from django.db.models.signals import post_migrate
def setup_groups(sender, apps, **kwargs):
"""
Add the appropriate permissions to the "member" and "buro" groups after the
`post_migrate` signal since the permissions will only be inserted in the
database at the very end of the migrations.
"""
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
# Buro members have perms bds.* and gestion.*
buro, _ = Group.objects.get_or_create(name="bds_buro")
app_perms = Permission.objects.filter(
content_type__app_label__in=["cof", "gestion"]
)
buro.permissions.add(*app_perms)
# Members have perm bds.member
members, _ = Group.objects.get_or_create(name="bds_members")
perm = Permission.objects.get(
codename="member",
content_type__app_label="bds"
)
members.permissions.add(perm)
class BDSConfig(AppConfig):
name = "bds"
verbose_name = "Application de gestion du BDS"
def ready(self):
# https://docs.djangoproject.com/en/1.11/ref/signals/#post-migrate
post_migrate.connect(setup_groups, sender=self)

View file

@ -4,15 +4,6 @@ from __future__ import unicode_literals
from django.db import migrations
def create_groups(apps, schema_editor):
"""
Creates the groups for BDS members and staff
"""
Group = apps.get_model("auth", "Group")
Group.objects.get_or_create(name="bds_members")
Group.objects.get_or_create(name="bds_buro")
class Migration(migrations.Migration):
dependencies = [
@ -31,5 +22,4 @@ class Migration(migrations.Migration):
'verbose_name_plural': 'Profils BDS'
},
),
migrations.RunPython(create_groups, migrations.RunPython.noop),
]

View file

@ -1,6 +1,36 @@
from django.apps import AppConfig
from django.db.models.signals import post_migrate
def setup_groups(sender, apps, **kwargs):
"""
Add the appropriate permissions to the "member" and "buro" groups after the
`post_migrate` signal since the permissions will only be inserted in the
database at the very end of the migrations.
"""
Group = apps.get_model("auth", "Group")
Permission = apps.get_model("auth", "Permission")
# Buro members have perms cof.* and gestion.*
buro, _ = Group.objects.get_or_create(name="cof_buro")
app_perms = Permission.objects.filter(
content_type__app_label__in=["cof", "gestion"]
)
buro.permissions.add(*app_perms)
# Members have perm cof.member
members, _ = Group.objects.get_or_create(name="cof_members")
perm = Permission.objects.get(
codename="member",
content_type__app_label="cof"
)
members.permissions.add(perm)
class COFConfig(AppConfig):
name = "cof"
verbose_name = "Application de gestion du COF"
def ready(self):
# https://docs.djangoproject.com/en/1.11/ref/signals/#post-migrate
post_migrate.connect(setup_groups, sender=self)

View file

@ -23,27 +23,16 @@ def create_profile(apps, schema_editor):
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')
COFProfile = apps.get_model("cof", "CofProfile")
# 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):
cprofiles = COFProfile.objects.select_related("profile__user")
for cofp in cprofiles.filter(is_cof=True):
cofp.profile.user.groups.add(member)
for cofp in CofProfile.objects.filter(is_buro=True):
for cofp in cprofiles.filter(is_buro=True):
cofp.profile.user.groups.add(buro)

View file

@ -1,26 +0,0 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
def create_cof_group(apps, schema_editor):
Group = apps.get_model("auth", "Group")
Group.objects.get_or_create(name="cof_members")
def create_buro_group(apps, schema_editor):
Group = apps.get_model("auth", "Group")
Group.objects.get_or_create(name="cof_buro")
class Migration(migrations.Migration):
dependencies = [
('cof', '0009_generic_profiles'),
]
operations = [
migrations.RunPython(create_cof_group, migrations.RunPython.noop),
migrations.RunPython(create_buro_group, migrations.RunPython.noop),
]

View file

@ -7,7 +7,7 @@ from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('cof', '0010_create_cof_group'),
('cof', '0009_generic_profiles'),
]
operations = [