...
 
......@@ -60,7 +60,7 @@ def create_argparser():
parser_get = subparsers.add_parser("get", help="Get specific message")
parser_get.add_argument("msgid", help="ID of notification message")
parser_get.add_argument("media_type", help="Media type of notification message", nargs="?", default="simple")
parser_get.add_argument("media_type", help="Media type of notification message", nargs="?", default="plain")
parser_get.add_argument("lang", help="Language of notification message", nargs="?", default="en")
parser_get.add_argument("--force-media-type", dest="force_media_type", help="Request media type and don't return default media type in case requested one is not available", action="store_true")
......
......@@ -45,7 +45,7 @@ class Api:
self.plugins = PluginStorage(plugin_dir)
self.notifications = NotificationStorage(volatile_dir, persistent_dir, self.plugins)
def get_notifications(self, media_type='simple', lang='en'):
def get_notifications(self, media_type='plain', lang='en'):
"""Return all notifications"""
self.notifications.delete_invalid_messages()
......
......@@ -15,6 +15,9 @@ class NoSuchActionException(NotifylibError):
class MediaTypeNotAvailableException(NotifylibError):
pass
class NoSuchTemplateException(NotifylibError):
pass
class NoSuchNotificationException(NotifylibError):
pass
......
......@@ -3,12 +3,17 @@ import logging
import uuid
from datetime import datetime
from jinja2 import TemplateError, TemplateNotFound
from jinja2 import TemplateError
from types import SimpleNamespace
from . import api_version
from .config import config
from .exceptions import CreateNotificationError, NotificationTemplatingError, VersionMismatchException
from .exceptions import (
CreateNotificationError,
NoSuchTemplateException,
NotificationTemplatingError,
VersionMismatchException
)
from .notificationskeleton import NotificationSkeleton
from .supervisor import Supervisor
......@@ -132,21 +137,24 @@ class Notification:
return self.skeleton.render(self.data, media_type, lang)
except TemplateError:
raise NotificationTemplatingError("Failed to render template")
except NoSuchTemplateException:
raise NotificationTemplatingError("Could not find template file")
def render(self, media_type, lang):
"""Return rendered template as given media type and in given language"""
try:
output = {}
output['message'] = self.render_template(media_type, lang)
output['actions'] = self.skeleton.translate_actions(lang)
output = {}
output['actions'] = self.skeleton.translate_actions(lang)
if self.explicit_dismiss:
output['actions']['dismiss'] = '' # default action for all notifications
output['metadata'] = self._serialize_data(self.META_ATTRS)
if self.explicit_dismiss:
output['actions']['dismiss'] = '' # default action for all notifications
output['metadata'] = self._serialize_data(self.META_ATTRS)
try:
output['message'] = self.render_template(media_type, lang)
return output
except NotificationTemplatingError:
return self.fallback[media_type]
output['message'] = self.fallback[media_type]
return output
def render_fallback_data(self):
"""Render all media types in default languages"""
......
......@@ -5,6 +5,7 @@ import os
import yaml
from jinja2 import TemplateNotFound
from .exceptions import NoSuchTemplateException
logger = logging.getLogger(__name__)
......@@ -92,6 +93,9 @@ class NotificationSkeleton:
def render(self, data, media_type, lang):
"""Render using jinja in given language"""
if self.fallback:
raise NoSuchTemplateException
self._set_jinja_translation(lang)
output = self.jinja_message_template.render(media=media_type, **data)
......