subjects.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """Module schemas/subjects verifies table data type"""
  2. from datetime import date
  3. from datetime import datetime
  4. from typing import List, Literal
  5. from typing import Optional
  6. from typing import Union
  7. from pydantic import BaseModel
  8. from pydantic import Field
  9. from pydantic import validator
  10. from schemas.trains import ShowTrain
  11. from settings.config import settings
  12. language = settings.config["lang"]
  13. message_dict = settings.get_message()
  14. message = message_dict[language]
  15. def get_timestamp() -> datetime:
  16. return datetime.strptime(datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
  17. "%Y-%m-%d %H:%M:%S")
  18. class SubjectBase(BaseModel):
  19. """Subject Base Pydantic Model"""
  20. name: str
  21. id_card: Union[str, None]
  22. gender: Literal["男", "女"]
  23. birthday: date
  24. rehabilitation_parts: list
  25. remarks: str = ""
  26. @validator("birthday")
  27. def validate_birthday_date(cls, value):
  28. value = datetime.strptime(str(value), "%Y-%m-%d")
  29. if value > datetime.now() or value < datetime.strptime(
  30. "1880-01-01", "%Y-%m-%d"):
  31. raise ValueError(message["form_error_gender"])
  32. return value
  33. @validator("rehabilitation_parts")
  34. def validate_rehabilitation_parts_date(cls, value):
  35. if len(value) == 0 or len(value) > 4:
  36. raise ValueError(message["rehab_parts_length"])
  37. for part in value:
  38. if part not in ["左手", "右手", "左腿", "右腿"]:
  39. raise ValueError(message["rehab_parts_value_error"])
  40. return value
  41. class SubjectUpdate(SubjectBase):
  42. pass
  43. class SubjectCreate(SubjectBase):
  44. create_time: Optional[datetime] = Field(default_factory=get_timestamp)
  45. class ShowSubject(BaseModel):
  46. """展示患者信息"""
  47. id: str
  48. name: str
  49. id_card: str
  50. age: int
  51. gender: str
  52. rehabilitation_parts: str
  53. create_time: datetime
  54. class Config():
  55. orm_mode = True
  56. class ShowSubjectDetails(ShowSubject):
  57. """展示患者详情"""
  58. trains: List[ShowTrain]
  59. class TodayStats(BaseModel):
  60. today_num_format: str
  61. class SubjectIds(BaseModel):
  62. ids: List[str] = Field(...)