Coverage for portality / models / lock.py: 80%

49 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-05-05 00:09 +0100

1from portality.dao import DomainObject 

2from datetime import datetime, timedelta 

3import tzlocal, pytz 

4 

5from portality.lib import dates 

6from portality.lib.dates import FMT_DATETIME_STD 

7 

8 

9class Lock(DomainObject): 

10 __type__ = "lock" 

11 """ 

12 { 

13 "id" : "<opaque id for this lock>", 

14 "about" : "<opaque id for the journal/suggestion to which it applies>", 

15 "type" : "<journal/suggestion>", 

16 "created_date" : "<timestamp this lock record was created>", 

17 "expires" : "<timestamp for when this lock record expires>", 

18 "username" : "<user name of the user who holds the lock>" 

19 } 

20 """ 

21 @property 

22 def about(self): 

23 return self.data.get("about") 

24 

25 def set_about(self, val): 

26 self.data["about"] = val 

27 

28 @property 

29 def type(self): 

30 return self.data.get("type") 

31 

32 def set_type(self, val): 

33 if val not in ["journal", "suggestion"]: 

34 return 

35 self.data["type"] = val 

36 

37 @property 

38 def username(self): 

39 return self.data.get("username") 

40 

41 def set_username(self, val): 

42 self.data["username"] = val 

43 

44 @property 

45 def expires(self): 

46 return self.data.get('expires') 

47 

48 def expires_in(self, timeout): 

49 expires = dates.now() + timedelta(0, timeout) 

50 self.data["expires"] = expires.strftime(FMT_DATETIME_STD) 

51 

52 def is_expired(self): 

53 ed = dates.parse(self.expires) 

54 return ed <= dates.now() 

55 

56 def utc_expires(self): 

57 ed = dates.parse(self.expires) 

58 local = pytz.timezone(str(tzlocal.get_localzone())) 

59 ld = local.localize(ed) 

60 tt = ld.utctimetuple() 

61 utcdt = datetime(tt.tm_year, tt.tm_mon, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec) 

62 return utcdt.strftime(FMT_DATETIME_STD) 

63 

64 def expire_formatted(self, format="%H:%M"): 

65 ed = dates.parse(self.expires) 

66 formatted = ed.strftime(format) 

67 return formatted 

68 

69 def would_expire_within(self, timeout): 

70 limit = dates.now() + timedelta(0, timeout) 

71 ed = dates.parse(self.expires) 

72 return ed < limit