import datetime
import json
from pydantic import BaseModel, validator
from ..descriptors import MessageMode, MessageType
[docs]
class UserSchema(BaseModel):
name: str = ''
postal_code: str = ''
city: str = '-'
[docs]
class Config:
orm_mode = True
from_attributes = True
[docs]
class ShowUserSchema(BaseModel):
id: str = 0
name: str = ''
postal_code: str = ''
city: str = '-'
[docs]
class Config:
orm_mode = True
from_attributes = True
[docs]
class UserConnection(BaseModel):
name: str
status: str = 'online'
lastConnection: datetime.datetime or str = datetime.datetime.now()
[docs]
class Config:
from_attributes = True
[docs]
class MessageSchema(BaseModel):
user_id: str
content: str = ''
timestamp: datetime.datetime or str = datetime.datetime.now()
mtype: str = MessageType.MESSAGE.value
isMine: bool = False
[docs]
class Config:
__type_descriptor__ = MessageMode()
orm_mode = True
from_attributes = True
[docs]
@validator('mtype')
def validate_mode(cls, v):
if isinstance(v, MessageType):
v = v.value
cls.Config.__type_descriptor__ = v
return v
[docs]
def connection_msg(self):
self.mtype = MessageType.CONNECT.value
self.content = "join the chat"
[docs]
def disconnection_msg(self):
self.mtype = MessageType.DISCONNECT.value
self.content = "left the chat"
[docs]
def to_json(self):
if self.user_id.isdigit():
self.user_id = f'Client {self.user_id}'
dict_ = dict(self)
dict_['timestamp'] = str(dict_['timestamp'])
return json.dumps(dict_)
[docs]
class NotificationSchema(BaseModel):
content: dict = {}
timestamp: datetime.datetime or str = datetime.datetime.now()
type: str = MessageType.MESSAGE.value
[docs]
class Config:
__type_descriptor__ = MessageMode()
from_attributes = True
[docs]
@validator('type')
def validate_mode(cls, v):
if isinstance(v, MessageType):
v = v.value
cls.Config.__type_descriptor__ = v
return v
[docs]
def connection_msg(self):
self.mtype = MessageType.CONNECT.value
self.content = "join the chat"
[docs]
def disconnection_msg(self):
self.mtype = MessageType.DISCONNECT.value
self.content = "left the chat"
[docs]
def to_json(self):
if self.user_id.isdigit():
self.user_id = f'Client {self.user_id}'
dict_ = dict(self)
dict_['timestamp'] = str(dict_['timestamp'])
return json.dumps(dict_)