Commit cfa2d78d authored by Martin Matějek's avatar Martin Matějek

Return translated notifications from Api

Return only relevant information without underlying technical details
parent 183ca02d
......@@ -21,12 +21,11 @@ class Api:
config.get('settings', 'persistent_dir'),
def get_notifications(self):
def get_notifications(self, media_type='simple', lang='en'):
"""Return all notifications"""
notifications = self.notifications.get_all()
return {k: v.get_data() for k, v in notifications.items()}
return self.notifications.get_all_rendered(media_type, lang)
def get_rendered_notification(self, msgid, media_type, lang):
"""Get rendered notification of specific media type by id"""
......@@ -14,6 +14,7 @@ from .notificationskeleton import NotificationSkeleton
class Notification:
ATTRS = ['notif_id', 'timestamp', 'skeleton', 'persistent', 'timeout', 'severity', 'data', 'fallback', 'valid']
META_ATTRS = ['persistent', 'severity']
def __init__(self, notif_id, timestamp, skeleton, data, persistent, timeout, severity, fallback=None, valid=True):
self.notif_id = notif_id
......@@ -87,7 +88,12 @@ class Notification:
def render(self, media_type, lang):
"""Return rendered template as given media type and in given language"""
return self.render_template(media_type, lang)
output = {}
output['message'] = self.render_template(media_type, lang)
output['actions'] = self.skeleton.translate_actions(lang)
output['metadata'] = self._serialize_metadata()
return output
except NotificationTemplatingError:
return self.fallback[media_type]
......@@ -118,6 +124,14 @@ class Notification:
return attrs
def _serialize_metadata(self):
attrs = {}
for attr in self.META_ATTRS:
attrs[attr] = getattr(self, attr)
return attrs
def serialize(self):
"""Return serialized data as json"""
return json.dumps(self._serialize_data(), indent=4)
......@@ -72,9 +72,35 @@ class NotificationStorage:
return self.rendered[(msgid, media_type, lang)]
def get_all(self):
"""Get all notifications as strings"""
"""Get all stored notification objects"""
return self.notifications
def filter_rendered(self, media_type, lang):
"""Filter rendered notifications by lang and media_type"""
result = {}
for k, v in self.rendered.items():
if k[1] == media_type and k[2] == lang:
result[k[0]] = v
return result
def get_all_rendered(self, media_type, lang):
"""Get all notifications rendered in lang and in given media_type"""
# expand notification keys to triples
expanded_keys = []
for k in self.notifications.keys():
expanded_keys.append((k, media_type, lang))
# render only uncached notifications
cached = set(self.rendered.keys())
for k in expanded_keys:
if k not in cached:
# return notifications only in desired media_type and lang
return self.filter_rendered(media_type, lang)
def delete_invalid_messages(self):
"""Delete messages based on their timeout"""
to_delete = []
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment