Skip to content

Commit

Permalink
OM-70: get models using get_model
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan committed Nov 6, 2023
1 parent cbf94d7 commit 62fd636
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions msystems/migrations/0003_add_search_policyholders_perms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,42 @@

from django.db import migrations

from core.models import Role, RoleRight

POLICY_HOLDER_SEARCH_PERM = [150101, 150201, 150301]
ROLE_NAME_INSPECTOR = "Inspector"
ROLE_NAME_EMPLOYER = "Employer"


def add_rights(role_name):
role = Role.objects.get(name=role_name)
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 RoleRight.objects.filter(validity_to__isnull=True, role=role, right_id=right_id).exists():
_add_right_for_role(role, right_id)
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):
RoleRight.objects.create(role=role, right_id=right_id, audit_user_id=1)
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):
RoleRight.objects.filter(
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):
add_rights(ROLE_NAME_INSPECTOR)
add_rights(ROLE_NAME_EMPLOYER)
role_model = apps.get_model("core", "role")
role_right_model = apps.get_model("core", "roleright")
add_rights(ROLE_NAME_INSPECTOR, role_model, role_right_model)
add_rights(ROLE_NAME_EMPLOYER, role_model, role_right_model)


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


class Migration(migrations.Migration):
Expand Down

0 comments on commit 62fd636

Please sign in to comment.