097ee44131
…migrations already applied from master.
81 lines
2.4 KiB
Python
81 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
from django.db import migrations
|
|
from django.db.models import Q
|
|
|
|
|
|
def convert_manage_perms(apps, schema_editor):
|
|
"""
|
|
Use the permissions of `kfet.auth.models.Group` model, instead of using the
|
|
`manage_perms` permission, which is deleted, of `kfet.models.Account`.
|
|
|
|
`Group` which have this last permission will get the permissions:
|
|
`kfetauth.view_group`, `kfetauth.add_group` and `kfetauth.change_group`.
|
|
"""
|
|
Group = apps.get_model('auth', 'Group')
|
|
Permission = apps.get_model('auth', 'Permission')
|
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
|
|
|
try:
|
|
old_p = Permission.objects.get(
|
|
content_type__app_label='kfet',
|
|
content_type__model='account',
|
|
codename='manage_perms',
|
|
)
|
|
except Permission.DoesNotExist:
|
|
return
|
|
|
|
groups = old_p.group_set.all()
|
|
|
|
ct_group, _ = ContentType.objects.get_or_create(
|
|
app_label='kfetauth',
|
|
model='group',
|
|
)
|
|
|
|
view_p, _ = Permission.objects.get_or_create(
|
|
content_type=ct_group, codename='view_group',
|
|
defaults={'name': 'Can view Groupe'})
|
|
add_p, _ = Permission.objects.get_or_create(
|
|
content_type=ct_group, codename='add_group',
|
|
defaults={'name': 'Can add Groupe'})
|
|
change_p, _ = Permission.objects.get_or_create(
|
|
content_type=ct_group, codename='change_group',
|
|
defaults={'name': 'Can change Group'})
|
|
|
|
GroupPermission = Group.permissions.through
|
|
|
|
GroupPermission.objects.bulk_create([
|
|
GroupPermission(permission=p, group=g)
|
|
for g in groups
|
|
for p in (view_p, add_p, change_p)
|
|
])
|
|
|
|
old_p.delete()
|
|
|
|
|
|
def delete_unused_permissions(apps, schema_editor):
|
|
Permission = apps.get_model('auth', 'Permission')
|
|
|
|
to_delete_q = Q(content_type__model='genericteamtoken') | Q(
|
|
content_type__model='group',
|
|
codename='delete_group',
|
|
)
|
|
|
|
to_delete_q &= Q(content_type__app_label='kfetauth')
|
|
|
|
Permission.objects.filter(to_delete_q).delete()
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
"""
|
|
Data migration about permissions.
|
|
"""
|
|
dependencies = [
|
|
('kfetauth', '0003_existing_groups'),
|
|
('auth', '0006_require_contenttypes_0002'),
|
|
('contenttypes', '0002_remove_content_type_name'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(convert_manage_perms),
|
|
migrations.RunPython(delete_unused_permissions),
|
|
]
|