/
models.py
94 lines (70 loc) · 2.09 KB
/
models.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import datetime
import logging
from typing import List, Optional
import requests
logger = logging.getLogger("gotify-tray")
try:
local_timezone = datetime.datetime.utcnow().astimezone().tzinfo
except Exception as e:
logger.error(f"gotify.models.local_timezone error: {e}")
local_timezone = None
class AttributeDict(dict):
def __init__(self, *args, **kwargs):
super(AttributeDict, self).__init__(*args, **kwargs)
self.__dict__ = self
class GotifyApplicationModel(AttributeDict):
description: str
id: int
image: str
internal: bool
name: str
token: str
class GotifyPagingModel(AttributeDict):
limit: int
next: Optional[str] = None
since: int
size: int
class GotifyMessageModel(AttributeDict):
appid: int
date: datetime.datetime
extras: Optional[dict] = None
id: int
message: str
priority: Optional[int] = None
title: Optional[str] = None
def __init__(self, d: dict, *args, **kwargs):
s = (
d["date"].split(".")[0] # date
+ "."
+ d["date"].split(".")[1][:6] # ms
+ "+"
+ d["date"].split("+")[-1] # timezone
)
d.update(
{"date": datetime.datetime.fromisoformat(s).astimezone(local_timezone)}
)
super(GotifyMessageModel, self).__init__(d, *args, **kwargs)
class GotifyPagedMessagesModel(AttributeDict):
messages: List[GotifyMessageModel]
paging: GotifyPagingModel
class GotifyHealthModel(AttributeDict):
database: str
health: str
class GotifyVersionModel(AttributeDict):
buildDate: str
commit: str
version: str
class GotifyErrorModel(AttributeDict):
error: str
errorCode: int
errorDescription: str
def __init__(self, response: requests.Response, *args, **kwargs):
try:
j = response.json()
except ValueError:
j = {
"error": "unknown",
"errorCode": response.status_code,
"errorDescription": "",
}
super(GotifyErrorModel, self).__init__(j, *args, **kwargs)