fixup! fixup! fixup! fixup! WIP: Refactor jinja_env + make fallback actually work

parent 4d5e3900
......@@ -15,6 +15,9 @@ class NoSuchActionException(NotifylibError):
class MediaTypeNotAvailableException(NotifylibError):
pass
class NoSuchTemplateException(NotifylibError):
pass
class NoSuchNotificationException(NotifylibError):
pass
......
......@@ -8,7 +8,12 @@ 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)
......
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