From 45d6f42623ce9ff9dacfb1c8578ae9271f8e1ea2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Ja=C5=A1ek?= Date: Tue, 1 Feb 2022 19:37:40 +0100 Subject: [PATCH] allow filtering of subjects before storing (#65) based on config it will filter out subjects which are not used and should not be visible. NHUB-63 --- newsroom/push.py | 6 ++++++ newsroom/web/default_settings.py | 8 ++++++++ tests/core/test_push.py | 12 ++++++++++++ 3 files changed, 26 insertions(+) diff --git a/newsroom/push.py b/newsroom/push.py index 3be4b9d74..6eefc8d5d 100644 --- a/newsroom/push.py +++ b/newsroom/push.py @@ -146,6 +146,12 @@ def publish_item(doc, original): logger.info('Failed to notify new wire item for Agenda watches') logger.exception(ex) + if app.config.get("WIRE_SUBJECT_SCHEME_WHITELIST") and doc.get("subject"): + doc["subject"] = [ + subject for subject in doc["subject"] + if subject.get("scheme") in app.config["WIRE_SUBJECT_SCHEME_WHITELIST"] + ] + publish_item_signal.send(app._get_current_object(), item=doc, is_new=original is None) _id = service.create([doc])[0] if 'associations' not in doc and original is not None and bool(original.get('associations', {})): diff --git a/newsroom/web/default_settings.py b/newsroom/web/default_settings.py index dd82fe41e..bb533b070 100644 --- a/newsroom/web/default_settings.py +++ b/newsroom/web/default_settings.py @@ -412,6 +412,14 @@ APM_SECRET_TOKEN = env("APM_SECRET_TOKEN") APM_SERVICE_NAME = env("APM_SERVICE_NAME") or SITE_NAME +#: Filter out subjects with schema which is not in the whitelist +#: before storing the item to avoid those being displayed in filter, +#: preview and outputs. +#: +#: .. versionadded:: 2.1 +#: +WIRE_SUBJECT_SCHEME_WHITELIST = [] + #: Agenda Filter groups (defaults set in ``newsroom.agenda.init_app``) #: #: .. versionadded:: 2.1.0 diff --git a/tests/core/test_push.py b/tests/core/test_push.py index eff23a4c5..91672358f 100644 --- a/tests/core/test_push.py +++ b/tests/core/test_push.py @@ -46,6 +46,10 @@ def get_signature_headers(data, key): }, 'event_id': 'urn:event/1', 'coverage_id': 'urn:coverage/1', + 'subject': [ + {"name": "a", "code": "a", "scheme": "a"}, + {"name": "b", "code": "b", "scheme": "b"} + ], } @@ -683,3 +687,11 @@ def test_push_event_coverage_info(client, app): parsed = get_entity_or_404(item['guid'], 'items') assert parsed['event_id'] == 'urn:event/1' assert parsed['coverage_id'] == 'urn:coverage/1' + + +def test_push_wire_subject_whitelist(client, app): + app.config["WIRE_SUBJECT_SCHEME_WHITELIST"] = ['b'] + client.post('/push', data=json.dumps(item), content_type='application/json') + parsed = get_entity_or_404(item['guid'], 'items') + assert 1 == len(parsed['subject']) + assert 'b' == parsed['subject'][0]['name']