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

Force media type for rendering; WIP

Raise exception if desired media type is not available
parent 765ae73e
......@@ -5,6 +5,10 @@ import json
import os
from notifylib.api import Api
from notifylib.exceptions import (
MediaTypeNotAvailableException,
NoSuchNotificationException,
)
from notifylib.sorting import Sorting
SEVERITIES = {
......@@ -48,6 +52,7 @@ def create_argparser():
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("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")
parser_call = subparsers.add_parser("call", help="Call actions on messages")
parser_call.add_argument("msgid", help="ID of notification message")
......@@ -79,12 +84,9 @@ def print_notifications(notifications):
def print_notification(notification):
"""Print single rendered notification"""
if not notification:
print("Notification not found")
else:
print("Message: {}".format(notification['message']))
print("Actions: {}".format(notification['actions']))
print("Metadata: {}".format(notification['metadata']))
print("Message: {}".format(notification['message']))
print("Actions: {}".format(notification['actions']))
print("Metadata: {}".format(notification['metadata']))
def process_args(parser, args):
......@@ -136,8 +138,14 @@ def process_args(parser, args):
media_type = args.media_type
lang = args.lang
ret = api.get_rendered_notification(msgid, media_type, lang)
print_notification(ret)
try:
ret = api.get_rendered_notification(msgid, media_type, lang, args.force_media_type)
print_notification(ret)
except NoSuchNotificationException as e:
print(e)
except MediaTypeNotAvailableException as e:
print(e)
elif args.command == 'call':
api.call_action(args.msgid, args.action)
else:
......
......@@ -2,6 +2,7 @@ import os.path
from .config import config
from .exceptions import (
MediaTypeNotAvailableException,
NoSuchActionException,
NoSuchNotificationException,
NotificationNotDismissibleException
......@@ -49,10 +50,20 @@ class Api:
return self.notifications.get_all_rendered(media_type, lang)
def get_rendered_notification(self, msgid, media_type, lang):
def get_rendered_notification(self, msgid, media_type, lang, force_media_type=False):
"""Get rendered notification of specific media type by id"""
self.notifications.delete_invalid_messages()
return self.notifications.get_rendered(msgid, media_type, lang)
if self.notifications.valid_id(msgid):
rendered = self.notifications.get_rendered(msgid, media_type, lang, force_media_type)
if not rendered:
logger.debug("Notification cannot be rendered in media type '%s'", media_type)
raise MediaTypeNotAvailableException("Notification does not have media type '{}'".format(media_type))
return rendered
raise NoSuchNotificationException("Notification ID '{}' does not exist".format(msgid))
def get_templates(self):
"""Return notification types from plugins"""
......
......@@ -10,6 +10,9 @@ class CreateNotificationError(NotifylibError):
class NoSuchActionException(Exception):
pass
class MediaTypeNotAvailableException(Exception):
pass
class NoSuchNotificationException(Exception):
pass
......
......@@ -187,6 +187,9 @@ class Notification:
def get_default_action(self):
return self.default_action
def has_media_type(self, media_type):
return media_type in self.skeleton.get_media_types()
@staticmethod
def _generate_id():
"""
......
......@@ -5,6 +5,7 @@ from collections import OrderedDict
from datetime import datetime
from functools import lru_cache
from .exceptions import MediaTypeNotAvailableException
from .logger import logger
from .notification import Notification
......@@ -95,19 +96,21 @@ class NotificationStorage:
if self.valid_id(msgid):
msgid = self._full_id(msgid)
return self.notifications.get(msgid)
return self.notifications[msgid]
return None
@lru_cache(maxsize=256)
def get_rendered(self, msgid, media_type, lang):
def get_rendered(self, msgid, media_type, lang, force_media_type=False):
"""Return notification either cached or if missing, cache it and return"""
if self.valid_id(msgid):
msgid = self._full_id(msgid)
msgid = self._full_id(msgid)
n = self.notifications[msgid]
return self.notifications[msgid].render(media_type, lang)
mt = n.has_media_type(media_type)
if not mt and force_media_type:
return None
return None
return n.render(media_type, lang)
def get_all(self):
"""Get all stored notification objects"""
......
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