feat: 用户表的增删改查
This commit is contained in:
@@ -1,51 +1,22 @@
|
|||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from enum import Enum
|
|
||||||
|
|
||||||
from fastapi import FastAPI, Depends, HTTPException
|
from fastapi import FastAPI, Depends, HTTPException
|
||||||
from fastapi.security import OAuth2PasswordRequestForm, OAuth2PasswordBearer
|
from fastapi.security import OAuth2PasswordRequestForm, OAuth2PasswordBearer
|
||||||
from jwt import InvalidTokenError
|
from jwt import InvalidTokenError
|
||||||
from pydantic import BaseModel
|
from sqlmodel import select
|
||||||
from sqlmodel import select, Field
|
|
||||||
from starlette import status
|
from starlette import status
|
||||||
|
|
||||||
from app.config.env import env
|
from app.config.env import env
|
||||||
from app.model.BasicModel import BasicModel
|
from app.model.UserModel import RegistryUser, UserModel, UserValidate, PublicUser, UserService
|
||||||
from app.utils.CrpyUtils import CryptUtils, TokenInfo
|
from app.utils.CrpyUtils import CryptUtils, TokenInfo
|
||||||
from app.utils.db_utils import AsyncSessionDep
|
from app.utils.db_utils import AsyncSessionDep
|
||||||
from app.utils.next_id import next_id
|
from app.utils.next_id import next_id
|
||||||
|
|
||||||
|
|
||||||
class UserValidate(str, Enum):
|
|
||||||
Y = 'Y'
|
|
||||||
N = 'N'
|
|
||||||
|
|
||||||
|
|
||||||
# 公共的,也是最后返回给前端的一个用户信息数据类型
|
|
||||||
class PublicUser(BasicModel):
|
|
||||||
username: str = Field(..., description="用户名")
|
|
||||||
email: str = Field(..., description="邮箱")
|
|
||||||
full_name: str = Field(..., description="用户全名")
|
|
||||||
valid: UserValidate = Field(default=UserValidate.N, description="用户账号是否已经激活")
|
|
||||||
|
|
||||||
|
|
||||||
# 注册的时候,客户端传入的用户信息,需要包含这个明文密码字段
|
|
||||||
class RegistryUser(PublicUser):
|
|
||||||
password: str
|
|
||||||
|
|
||||||
|
|
||||||
# 对pl_user表进行增删改查时的这个model类
|
|
||||||
class UserModel(PublicUser, table=True):
|
|
||||||
__tablename__ = "pl_user"
|
|
||||||
hash_password: str
|
|
||||||
|
|
||||||
|
|
||||||
class Token(BaseModel):
|
|
||||||
token: str
|
|
||||||
token_type: str
|
|
||||||
|
|
||||||
|
|
||||||
# 添加用户相关的端点接口
|
# 添加用户相关的端点接口
|
||||||
def add_user_route(app: FastAPI):
|
def add_user_route(app: FastAPI):
|
||||||
|
UserService.add_route(app=app, path="/user")
|
||||||
|
|
||||||
# 用户注册接口
|
# 用户注册接口
|
||||||
@app.post("/registry")
|
@app.post("/registry")
|
||||||
async def _registry(registry_user: RegistryUser, session: AsyncSessionDep):
|
async def _registry(registry_user: RegistryUser, session: AsyncSessionDep):
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
from enum import Enum
|
||||||
|
|
||||||
|
from sqlmodel import Field
|
||||||
|
|
||||||
|
from app.model.BasicModel import BasicModel
|
||||||
|
from app.utils.create_module_service import create_model_service
|
||||||
|
|
||||||
|
|
||||||
|
class UserValidate(str, Enum):
|
||||||
|
Y = 'Y'
|
||||||
|
N = 'N'
|
||||||
|
|
||||||
|
|
||||||
|
# 公共的,也是最后返回给前端的一个用户信息数据类型
|
||||||
|
class PublicUser(BasicModel):
|
||||||
|
username: str = Field(..., description="用户名")
|
||||||
|
email: str = Field(..., description="邮箱")
|
||||||
|
full_name: str = Field(..., description="用户全名")
|
||||||
|
valid: UserValidate = Field(default=UserValidate.N, description="用户账号是否已经激活")
|
||||||
|
|
||||||
|
|
||||||
|
# 注册的时候,客户端传入的用户信息,需要包含这个明文密码字段
|
||||||
|
class RegistryUser(PublicUser):
|
||||||
|
password: str
|
||||||
|
|
||||||
|
|
||||||
|
# 对pl_user表进行增删改查时的这个model类
|
||||||
|
class UserModel(PublicUser, table=True):
|
||||||
|
__tablename__ = "pl_user"
|
||||||
|
hash_password: str
|
||||||
|
|
||||||
|
|
||||||
|
# 对pl_user表进行增删改查时的这个model类
|
||||||
|
class UserServiceModel(PublicUser, table=True):
|
||||||
|
__tablename__ = "pl_user"
|
||||||
|
__table_args__ = {'extend_existing': True}
|
||||||
|
|
||||||
|
|
||||||
|
UserService = create_model_service(UserServiceModel)
|
||||||
Reference in New Issue
Block a user