From 914f3765aed20f8cf98ca49aee10c5b21fc3e3f5 Mon Sep 17 00:00:00 2001 From: Hannah Spinde Date: Sat, 11 May 2024 23:38:46 +0200 Subject: [PATCH 1/2] allow filter by specific fields --- factsheet/urls.py | 1 + factsheet/views.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/factsheet/urls.py b/factsheet/urls.py index 155926e71..2115e18e9 100644 --- a/factsheet/urls.py +++ b/factsheet/urls.py @@ -29,6 +29,7 @@ path(r"get_scenarios/", views.get_scenarios), path(r"test_query/", views.test_query), path(r"get_oekg_modifications/", views.get_oekg_modifications), + path(r"get_oekg_modifications_filtered/", views.filter_oekg_modifications), path( r"check-owner//", views.check_ownership, name="check_ownership" ), diff --git a/factsheet/views.py b/factsheet/views.py index 0c663a486..90b37e3cc 100644 --- a/factsheet/views.py +++ b/factsheet/views.py @@ -180,6 +180,16 @@ def get_oekg_modifications(request, *args, **kwargs): patch_response_headers(response, cache_timeout=1) return response +def filter_oekg_modifications(request,*args,**kwargs): + field_name = input("Input the Field you want to filter by: ") + field_value = input("Input the field value: ") + if (str(field_name) in ("bundle_id","id","timestamp","user","user_id")): + kwargs = {field_name:field_value} + histroy = OEKG_Modifications.objects.all().filter(**kwargs) + else: + print("_________________________") + print("This field is not available or not available for filtering yet. \nAvailable options are: bundle_id, id, timestamp, user, user_id") + print("_________________________") # @login_required def create_factsheet(request, *args, **kwargs): From 085036e1e51f4bab4e8501bc46f96c4f17ad0d57 Mon Sep 17 00:00:00 2001 From: Hannah Spinde Date: Tue, 21 May 2024 16:52:12 +0200 Subject: [PATCH 2/2] add filtering old/new state --- factsheet/views.py | 41 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/factsheet/views.py b/factsheet/views.py index 90b37e3cc..33b5d4a76 100644 --- a/factsheet/views.py +++ b/factsheet/views.py @@ -179,6 +179,7 @@ def get_oekg_modifications(request, *args, **kwargs): response = JsonResponse(histroy_json, safe=False, content_type="application/json") patch_response_headers(response, cache_timeout=1) return response + def filter_oekg_modifications(request,*args,**kwargs): field_name = input("Input the Field you want to filter by: ") @@ -186,10 +187,44 @@ def filter_oekg_modifications(request,*args,**kwargs): if (str(field_name) in ("bundle_id","id","timestamp","user","user_id")): kwargs = {field_name:field_value} histroy = OEKG_Modifications.objects.all().filter(**kwargs) + else: #search jsons + matching_entries = [] + i = 0 + for entry in OEKG_Modifications.objects.all(): + if filter_by(i, field_name, field_value): + matching_entries.append(entry.id) + i += 1 + history = OEKG_Modifications.objects.filter(id__in=matching_entries) + histroy_json = serializers.serialize("json", OEKG_Modifications.objects.filter(id__in=matching_entries)) + response = JsonResponse(histroy_json, safe=False, content_type="application/json") + patch_response_headers(response, cache_timeout=1) + + +def filter_state(d, filter_field, filter_val): + if not d: + return False + elif not filter_field in d[0]: + return False else: - print("_________________________") - print("This field is not available or not available for filtering yet. \nAvailable options are: bundle_id, id, timestamp, user, user_id") - print("_________________________") + if isinstance(d[0][filter_field], list): #if the value is a list #add for loop to interate over d[0][filter_field][i] + for i in range (len(d[0][filter_field])): + if filter_val in (d[0][filter_field][i]).values(): + return True + return False + else: + if d[0][filter_field] == filter_val: + return True + return False + +def filter_by(nmbr, filter_field, filter_val): + old_state = json.loads((OEKG_Modifications.objects.values('old_state')[nmbr]).get("old_state", "not found")) + new_state = json.loads((OEKG_Modifications.objects.values('new_state')[nmbr]).get("new_state", "not found")) + if filter_state(old_state, filter_field, filter_val): + return True + if filter_state(new_state, filter_field, filter_val): + return True + return False + # @login_required def create_factsheet(request, *args, **kwargs):