diff --git a/bifrost/bifrost_queue_broker/common/database.py b/bifrost/bifrost_queue_broker/common/database.py index d3c9e525..d2d5d4fe 100644 --- a/bifrost/bifrost_queue_broker/common/database.py +++ b/bifrost/bifrost_queue_broker/common/database.py @@ -135,20 +135,22 @@ def get_collection(collection: str): def recursive_replace(data, replacement_fn, filter_list=None, filtered_parent=False): # If no filter_list is provided, then assume all leaf nodes in tree must be replaced - do_filter = not filter_list or filtered_parent + do_filter = not filter_list or filtered_parent if isinstance(data, (dict, list)): for k, v in data.items() if isinstance(data, dict) else enumerate(data): # If a key in the filter_list is seen at any node in the tree, leaf values # underneath that node must be replaced if k in filter_list: do_filter = True - if (not (isinstance(v, (dict, list)))) and do_filter: - try: + else: + do_filter = False + if (not (isinstance(v, (dict, list)))) and do_filter: + try: replacement_text = replacement_fn(v) data[k] = replacement_text except: pass - else: + else: data[k] = recursive_replace(v, replacement_fn, filter_list, do_filter) return data @@ -170,6 +172,8 @@ def encrypt_dict(encryption_client: ClientEncryption, val, filter_list=None): def coerce_date(dayfirst): def parse_value(v): try: + if isinstance(v, datetime.datetime): + return v.isoformat() return parser.parse(v, dayfirst=dayfirst).isoformat() if v else None except: return None diff --git a/microreact/microreact-server b/microreact/microreact-server index 916bcf21..e9b839da 160000 --- a/microreact/microreact-server +++ b/microreact/microreact-server @@ -1 +1 @@ -Subproject commit 916bcf21841760e38409077115844430101e95b9 +Subproject commit e9b839dacb78859ffbcf07894229cb908313b717 diff --git a/test/requirements.txt b/test/requirements.txt new file mode 100644 index 00000000..8a0e60bc --- /dev/null +++ b/test/requirements.txt @@ -0,0 +1 @@ +python-dateutil==2.8.1 \ No newline at end of file diff --git a/test/test.py b/test/test.py new file mode 100644 index 00000000..8b4fe044 --- /dev/null +++ b/test/test.py @@ -0,0 +1,60 @@ +import os, sys +import datetime +from dateutil import parser + +def recursive_replace(data, replacement_fn, filter_list=None, filtered_parent=False): + # If no filter_list is provided, then assume all leaf nodes in tree must be replaced + do_filter = not filter_list or filtered_parent + if isinstance(data, (dict, list)): + for k, v in data.items() if isinstance(data, dict) else enumerate(data): + # If a key in the filter_list is seen at any node in the tree, leaf values + # underneath that node must be replaced + if k in filter_list: + do_filter = True + else: + do_filter = False + if (not (isinstance(v, (dict, list)))) and do_filter: + try: + replacement_text = replacement_fn(v) + data[k] = replacement_text + except: + pass + else: + data[k] = recursive_replace(v, replacement_fn, filter_list, do_filter) + return data + +def coerce_date(dayfirst): + def parse_value(v): + try: + if isinstance(v, datetime.datetime): + return v.isoformat() + return parser.parse(v, dayfirst=dayfirst).isoformat() if v else None + except: + return None + return parse_value + +def coerce_dates(val, dayfirst=None): + filter_list = list(filter(lambda k: k.startswith("date_"), val.keys())) + print (filter_list) + return recursive_replace( + val, + coerce_date(dayfirst), + filter_list, + ) + +data = { + 'date_sample': datetime.datetime(2024, 5, 19, 2, 56), + 'date_received': '2024-02-19', + 'cpr_nr': '2301232629', + 'name': 'KNUDSEN, ROSA', + 'primary_isolate': False, + 'date_received_kma': None, + 'kma': None, + 'gender': 'K', + 'age': 1, + 'travel': 'Nej', + 'travel_country': None, + 'region': 'MIDTJYLLAND' +} + +print(coerce_dates(data)) \ No newline at end of file diff --git a/web/microreact_integration b/web/microreact_integration index 9319e7dc..360d2ca5 160000 --- a/web/microreact_integration +++ b/web/microreact_integration @@ -1 +1 @@ -Subproject commit 9319e7dc9d7d84e86ceb1698c8aeac12a2d027f3 +Subproject commit 360d2ca506f187ee1f586fa13dc7aa4f033ae637