Skip to content

Commit 1104a28

Browse files
author
Damian Borowiecki
authored
Merge pull request #66 from openimis/feature/OM-139
OM-139 Implement bill/invoice generic hookable user filter
2 parents 5d3a239 + 898f753 commit 1104a28

File tree

3 files changed

+45
-37
lines changed

3 files changed

+45
-37
lines changed

invoice/apps.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22

33
from django.apps import AppConfig
4+
from core.utils import ConfigUtilMixin
45

56
MODULE_NAME = 'invoice'
67

@@ -10,47 +11,52 @@
1011
"gql_invoice_create_perms": ["155102"],
1112
"gql_invoice_update_perms": ["155103"],
1213
"gql_invoice_delete_perms": ["155104"],
13-
"gql_invoice_amend_perms": ["155109"],
14+
"gql_invoice_amend_perms": ["155109"],
1415

1516
"gql_invoice_payment_search_perms": ["155201"],
1617
"gql_invoice_payment_create_perms": ["155202"],
1718
"gql_invoice_payment_update_perms": ["155203"],
1819
"gql_invoice_payment_delete_perms": ["155204"],
1920
"gql_invoice_payment_refund_perms": ["155206"],
2021

21-
"gql_invoice_event_search_perms": ["155301"],
22-
"gql_invoice_event_create_perms": ["155302"],
23-
"gql_invoice_event_update_perms": ["155303"],
24-
"gql_invoice_event_delete_perms": ["155304"],
25-
"gql_invoice_event_create_message_perms": ["155306"],
26-
"gql_invoice_event_delete_my_message_perms": ["155307"],
22+
"gql_invoice_event_search_perms": ["155301"],
23+
"gql_invoice_event_create_perms": ["155302"],
24+
"gql_invoice_event_update_perms": ["155303"],
25+
"gql_invoice_event_delete_perms": ["155304"],
26+
"gql_invoice_event_create_message_perms": ["155306"],
27+
"gql_invoice_event_delete_my_message_perms": ["155307"],
2728
"gql_invoice_event_delete_all_message_perms": ["155308"],
2829

2930
"gql_bill_search_perms": ["156101"],
3031
"gql_bill_create_perms": ["156102"],
3132
"gql_bill_update_perms": ["156103"],
3233
"gql_bill_delete_perms": ["156104"],
33-
"gql_bill_amend_perms": ["156109"],
34+
"gql_bill_amend_perms": ["156109"],
3435

3536
"gql_bill_payment_search_perms": ["156201"],
3637
"gql_bill_payment_create_perms": ["156202"],
3738
"gql_bill_payment_update_perms": ["156203"],
3839
"gql_bill_payment_delete_perms": ["156204"],
3940
"gql_bill_payment_refund_perms": ["156206"],
4041

41-
"gql_bill_event_search_perms": ["156301"],
42-
"gql_bill_event_create_perms": ["156302"],
43-
"gql_bill_event_update_perms": ["156303"],
44-
"gql_bill_event_delete_perms": ["156304"],
45-
"gql_bill_event_create_message_perms": ["156306"],
46-
"gql_bill_event_delete_my_message_perms": ["156307"],
42+
"gql_bill_event_search_perms": ["156301"],
43+
"gql_bill_event_create_perms": ["156302"],
44+
"gql_bill_event_update_perms": ["156303"],
45+
"gql_bill_event_delete_perms": ["156304"],
46+
"gql_bill_event_create_message_perms": ["156306"],
47+
"gql_bill_event_delete_my_message_perms": ["156307"],
4748
"gql_bill_event_delete_all_message_perms": ["156308"],
49+
50+
# Functions of type Callable[[QuerySet, User], QuerySet], to be used as custom user filters for bills and invoices
51+
# To be specified as "module_name.submodule.function_name"
52+
"bill_user_filter_function": None,
53+
"invoice_user_filter_function": None,
4854
}
4955

5056
logger = logging.getLogger(__name__)
5157

5258

53-
class InvoiceConfig(AppConfig):
59+
class InvoiceConfig(AppConfig, ConfigUtilMixin):
5460
name = MODULE_NAME
5561

5662
default_currency_code = None
@@ -89,12 +95,14 @@ class InvoiceConfig(AppConfig):
8995
gql_bill_event_delete_my_message_perms = None
9096
gql_bill_event_delete_all_message_perms = None
9197

92-
def __load_config(self, cfg):
93-
for field in cfg:
94-
if hasattr(InvoiceConfig, field):
95-
setattr(InvoiceConfig, field, cfg[field])
98+
bill_user_filter = None
99+
invoice_user_filter = None
96100

