From ea2d12fd5c338ad731ac8091cf9fca0c93d43298 Mon Sep 17 00:00:00 2001 From: delcroip Date: Thu, 5 Sep 2024 20:41:44 +0200 Subject: [PATCH] fix migration --- .../0002_add_benefit_plan_rights_to_admin.py | 19 +++-------- .../0004_add_beneficiary_rights_to_admin.py | 8 +++-- .../0009_add_schema_admin_and_schema_roles.py | 32 ++++++++++--------- 3 files changed, 26 insertions(+), 33 deletions(-) diff --git a/social_protection/migrations/0002_add_benefit_plan_rights_to_admin.py b/social_protection/migrations/0002_add_benefit_plan_rights_to_admin.py index d60e723..0ca5efe 100644 --- a/social_protection/migrations/0002_add_benefit_plan_rights_to_admin.py +++ b/social_protection/migrations/0002_add_benefit_plan_rights_to_admin.py @@ -1,29 +1,18 @@ from django.db import migrations - -from core.models import Role, RoleRight +from core.utils import insert_role_right_for_system, remove_role_right_for_system benefit_plan_rights = [160001, 160002, 160003, 160004] imis_administrator_system = 64 def add_rights(apps, schema_editor): - role = Role.objects.get(is_system=imis_administrator_system) for right_id in benefit_plan_rights: - if not RoleRight.objects.filter(validity_to__isnull=True, role=role, right_id=right_id).exists(): - _add_right_for_role(role, right_id) - - -def _add_right_for_role(role, right_id): - RoleRight.objects.create(role=role, right_id=right_id, audit_user_id=1) + insert_role_right_for_system(imis_administrator_system, right_id, apps) def remove_rights(apps, schema_editor): - RoleRight.objects.filter( - role__is_system=imis_administrator_system, - right_id__in=benefit_plan_rights, - validity_to__isnull=True - ).delete() - + for right_id in benefit_plan_rights: + remove_role_right_for_system(imis_administrator_system, right_id, apps) class Migration(migrations.Migration): dependencies = [ diff --git a/social_protection/migrations/0004_add_beneficiary_rights_to_admin.py b/social_protection/migrations/0004_add_beneficiary_rights_to_admin.py index a44fbc0..dcdc2d9 100644 --- a/social_protection/migrations/0004_add_beneficiary_rights_to_admin.py +++ b/social_protection/migrations/0004_add_beneficiary_rights_to_admin.py @@ -2,24 +2,26 @@ from django.db import migrations -from core.models import Role, RoleRight benefit_plan_rights = [170001, 170002, 170003, 170004] imis_administrator_system = 64 def add_rights(apps, schema_editor): + RoleRight = apps.get_model('core', 'RoleRight') + Role = apps.get_model('core', 'Role') role = Role.objects.get(is_system=imis_administrator_system) for right_id in benefit_plan_rights: if not RoleRight.objects.filter(validity_to__isnull=True, role=role, right_id=right_id).exists(): - _add_right_for_role(role, right_id) + _add_right_for_role(role, right_id, RoleRight) -def _add_right_for_role(role, right_id): +def _add_right_for_role(role, right_id, RoleRight): RoleRight.objects.create(role=role, right_id=right_id, audit_user_id=1) def remove_rights(apps, schema_editor): + RoleRight = apps.get_model('core', 'RoleRight') RoleRight.objects.filter( role__is_system=imis_administrator_system, right_id__in=benefit_plan_rights, diff --git a/social_protection/migrations/0009_add_schema_admin_and_schema_roles.py b/social_protection/migrations/0009_add_schema_admin_and_schema_roles.py index 773dbf8..dc83559 100644 --- a/social_protection/migrations/0009_add_schema_admin_and_schema_roles.py +++ b/social_protection/migrations/0009_add_schema_admin_and_schema_roles.py @@ -2,7 +2,6 @@ from django.db import migrations -from core.models import Role, RoleRight from core.utils import insert_role_right_for_system SCHEMA_ADMIN_ID = 4194304 @@ -11,16 +10,16 @@ SCHEMA_ADMIN_ROLE_RIGHTS = ["171001", "171002", "171003", "171004"] -def _get_role(role_id): +def _get_role(role_id, Role): return Role.objects.filter(is_system=role_id).first() -def _add_rights_to_role(role): +def _add_rights_to_role(role, apps): for right in SCHEMA_ADMIN_ROLE_RIGHTS: - insert_role_right_for_system(role, right) + insert_role_right_for_system(role, right, apps) -def _remove_rights_from_role(role): +def _remove_rights_from_role(role, RoleRight): RoleRight.objects.filter( role__is_system=role, right_id__in=SCHEMA_ADMIN_ROLE_RIGHTS, @@ -28,29 +27,32 @@ def _remove_rights_from_role(role): ).delete() -def _create_task_triage_role(): - role = _get_role(SCHEMA_ADMIN_ID) +def _create_task_triage_role(Role): + role = _get_role(SCHEMA_ADMIN_ID, Role) if not role: task_triage = Role(is_system=SCHEMA_ADMIN_ID, name=ROLE_NAME, is_blocked=False) task_triage.save() -def _delete_task_triage_role(): - role = _get_role(SCHEMA_ADMIN_ID) +def _delete_task_triage_role(Role): + role = _get_role(SCHEMA_ADMIN_ID, Role) if role: role.delete() def on_migration(apps, schema_editor): - _create_task_triage_role() - _add_rights_to_role(IMIS_ADMIN) - _add_rights_to_role(SCHEMA_ADMIN_ID) + Role = apps.get_model('core', 'Role') + _create_task_triage_role(Role) + _add_rights_to_role(IMIS_ADMIN, apps) + _add_rights_to_role(SCHEMA_ADMIN_ID, apps) def on_migration_reverse(apps, schema_editor): - _remove_rights_from_role(IMIS_ADMIN) - _remove_rights_from_role(SCHEMA_ADMIN_ID) - _delete_task_triage_role() + RoleRight = apps.get_model('core', 'RoleRight') + Role = apps.get_model('core', 'Role') + _remove_rights_from_role(IMIS_ADMIN, RoleRight) + _remove_rights_from_role(SCHEMA_ADMIN_ID, RoleRight) + _delete_task_triage_role(Role) class Migration(migrations.Migration):