Skip to content

Commit

Permalink
allow filtering of subjects before storing (#65)
Browse files Browse the repository at this point in the history
based on config it will filter out subjects which
are not used and should not be visible.

NHUB-63
  • Loading branch information
petrjasek authored Feb 1, 2022
1 parent 1284571 commit 45d6f42
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions newsroom/push.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', {})):
Expand Down
8 changes: 8 additions & 0 deletions newsroom/web/default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/core/test_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
],
}


Expand Down Expand Up @@ -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']

0 comments on commit 45d6f42

Please sign in to comment.