-
Notifications
You must be signed in to change notification settings - Fork 16
/
lock.py
72 lines (58 loc) · 2.02 KB
/
lock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from portality.dao import DomainObject
from datetime import datetime, timedelta
import tzlocal
from portality.lib import dates
from portality.lib.dates import FMT_DATETIME_STD
class Lock(DomainObject):
__type__ = "lock"
"""
{
"id" : "<opaque id for this lock>",
"about" : "<opaque id for the journal/suggestion to which it applies>",
"type" : "<journal/suggestion>",
"created_date" : "<timestamp this lock record was created>",
"expires" : "<timestamp for when this lock record expires>",
"username" : "<user name of the user who holds the lock>"
}
"""
@property
def about(self):
return self.data.get("about")
def set_about(self, val):
self.data["about"] = val
@property
def type(self):
return self.data.get("type")
def set_type(self, val):
if val not in ["journal", "suggestion"]:
return
self.data["type"] = val
@property
def username(self):
return self.data.get("username")
def set_username(self, val):
self.data["username"] = val
@property
def expires(self):
return self.data.get('expires')
def expires_in(self, timeout):
expires = dates.now() + timedelta(0, timeout)
self.data["expires"] = expires.strftime(FMT_DATETIME_STD)
def is_expired(self):
ed = dates.parse(self.expires)
return ed <= dates.now()
def utc_expires(self):
ed = dates.parse(self.expires)
local = tzlocal.get_localzone()
ld = local.localize(ed)
tt = ld.utctimetuple()
utcdt = datetime(tt.tm_year, tt.tm_mon, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec)
return utcdt.strftime(FMT_DATETIME_STD)
def expire_formatted(self, format="%H:%M"):
ed = dates.parse(self.expires)
formatted = ed.strftime(format)
return formatted
def would_expire_within(self, timeout):
limit = dates.now() + timedelta(0, timeout)
ed = dates.parse(self.expires)
return ed < limit