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

WIP; base for rendering fallback data

parent 65caa63b
......@@ -34,43 +34,56 @@ class Notification:
"""Load notification from it's file"""
try:
with open(path, 'r') as f:
dict = json.load(f)
json_data = json.load(f)
j_skel = dict['skeleton']
content = json_data['content']
skel_obj = NotificationSkeleton(**content['skeleton'])
content['skeleton'] = skel_obj # replace json data with skeleton instance
skel_obj = NotificationSkeleton(j_skel['name'], j_skel['template'], j_skel['actions'])
dict['skeleton'] = skel_obj # replace json data with skeleton object
n = cls(**dict)
return cls(**content)
# set attributes of this instance
# if 'persistent' in dict:
# n.persistent = True
return n
# TODO: exception handling
except Exception as e:
# TODO: proper logging
# TODO: proper logging per exception
logger.warn("Failed to deserialize json file: %s" % e)
# return json_data['fallback']
def valid(self, timestamp=None):
"""If notification is still valid"""
if not timestamp:
t = Notification.generate_timestamp()
ts = Notification.generate_timestamp()
# TODO: compare self.timestamp an ts
def render(self, media_type, lang):
"""Return rendered template as given media type and in given language"""
return self.skeleton.render(media_type, lang, self.content)
def render_all(self):
"""Render all media types in default languages"""
ret = {}
# TODO: compare self.timestamp an t
for mt in self.skeleton.get_media_types():
# render in default lang -> en
ret[mt] = self.render(mt, 'en')
def render(self):
"""Return rendered template as text"""
pass
return ret
def serialize(self):
"""Return serialized data"""
json_data = {
'notif_id': self.notif_id,
'timestamp': self.timestamp,
'persistent': self.persistent,
'message': self.content,
'skeleton': self.skeleton.serialize(),
'content': {
'notif_id': self.notif_id,
'timestamp': self.timestamp,
'persistent': self.persistent,
'message': self.content,
'skeleton': self.skeleton.serialize(),
},
'fallback': self.render_all(),
}
return json.dumps(json_data)
......
......@@ -4,6 +4,9 @@ class NotificationSkeleton:
self.template = template
self.actions = actions
def get_media_types(self):
return self.template['supported_media']
def serialize(self):
json_data = {
'name': self.name,
......@@ -13,6 +16,10 @@ class NotificationSkeleton:
return json_data
def render(self, media_type, lang, content):
"""Render using jinja"""
return None
def __str__(self):
out = "{\n"
out += "\tname: {}\n".format(self.name)
......
......@@ -11,7 +11,6 @@ class NotificationStorage:
self.storage_dirs = {
'persistent': persistent_dir,
'volatile': volatile_dir,
'fallback': None, # in notification itself
}
self.notifications = {}
......@@ -21,7 +20,11 @@ class NotificationStorage:
self.load(persistent_dir)
def store(self, n):
"""Store in memory and serializate to disk"""
"""
Store in memory
serializate to disk
render fallback in default languages
"""
self.notifications[n.notif_id] = n
if n.persistent:
......@@ -29,25 +32,13 @@ class NotificationStorage:
else:
storage_dir = self.storage_dirs['volatile']
# fallback_render_dir = storage_dirs['render_fallback']
# do something to render content
# fallback_content = n.render()
# metadata_content = n.serialize_metadata()
content = n.serialize()
# fileid = self.generate_id()
fileid = n.notif_id
json_data = n.serialize()
# save to disk
regular_file = os.path.join(storage_dir, "{}.json".format(fileid))
# fallback_file = os.path.join(fallback_render_dir, fileid)
file_path = os.path.join(storage_dir, "{}.json".format(n.notif_id))
# TODO: try/catch
with open(regular_file, 'w') as f:
f.write(content)
# with open(fallback_file, 'w') as f:
# f.write(fallback_content)
with open(file_path, 'w') as f:
f.write(json_data)
def load(self, storage_dir):
"""Deserialize from FS"""
......
......@@ -7,14 +7,14 @@ class Plugin:
def __init__(self, name, actions, templates, notifications):
self.name = name
self.actions = []
self.templates = []
self.templates = {}
self.notification_types = {}
for a in actions:
self.actions.append(a)
for t in templates:
self.templates.append(t)
self.templates[t['type']] = t
logger.debug("%s" % notifications)
for n in notifications:
......@@ -35,6 +35,9 @@ class Plugin:
def get_actions(self):
return self.actions
def get_templates(self):
return self.templates
def get_notification_types(self):
return self.notification_types
......
......@@ -50,9 +50,16 @@ class PluginStorage:
if plugin_name in self.plugins:
# TODO: naming?
notification_types = self.plugins[plugin_name].get_notification_types()
templates = self.plugins[plugin_name].get_templates()
if skel_name in notification_types:
self.skeletons[skel_id] = NotificationSkeleton(**notification_types[skel_name]) # cache it
name = notification_types[skel_name]['name']
actions = notification_types[skel_name]['actions']
tmpl_name = notification_types[skel_name]['template']
template = templates[tmpl_name]
self.skeletons[skel_id] = NotificationSkeleton(name, template, actions) # cache it
# else:
# logger.warn("No such notification type '%s' in plugin '%s'" % (skel_name, plugin_name))
# return what?
......
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