From 3d8a73984ef4c20ffae0230d545281806a6363d8 Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Tue, 9 Jan 2024 21:56:32 +0000 Subject: [PATCH 1/5] add a censorship threshold message --- bento_beacon/utils/censorship.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/bento_beacon/utils/censorship.py b/bento_beacon/utils/censorship.py index 49a8005..af9d08b 100644 --- a/bento_beacon/utils/censorship.py +++ b/bento_beacon/utils/censorship.py @@ -2,6 +2,13 @@ from .exceptions import APIException, InvalidQuery +def no_results_censorship_message(): + return ( + "No results. Either none were found, or the query produced results numbering at " + f"or below the threshold for censorship ({current_app.config['COUNT_THRESHOLD']} items)" + ) + + def get_censorship_threshold(): if g.permission_query_data: return 0 From 170d79d10339c1a77108a16132df5dee19744be6 Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Tue, 9 Jan 2024 22:03:03 +0000 Subject: [PATCH 2/5] if zero results and anonymous, mention threshold --- bento_beacon/utils/beacon_response.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bento_beacon/utils/beacon_response.py b/bento_beacon/utils/beacon_response.py index 7b96781..d36461b 100644 --- a/bento_beacon/utils/beacon_response.py +++ b/bento_beacon/utils/beacon_response.py @@ -1,6 +1,6 @@ from flask import current_app, g, request from .katsu_utils import search_summary_statistics, overview_statistics -from .censorship import get_censorship_threshold, censored_count +from .censorship import get_censorship_threshold, censored_count, no_results_censorship_message from .exceptions import InvalidQuery, APIException from ..constants import GRANULARITY_BOOLEAN, GRANULARITY_COUNT, GRANULARITY_RECORD @@ -107,6 +107,8 @@ def build_query_response(ids=None, numTotalResults=None, full_record_handler=Non granularity = response_granularity() count = len(ids) if numTotalResults is None else numTotalResults returned_count = censored_count(count) + if returned_count == 0 and not g.permission_query_data: + add_info_to_response(no_results_censorship_message()) if granularity == GRANULARITY_BOOLEAN: return beacon_boolean_response(returned_count) if granularity == GRANULARITY_COUNT: From 5e5f97222261fbc85d048c1f82fe9e746784743d Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Tue, 9 Jan 2024 22:03:25 +0000 Subject: [PATCH 3/5] whitespace --- bento_beacon/utils/beacon_response.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bento_beacon/utils/beacon_response.py b/bento_beacon/utils/beacon_response.py index d36461b..d323f64 100644 --- a/bento_beacon/utils/beacon_response.py +++ b/bento_beacon/utils/beacon_response.py @@ -10,6 +10,7 @@ def init_response_data(): g.response_data = {} g.response_info = {} + def add_info_to_response(info): add_message({"description": info, "level": "info"}) From 36db4aaed0c3e14a42069e0cc0a07bd72177018a Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Wed, 10 Jan 2024 15:12:05 +0000 Subject: [PATCH 4/5] handle edge case where threshold = 0 --- bento_beacon/utils/beacon_response.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bento_beacon/utils/beacon_response.py b/bento_beacon/utils/beacon_response.py index d323f64..1c47758 100644 --- a/bento_beacon/utils/beacon_response.py +++ b/bento_beacon/utils/beacon_response.py @@ -108,7 +108,7 @@ def build_query_response(ids=None, numTotalResults=None, full_record_handler=Non granularity = response_granularity() count = len(ids) if numTotalResults is None else numTotalResults returned_count = censored_count(count) - if returned_count == 0 and not g.permission_query_data: + if returned_count == 0 and get_censorship_threshold() > 0: add_info_to_response(no_results_censorship_message()) if granularity == GRANULARITY_BOOLEAN: return beacon_boolean_response(returned_count) From 3d39e7a0b7ab443ae8d4e13ec6288d65fd08ef00 Mon Sep 17 00:00:00 2001 From: Gordon Krieger Date: Mon, 22 Jan 2024 14:35:27 +0000 Subject: [PATCH 5/5] put count threshold integer in its own message --- bento_beacon/utils/beacon_response.py | 9 +++++++-- bento_beacon/utils/censorship.py | 6 +----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/bento_beacon/utils/beacon_response.py b/bento_beacon/utils/beacon_response.py index 1c47758..8c4dedb 100644 --- a/bento_beacon/utils/beacon_response.py +++ b/bento_beacon/utils/beacon_response.py @@ -1,6 +1,6 @@ from flask import current_app, g, request from .katsu_utils import search_summary_statistics, overview_statistics -from .censorship import get_censorship_threshold, censored_count, no_results_censorship_message +from .censorship import get_censorship_threshold, censored_count, MESSAGE_FOR_CENSORED_QUERY_WITH_NO_RESULTS from .exceptions import InvalidQuery, APIException from ..constants import GRANULARITY_BOOLEAN, GRANULARITY_COUNT, GRANULARITY_RECORD @@ -21,6 +21,11 @@ def add_message(message_obj): g.response_info["messages"] = messages +def add_no_results_censorship_message_to_response(): + add_info_to_response(MESSAGE_FOR_CENSORED_QUERY_WITH_NO_RESULTS) + add_info_to_response(f"censorship threshold: {current_app.config['COUNT_THRESHOLD']}") + + def add_stats_to_response(ids): if ids is not None and len(ids) <= get_censorship_threshold(): return @@ -109,7 +114,7 @@ def build_query_response(ids=None, numTotalResults=None, full_record_handler=Non count = len(ids) if numTotalResults is None else numTotalResults returned_count = censored_count(count) if returned_count == 0 and get_censorship_threshold() > 0: - add_info_to_response(no_results_censorship_message()) + add_no_results_censorship_message_to_response() if granularity == GRANULARITY_BOOLEAN: return beacon_boolean_response(returned_count) if granularity == GRANULARITY_COUNT: diff --git a/bento_beacon/utils/censorship.py b/bento_beacon/utils/censorship.py index af9d08b..a418628 100644 --- a/bento_beacon/utils/censorship.py +++ b/bento_beacon/utils/censorship.py @@ -2,11 +2,7 @@ from .exceptions import APIException, InvalidQuery -def no_results_censorship_message(): - return ( - "No results. Either none were found, or the query produced results numbering at " - f"or below the threshold for censorship ({current_app.config['COUNT_THRESHOLD']} items)" - ) +MESSAGE_FOR_CENSORED_QUERY_WITH_NO_RESULTS = "No results. Either none were found, or the query produced results numbering at or below the threshold for censorship." def get_censorship_threshold():