Files
ACORE/apps/employees/migrations/0004_create_role_groups.py
T
lokses 27a917e8c3 Replace role field with auth.Group membership
- Remove Employee.role CharField — was a display-only label with no
  actual permission logic
- 3 migrations: create groups, migrate data, remove field
- Admin: list/filter/fieldset now use groups; added groups_display method
- Serializer: role → groups (SlugRelatedField returning group names)
- Template: get_role_display → groups.first.name
- EmployeePermissionOverride and PermissionGroup remain for fine-grained
  per-employee and per-group permission management
2026-05-31 15:04:31 +03:00

45 lines
1.3 KiB
Python

"""
Data migration: создаём auth.Group + PermissionGroup для каждой роли.
"""
from django.db import migrations
ROLE_GROUPS = [
('admin', 'Администратор сети'),
('warehouse', 'Сотрудник склада'),
('pharmacist', 'Фармацевт'),
('manager', 'Заведующий аптекой'),
('revisor', 'Ревизор'),
('office', 'Офисный сотрудник'),
]
def create_groups(apps, schema_editor):
Group = apps.get_model('auth', 'Group')
PermissionGroup = apps.get_model('employees', 'PermissionGroup')
for code, name in ROLE_GROUPS:
group, _ = Group.objects.get_or_create(name=name)
PermissionGroup.objects.get_or_create(
group=group,
defaults={'description': f'Группа для роли: {name}', 'is_active': True},
)
def delete_groups(apps, schema_editor):
Group = apps.get_model('auth', 'Group')
names = [name for _, name in ROLE_GROUPS]
Group.objects.filter(name__in=names).delete()
class Migration(migrations.Migration):
dependencies = [
('employees', '0003_employee_user_location'),
('auth', '0012_alter_user_first_name_max_length'),
]
operations = [
migrations.RunPython(create_groups, delete_groups),
]