Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDAN-725 homepage/notifications crash fix #1185

Open
wants to merge 1 commit into
base: lockhart
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading