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

API cleanup and refactoring

parent c6013b61
......@@ -21,23 +21,15 @@ class Api:
config.get('settings', 'persistent_dir'),
)
def delete_invalid_messages(self):
"""Delete all invalid messages in storage"""
self.notifications.delete_invalid_messages()
def get_actions(self, plug_name):
"""Get actions of specified plugin"""
return self.plugins.get_plugin(plug_name).get_actions()
def get_notifications(self):
"""Return all notifications"""
self.delete_invalid_messages()
self.notifications.delete_invalid_messages()
return self.notifications.get_all()
def get_rendered_notification(self, msgid, media_type, lang):
"""Show notification of one specific by id"""
self.delete_invalid_messages()
return self.notifications.get_rendered_notification(msgid, media_type, lang)
self.notifications.delete_invalid_messages()
return self.notifications.get_rendered(msgid, media_type, lang)
def get_templates(self):
"""Return notification types from plugins"""
......@@ -67,10 +59,12 @@ class Api:
def call_action(self, msgid, name):
"""Call action on notification"""
self.delete_invalid_messages()
self.notifications.delete_invalid_messages()
n = self.notifications.get_notification(msgid)
n.call_action(name)
n = self.notifications.get(msgid)
if name == 'dismiss':
n.dismiss()
self.notifications.remove(msgid)
else:
n.call_action(name)
......@@ -59,17 +59,18 @@ class Notification:
def is_valid(self, timestamp=None):
"""If notification is still valid based on multiple conditions"""
if not timestamp:
timestamp = int(datetime.utcnow().timestamp())
if not self.valid:
return False
if self.timeout:
if not timestamp:
timestamp = int(datetime.utcnow().timestamp())
creat_time = datetime.fromtimestamp(self.timestamp)
delta = timestamp - creat_time
create_time = datetime.fromtimestamp(self.timestamp)
delta = timestamp - create_time
return delta.total_seconds() < self.timeout
if delta.total_seconds() >= self.timeout:
return False
return True
......@@ -113,13 +114,12 @@ class Notification:
return json.dumps(json_data, indent=4)
def dismiss(self):
self.valid = False
def call_action(self, name):
if name == 'dismiss':
self.valid = False
else:
# call action from skeleton
# TODO: action args?
self.skeleton.call_action(name)
# call action from skeleton
self.skeleton.call_action(name)
@staticmethod
def _generate_id():
......
......@@ -55,10 +55,11 @@ class NotificationStorage:
n = Notification.from_file(filepath)
self.notifications[n.notif_id] = n
def get_notification(self, msgid):
def get(self, msgid):
"""Return single notification instance"""
return self.notifications[msgid]
def get_rendered_notification(self, msgid, media_type, lang):
def get_rendered(self, msgid, media_type, lang):
"""Return notification either cached or if missing, cache it and return"""
if (msgid, media_type, lang) not in self.rendered:
self.rendered[(msgid, media_type, lang)] = self.notifications[msgid].render(media_type, lang)
......
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