97101
def ready(self):
98102
from core.models import ModuleConfiguration
99103
cfg = ModuleConfiguration.get_or_default(MODULE_NAME, DEFAULT_CONFIG)
100-
self.__load_config(cfg)
104+
self._load_config_fields(cfg)
105+
if cfg['bill_user_filter_function']:
106+
self._load_config_function('bill_user_filter', cfg['bill_user_filter_function'])
107+
if cfg['invoice_user_filter_function']:
108+
self._load_config_function('invoice_user_filter', cfg['invoice_user_filter_function'])

invoice/gql/bill/query.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
import functools
2-
import logging
3-
import types
4-
51
import graphene
6-
import pandas as pd
72
from django.contrib.auth.models import AnonymousUser
83
from django.contrib.contenttypes.models import ContentType
94
from django.db.models import Q
10-
from django.http import HttpResponse
115
from pandas import DataFrame
126

137
from core.gql.export_mixin import ExportableQueryMixin
@@ -26,14 +20,15 @@ def patch_subjects(bills_df: DataFrame):
2620
bills_df['subject_class'] = 'undefined'
2721
for subject in subject_df['subject_type'].unique():
2822
model = ContentType.objects.get(id=subject).model_class()
29-
entities_for_model = subject_df[subject_df['subject_type']==subject]['subject_id']
23+
entities_for_model = subject_df[subject_df['subject_type'] == subject]['subject_id']
3024
if model == Policy:
31-
subject_names = model.objects\
32-
.filter(id__in=entities_for_model)\
25+
subject_names = model.objects \
26+
.filter(id__in=entities_for_model) \
3327
.values('id', 'family__head_insuree__chf_id',
3428
'family__head_insuree__last_name', 'family__head_insuree__other_names')
3529
names = {
36-
str(x['id']): F"{x['family__head_insuree__other_names']} {x['family__head_insuree__last_name']} ({x['family__head_insuree__chf_id']})" for x in subject_names
30+
str(x['id']): F"{x['family__head_insuree__other_names']} {x['family__head_insuree__last_name']} ({x['family__head_insuree__chf_id']})"
31+
for x in subject_names
3732
}
3833
updated = bills_df['subject_type'] == subject
3934
bills_df.loc[updated, 'subject_id'] = bills_df[updated]['subject_id'].apply(lambda x: names[x])
@@ -60,6 +55,7 @@ class BillQueryMixin(ExportableQueryMixin):
6055
)
6156

6257
def resolve_bill(self, info, **kwargs):
58+
BillQueryMixin._check_permissions(info.context.user)
6359
filters = []
6460
filters += append_validity_filter(**kwargs)
6561

@@ -75,8 +71,11 @@ def resolve_bill(self, info, **kwargs):
7571
if thirdparty_type:
7672
filters.append(Q(thirdparty_type__model=thirdparty_type))
7773

78-
BillQueryMixin._check_permissions(info.context.user)
79-
return gql_optimizer.query(Bill.objects.filter(*filters).all(), info)
74+
qs = Bill.objects.filter(*filters)
75+
if InvoiceConfig.bill_user_filter:
76+
qs = InvoiceConfig.bill_user_filter(qs, info.context.user)
77+
78+
return gql_optimizer.query(qs, info)
8079

8180
@staticmethod
8281
def _check_permissions(user):

invoice/gql/invoice/query.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class InvoiceQueryMixin:
2121
)
2222

2323
def resolve_invoice(self, info, **kwargs):
24+
InvoiceQueryMixin._check_permissions(info.context.user)
2425
filters = []
2526
filters += append_validity_filter(**kwargs)
2627

@@ -36,14 +37,14 @@ def resolve_invoice(self, info, **kwargs):
3637
if thirdparty_type:
3738
filters.append(Q(thirdparty_type__model=thirdparty_type))
3839

39-
InvoiceQueryMixin._check_permissions(info.context.user)
40-
return gql_optimizer.query(Invoice.objects.filter(*filters).all(), info)
40+
qs = Invoice.objects.filter(*filters)
41+
if InvoiceConfig.invoice_user_filter:
42+
qs = InvoiceConfig.invoice_user_filter(qs, info.context.user)
43+
44+
return gql_optimizer.query(qs, info)
4145

4246
@staticmethod
4347
def _check_permissions(user):
4448
if type(user) is AnonymousUser or not user.id or not user.has_perms(
4549
InvoiceConfig.gql_invoice_search_perms):
4650
raise PermissionError("Unauthorized")
47-
48-
49-

0 commit comments

Comments
 (0)