forked from DGNum/gestioCOF
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:
parent
a1ffb630c0
commit
b68590ffd7
6 changed files with 65 additions and 52 deletions
30
bds/apps.py
30
bds/apps.py
|
@ -1,6 +1,36 @@
|
||||||
from django.apps import AppConfig
|
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):
|
class BDSConfig(AppConfig):
|
||||||
name = "bds"
|
name = "bds"
|
||||||
verbose_name = "Application de gestion du 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)
|
||||||
|
|
|
@ -4,15 +4,6 @@ from __future__ import unicode_literals
|
||||||
from django.db import migrations
|
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):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
@ -31,5 +22,4 @@ class Migration(migrations.Migration):
|
||||||
'verbose_name_plural': 'Profils BDS'
|
'verbose_name_plural': 'Profils BDS'
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
migrations.RunPython(create_groups, migrations.RunPython.noop),
|
|
||||||
]
|
]
|
||||||
|
|
30
cof/apps.py
30
cof/apps.py
|
@ -1,6 +1,36 @@
|
||||||
from django.apps import AppConfig
|
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):
|
class COFConfig(AppConfig):
|
||||||
name = "cof"
|
name = "cof"
|
||||||
verbose_name = "Application de gestion du 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)
|
||||||
|
|
|
@ -23,27 +23,16 @@ def create_profile(apps, schema_editor):
|
||||||
|
|
||||||
|
|
||||||
def preserve_perms(apps, schema_editor):
|
def preserve_perms(apps, schema_editor):
|
||||||
# from django.contrib.auth.management import create_permissions
|
COFProfile = apps.get_model("cof", "CofProfile")
|
||||||
|
|
||||||
# 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
|
# creates the groups for COF members and
|
||||||
member = Group.objects.create(name='cof_members')
|
member = Group.objects.create(name='cof_members')
|
||||||
buro = Group.objects.create(name='cof_buro')
|
buro = Group.objects.create(name='cof_buro')
|
||||||
|
|
||||||
# associate permissions to the respective groups.
|
cprofiles = COFProfile.objects.select_related("profile__user")
|
||||||
# buro.permissions = [burop, memberp]
|
for cofp in cprofiles.filter(is_cof=True):
|
||||||
# member.permissions = [memberp]
|
|
||||||
|
|
||||||
for cofp in CofProfile.objects.filter(is_cof=True):
|
|
||||||
cofp.profile.user.groups.add(member)
|
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)
|
cofp.profile.user.groups.add(buro)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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),
|
|
||||||
]
|
|
|
@ -7,7 +7,7 @@ from django.db import migrations, models
|
||||||
class Migration(migrations.Migration):
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
dependencies = [
|
dependencies = [
|
||||||
('cof', '0010_create_cof_group'),
|
('cof', '0009_generic_profiles'),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
|
|
Loading…
Reference in a new issue