Files
Wu Clan 7bfc7908ab Migrate to pydantic-v2 (#246)
* update the schema to pydantic-v2

* upgrade the ruff version in pre-commit

* update pagination to p-v2

* update encoder to p-v2

* update response_schema to p-v2

* update exception_handler to p-v2

* update config to p-v2

* update crud base to p-v2

* Migrate the JSON Encoder to the official one to reduce maintenance costs

* updated readme description

* restore the bcrypt compatible version

* fix validation error log code return format

* Fix manual processing method for GetCurrentUserInfo

* Integrate pydantic validate exceptions and user exceptions

* Fix validation exception handler

* add custom open response code

* Add a stand-alone assertion error handler

* Restore todo deleted by v1 branch to v2

* Add email and phone number types
2023-12-20 23:56:37 +08:00

95 lines
2.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from datetime import datetime
from pydantic import ConfigDict, EmailStr, Field, HttpUrl, model_validator
from backend.app.common.enums import StatusType
from backend.app.schemas.base import CustomPhoneNumber, SchemaBase
from backend.app.schemas.dept import GetAllDept
from backend.app.schemas.role import GetAllRole
class Auth(SchemaBase):
username: str
password: str
class AuthLogin(Auth):
captcha: str
class RegisterUser(Auth):
nickname: str | None = None
email: EmailStr = Field(..., example='user@example.com')
class AddUser(Auth):
dept_id: int
roles: list[int]
nickname: str | None = None
email: EmailStr = Field(..., example='user@example.com')
class _UserInfoBase(SchemaBase):
dept_id: int | None = None
username: str
nickname: str
email: EmailStr = Field(..., example='user@example.com')
phone: CustomPhoneNumber | None = None
class UpdateUser(_UserInfoBase):
pass
class UpdateUserRole(SchemaBase):
roles: list[int]
class Avatar(SchemaBase):
url: HttpUrl = Field(..., description='头像 http 地址')
class GetUserInfoNoRelation(_UserInfoBase):
model_config = ConfigDict(from_attributes=True)
dept_id: int | None = None
id: int
uuid: str
avatar: str | None = None
status: StatusType = Field(default=StatusType.enable)
is_superuser: bool
is_staff: bool
is_multi_login: bool
join_time: datetime = None
last_login_time: datetime | None = None
class GetAllUserInfo(GetUserInfoNoRelation):
model_config = ConfigDict(from_attributes=True)
dept: GetAllDept | None = None
roles: list[GetAllRole]
class GetCurrentUserInfo(GetAllUserInfo):
model_config = ConfigDict(from_attributes=True)
@model_validator(mode='after')
def handel(self, values):
"""处理部门和角色"""
dept = self.dept
if dept:
self.dept = dept.name # type: ignore
roles = self.roles
if roles:
self.roles = [role.name for role in roles] # type: ignore
return values
class ResetPassword(SchemaBase):
old_password: str
new_password: str
confirm_password: str