Coverage for portality/models/lock.py: 79%
47 statements
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-22 15:59 +0100
« prev ^ index » next coverage.py v6.4.2, created at 2022-07-22 15:59 +0100
1from portality.dao import DomainObject
2from datetime import datetime, timedelta
3import tzlocal
5class Lock(DomainObject):
6 __type__ = "lock"
7 """
8 {
9 "id" : "<opaque id for this lock>",
10 "about" : "<opaque id for the journal/suggestion to which it applies>",
11 "type" : "<journal/suggestion>",
12 "created_date" : "<timestamp this lock record was created>",
13 "expires" : "<timestamp for when this lock record expires>",
14 "username" : "<user name of the user who holds the lock>"
15 }
16 """
17 @property
18 def about(self):
19 return self.data.get("about")
21 def set_about(self, val):
22 self.data["about"] = val
24 @property
25 def type(self):
26 return self.data.get("type")
28 def set_type(self, val):
29 if val not in ["journal", "suggestion"]:
30 return
31 self.data["type"] = val
33 @property
34 def username(self):
35 return self.data.get("username")
37 def set_username(self, val):
38 self.data["username"] = val
40 @property
41 def expires(self):
42 return self.data.get('expires')
44 def expires_in(self, timeout):
45 expires = datetime.now() + timedelta(0, timeout)
46 self.data["expires"] = expires.strftime("%Y-%m-%dT%H:%M:%SZ")
48 def is_expired(self):
49 ed = datetime.strptime(self.expires, "%Y-%m-%dT%H:%M:%SZ")
50 return ed <= datetime.now()
52 def utc_expires(self):
53 ed = datetime.strptime(self.expires, "%Y-%m-%dT%H:%M:%SZ")
54 local = tzlocal.get_localzone()
55 ld = local.localize(ed)
56 tt = ld.utctimetuple()
57 utcdt = datetime(tt.tm_year, tt.tm_mon, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec)
58 return utcdt.strftime("%Y-%m-%dT%H:%M:%SZ")
60 def expire_formatted(self, format="%H:%M"):
61 ed = datetime.strptime(self.expires, "%Y-%m-%dT%H:%M:%SZ")
62 formatted = ed.strftime(format)
63 return formatted
65 def would_expire_within(self, timeout):
66 limit = datetime.now() + timedelta(0, timeout)
67 ed = datetime.strptime(self.expires, "%Y-%m-%dT%H:%M:%SZ")
68 return ed < limit