Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OM-70: add migration with modal role #12

Merged
merged 5 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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),
]
Loading