Skip to content

Commit

Permalink
SDAN-725 homepage/notifications crash fix
Browse files Browse the repository at this point in the history
  • Loading branch information
marwoodandrew committed Feb 2, 2024
1 parent ab8d973 commit 1a14b74
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
19 changes: 17 additions & 2 deletions newsroom/notifications/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
from superdesk.utc import utcnow
from flask import current_app as app, session

# The set of fields we need to return to satisfy the notifications
ITEM_FIELDS = ('_id', 'type', 'headline', 'versioncreated')
AGENDA_FIELDS = ('_id', 'type', 'name', 'versioncreated')


class NotificationsResource(newsroom.Resource):
url = 'users/<regex("[a-f0-9]{24}"):user>/notifications'
Expand Down Expand Up @@ -72,14 +76,25 @@ def get_initial_notifications():
items = []
try:
items.extend(superdesk.get_resource_service('wire_search').get_items(item_ids))
for item in items:
item["body_html"] = escape(item["body_html"])
except KeyError: # wire disabled
pass
try:
items.extend(superdesk.get_resource_service('agenda').get_items(item_ids))
except KeyError: # agenda disabled
pass

for item in items:
fields = AGENDA_FIELDS if item.get('type') == 'agenda' else ITEM_FIELDS
keys_to_remove = set(item.keys()) - set(fields)

for key in keys_to_remove:
item.pop(key, None)

# escape any strings to be sure
for key, value in item.items():
if isinstance(value, str):
item[key] = escape(value)

return {
'user': str(session['user']) if session['user'] else None,
'notifications': list(items),
Expand Down
10 changes: 9 additions & 1 deletion newsroom/wire/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ def get_view_data():
}


def escape_strings(obj):
for k, v in obj.items():
if isinstance(v, dict):
escape_strings(v)
elif isinstance(v, str):
obj[k] = escape(v)


def get_items_by_card(cards):
if app.cache.get(HOME_ITEMS_CACHE_KEY):
return app.cache.get(HOME_ITEMS_CACHE_KEY)
Expand All @@ -100,7 +108,7 @@ def get_items_by_card(cards):
get_product_items(ObjectId(card['config']['product']), card['config']['size'])
if items:
for item in items:
item["body_html"] = escape(item["body_html"])
escape_strings(item)
items_by_card[card['label']] = items
elif card['type'] == '4-photo-gallery':
# Omit external media, let the client manually request these
Expand Down

0 comments on commit 1a14b74

Please sign in to comment.