Skip to content

Commit

Permalink
fix migration
Browse files Browse the repository at this point in the history
  • Loading branch information
delcroip committed Sep 5, 2024
1 parent 04c109f commit ea2d12f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -11,46 +10,49 @@
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,
validity_to__isnull=True
).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):
Expand Down

0 comments on commit ea2d12f

Please sign in to comment.