Skip to content

Commit

Permalink
OM-70: add migration with modal role (#12)
Browse files Browse the repository at this point in the history
* OM-70: add additional roles

* OM-70: get models using get_model

* OM-70: add migration that adds modal view right

* OM-70: add operation in migrations

---------

Co-authored-by: Jan <[email protected]>
  • Loading branch information
jdolkowski and Jan authored Nov 6, 2023
1 parent b59ae37 commit f18a902
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions msystems/migrations/0003_add_search_policyholders_perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(on_migration, on_reverse_migration),
]
47 changes: 47 additions & 0 deletions msystems/migrations/0004_add_modal_right.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated by Django 3.2.21 on 2023-11-06 09:29

from django.db import migrations

POLICY_HOLDER_SEARCH_PERM = [203000]
ROLE_NAME_EMPLOYER = "Employer"


def add_rights(role_name, role_model, role_right_model):
role = role_model.objects.get(name=role_name)
for right_id in POLICY_HOLDER_SEARCH_PERM:
if not role_right_model.objects.filter(validity_to__isnull=True, role=role, right_id=right_id).exists():
_add_right_for_role(role, right_id, role_right_model)


def _add_right_for_role(role, right_id, role_right_model):
role_right_model.objects.create(role=role, right_id=right_id, audit_user_id=1)


def remove_rights(role_id, role_right_model):
role_right_model.objects.filter(
role__is_system=role_id,
right_id__in=POLICY_HOLDER_SEARCH_PERM,
validity_to__isnull=True
).delete()


def on_migration(apps, schema_editor):
role_model = apps.get_model("core", "role")
role_right_model = apps.get_model("core", "roleright")
add_rights(ROLE_NAME_EMPLOYER, role_model, role_right_model)


def on_reverse_migration(apps, schema_editor):
role_right_model = apps.get_model("core", "roleright")
remove_rights(ROLE_NAME_EMPLOYER, role_right_model)


class Migration(migrations.Migration):

dependencies = [
('msystems', '0003_add_search_policyholders_perms'),
]

operations = [
migrations.RunPython(on_migration, on_reverse_migration),
]

0 comments on commit f18a902

Please sign in to comment.