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

Refactor datetime code

parent 76385ec5
import json import json
import random import random
from datetime import datetime as dt from datetime import datetime
from jinja2 import TemplateSyntaxError, TemplateRuntimeError, TemplateAssertionError from jinja2 import TemplateSyntaxError, TemplateRuntimeError, TemplateAssertionError
from .logger import logger from .logger import logger
...@@ -33,7 +32,7 @@ class Notification: ...@@ -33,7 +32,7 @@ class Notification:
def new(cls, skel, **data): def new(cls, skel, **data):
"""Generate some mandatory params during creation""" """Generate some mandatory params during creation"""
nid = cls._generate_id() nid = cls._generate_id()
ts = cls._generate_timestamp() ts = int(datetime.utcnow().timestamp())
n = cls(nid, ts, skel, **data) n = cls(nid, ts, skel, **data)
...@@ -60,9 +59,9 @@ class Notification: ...@@ -60,9 +59,9 @@ class Notification:
"""If notification is still valid""" """If notification is still valid"""
if self.timeout: if self.timeout:
if not timestamp: 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 delta = timestamp - creat_time
return delta.total_seconds() < self.timeout return delta.total_seconds() < self.timeout
...@@ -111,16 +110,11 @@ class Notification: ...@@ -111,16 +110,11 @@ class Notification:
returned as string returned as string
""" """
ts = int(cls._generate_timestamp()) # rounding to int ts = int(datetime.utcnow().timestamp())
# append random number for uniqueness rand = random.randint(1, 1000)
unique = random.randint(1, 1000)
return "{}-{}".format(ts, unique)
@classmethod # append random number for uniqueness
def _generate_timestamp(cls): return "{}-{}".format(ts, rand)
"""Create UTC timestamp"""
return dt.utcnow().timestamp()
def __str__(self): def __str__(self):
out = "{\n" out = "{\n"
......
import os import os
import subprocess import subprocess
from datetime import datetime as dt from datetime import datetime
from .logger import logger from .logger import logger
from .notification import Notification from .notification import Notification
...@@ -78,7 +78,7 @@ class NotificationStorage: ...@@ -78,7 +78,7 @@ class NotificationStorage:
def delete_old_messages(self): def delete_old_messages(self):
"""Delete messages based on their timeout""" """Delete messages based on their timeout"""
to_delete = [] to_delete = []
now = dt.utcnow() now = datetime.utcnow()
for n in self.notifications.values(): for n in self.notifications.values():
if not n.valid(now): if not n.valid(now):
......
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