-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
50 lines (37 loc) · 1.03 KB
/
Copy pathmodels.py
File metadata and controls
50 lines (37 loc) · 1.03 KB
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
from datetime import datetime
import pytz
from pydantic import BaseModel, root_validator, validator
from db import MessageType
class MessageModel(BaseModel):
chat: int
utctime: int
msg_type: str
nick: str
text: str = ""
outgoing: bool
@validator("msg_type", pre=True)
def get_message_type(cls, v):
return MessageType(v).name
@validator("utctime", pre=True)
def get_timestamp(cls, v: datetime):
if v.tzinfo:
dt_utc = pytz.utc.normalize(v.astimezone(pytz.utc))
else:
dt_utc = pytz.utc.normalize(pytz.utc.localize(v))
return int(dt_utc.timestamp() * 1000)
@validator("chat", pre=True)
def get_chat_id(cls, v):
return v.id
class Config:
orm_mode = True
class ChatModel(BaseModel):
id: int
jid: str
name: str
is_muc: bool
@root_validator()
def add_type(cls, values):
values["type"] = "muc" if values["is_muc"] else "user"
return values
class Config:
orm_mode = True