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

Refactor datetime code

parent 76385ec5
import json
import random
from datetime import datetime as dt
from datetime import datetime
from jinja2 import TemplateSyntaxError, TemplateRuntimeError, TemplateAssertionError
from .logger import logger
......@@ -33,7 +32,7 @@ class Notification:
def new(cls, skel, **data):
"""Generate some mandatory params during creation"""
nid = cls._generate_id()
ts = cls._generate_timestamp()
ts = int(datetime.utcnow().timestamp())
n = cls(nid, ts, skel, **data)
......@@ -60,9 +59,9 @@ class Notification:
"""If notification is still valid"""
if self.timeout:
if not timestamp:
timestamp = Notification._generate_timestamp()
timestamp = int(datetime.utcnow().timestamp())
creat_time = dt.fromtimestamp(self.timestamp)
creat_time = datetime.fromtimestamp(self.timestamp)
delta = timestamp - creat_time
return delta.total_seconds() < self.timeout
......@@ -111,16 +110,11 @@ class Notification:
returned as string
ts = int(cls._generate_timestamp()) # rounding to int
# append random number for uniqueness
unique = random.randint(1, 1000)
return "{}-{}".format(ts, unique)
ts = int(datetime.utcnow().timestamp())
rand = random.randint(1, 1000)
def _generate_timestamp(cls):
"""Create UTC timestamp"""
return dt.utcnow().timestamp()
# append random number for uniqueness
return "{}-{}".format(ts, rand)
def __str__(self):
out = "{\n"
import os
import subprocess
from datetime import datetime as dt
from datetime import datetime
from .logger import logger
from .notification import Notification
......@@ -78,7 +78,7 @@ class NotificationStorage:
def delete_old_messages(self):
"""Delete messages based on their timeout"""
to_delete = []
now = dt.utcnow()
now = datetime.utcnow()
for n in self.notifications.values():
if not n.valid(now):
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment