ae54d93442
Section-based portal access, matrix прав по группам, аккаунтинг-портал, управление группами пользователей, справочники, дашборд и общий каркас навигации/стилей. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
"""Права на разделы «Управление»: аптеки, РРО, коммунальные услуги."""
|
|
from django.db import migrations
|
|
|
|
NEW_PERMS = [
|
|
('access_pharmacies', 'Доступ к разделу: Аптеки'),
|
|
('access_rro', 'Доступ к разделу: РРО / ПРРО'),
|
|
('access_utilities', 'Доступ к разделу: Коммунальные услуги'),
|
|
]
|
|
|
|
GROUP_ADDITIONS = {
|
|
'IT': ['access_pharmacies', 'access_rro', 'access_utilities'],
|
|
'Офисный сотрудник': ['access_pharmacies', 'access_utilities'],
|
|
'Заведующий аптекой': ['access_pharmacies', 'access_rro', 'access_utilities'],
|
|
'Ревизор': ['access_pharmacies', 'access_rro'],
|
|
'Фармацевт': ['access_utilities'],
|
|
}
|
|
|
|
|
|
def forwards(apps, schema_editor):
|
|
Group = apps.get_model('auth', 'Group')
|
|
Permission = apps.get_model('auth', 'Permission')
|
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
|
|
|
ct, _ = ContentType.objects.get_or_create(app_label='core', model='portalaccess')
|
|
perms_by_code = {}
|
|
for codename, name in NEW_PERMS:
|
|
perm, _ = Permission.objects.get_or_create(
|
|
content_type=ct, codename=codename,
|
|
defaults={'name': name},
|
|
)
|
|
if perm.name != name:
|
|
perm.name = name
|
|
perm.save(update_fields=['name'])
|
|
perms_by_code[codename] = perm
|
|
|
|
admin_group = Group.objects.filter(name='Администратор сети').first()
|
|
if admin_group:
|
|
admin_group.permissions.add(*perms_by_code.values())
|
|
|
|
for group_name, codes in GROUP_ADDITIONS.items():
|
|
group = Group.objects.filter(name=group_name).first()
|
|
if not group:
|
|
continue
|
|
to_add = [perms_by_code[c] for c in codes if c in perms_by_code]
|
|
if to_add:
|
|
group.permissions.add(*to_add)
|
|
|
|
|
|
def backwards(apps, schema_editor):
|
|
Group = apps.get_model('auth', 'Group')
|
|
Permission = apps.get_model('auth', 'Permission')
|
|
ContentType = apps.get_model('contenttypes', 'ContentType')
|
|
|
|
ct = ContentType.objects.filter(app_label='core', model='portalaccess').first()
|
|
if ct is None:
|
|
return
|
|
codes = [c for c, _ in NEW_PERMS]
|
|
perms = list(Permission.objects.filter(content_type=ct, codename__in=codes))
|
|
if not perms:
|
|
return
|
|
for group in Group.objects.all():
|
|
group.permissions.remove(*perms)
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('core', '0011_ensure_portal_section_perms'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(forwards, backwards),
|
|
]
|