57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
"""
|
|
Agrega el permiso notificaciones.receive al catálogo y lo asigna a todos los
|
|
OrganizationRole que correspondan a los 5 roles por defecto (en todas las orgs).
|
|
"""
|
|
from django.db import migrations
|
|
|
|
|
|
NUEVO_PERMISO = (
|
|
'notificaciones.receive',
|
|
'Recibir notificaciones automáticas de eventos',
|
|
'notificaciones',
|
|
)
|
|
|
|
# Todos los roles por defecto deben recibir notificaciones
|
|
ROLES_CON_PERMISO = ['admin', 'developer', 'Agente Aduanal', 'user', 'Importador']
|
|
|
|
|
|
def agregar_notificaciones_receive(apps, schema_editor):
|
|
RolePermission = apps.get_model('rbac', 'RolePermission')
|
|
OrganizationRole = apps.get_model('rbac', 'OrganizationRole')
|
|
|
|
codename, descripcion, modulo = NUEVO_PERMISO
|
|
perm, _ = RolePermission.objects.get_or_create(
|
|
codename=codename,
|
|
defaults={'descripcion': descripcion, 'modulo': modulo},
|
|
)
|
|
|
|
roles = OrganizationRole.objects.filter(nombre__in=ROLES_CON_PERMISO)
|
|
for role in roles:
|
|
role.permissions.add(perm)
|
|
|
|
|
|
def revertir(apps, schema_editor):
|
|
RolePermission = apps.get_model('rbac', 'RolePermission')
|
|
OrganizationRole = apps.get_model('rbac', 'OrganizationRole')
|
|
|
|
try:
|
|
perm = RolePermission.objects.get(codename='notificaciones.receive')
|
|
except RolePermission.DoesNotExist:
|
|
return
|
|
|
|
for role in OrganizationRole.objects.all():
|
|
role.permissions.remove(perm)
|
|
|
|
perm.delete()
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
dependencies = [
|
|
('rbac', '0002_data_permissions'),
|
|
]
|
|
|
|
operations = [
|
|
migrations.RunPython(agregar_notificaciones_receive, reverse_code=revertir),
|
|
]
|