ref: response schema file

This commit is contained in:
zy7y 2022-09-12 15:22:08 +08:00
parent 7db1277dd9
commit f27d2e9f7c
18 changed files with 132 additions and 140 deletions

View File

@ -1,7 +1,6 @@
from core import Response
from core.security import generate_token, verify_password
from dbhelper.user import get_user
from schemas.common import LoginForm, LoginResult
from schemas import LoginForm, LoginResult, Response
async def login(auth_data: LoginForm) -> Response[LoginResult]:

View File

@ -1,6 +1,5 @@
from core import Response
from dbhelper.menu import insert_menu
from schemas.menu import MenuIn, MenuRead
from schemas import MenuIn, MenuRead, Response
async def menu_add(data: MenuIn) -> Response[MenuRead]:

View File

@ -1,9 +1,8 @@
import json
from core import Response
from core.utils import list_to_tree
from dbhelper.role import get_role_menus, new_role
from schemas.role import RoleIn, RoleInfo
from schemas import Response, RoleIn, RoleInfo
async def role_add(data: RoleIn) -> Response[RoleInfo]:

View File

@ -1,10 +1,9 @@
from fastapi import Query
from core import Response
from core.security import get_password_hash
from dbhelper.user import get_user, get_user_info, get_users, insert_user, new_user
from schemas import Response, UserAdd, UserIn, UserInfo, UserList, UserQuery, UserRead
from schemas.common import ListAll
from schemas.user import UserAdd, UserIn, UserInfo, UserList, UserQuery, UserRead
async def user_add(data: UserAdd) -> Response[UserInfo]:

View File

@ -1,125 +1 @@
from typing import Generic, Optional, TypeVar
from pydantic import BaseModel, Field
from pydantic.generics import GenericModel
T = TypeVar("T")
class Response(GenericModel, Generic[T]):
code: int = 200
data: Optional[T]
msg: str = "请求成功"
from datetime import datetime
class ReadBase(BaseModel):
"""数据读取的基类"""
id: int
status: int = Field(default=1, description="数据状态 1正常默认值 9 删除 5使用中 ")
created: datetime
modified: datetime
from typing import Any, Callable, get_type_hints
from fastapi import routing
class Route(routing.APIRoute):
"""
https://github.com/tiangolo/fastapi/issues/620
Django挂载视图方法
def index() -> User:
pass
Route("/", endpoint=index)
"""
def __init__(
self,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
if kwargs.get("response_model") is None:
kwargs["response_model"] = get_type_hints(endpoint).get("return")
super(Route, self).__init__(
path=path, endpoint=endpoint, tags=tags, summary=summary, **kwargs
)
@classmethod
def post(
cls,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
return Route(
path=path,
endpoint=endpoint,
methods=["POST"],
tags=tags,
summary=summary,
**kwargs
)
@classmethod
def get(
cls,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
return Route(
path=path,
endpoint=endpoint,
methods=["GET"],
tags=tags,
summary=summary,
**kwargs
)
@classmethod
def delete(
cls,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
return Route(
path=path,
endpoint=endpoint,
methods=["DELETE"],
tags=tags,
summary=summary,
**kwargs
)
@classmethod
def put(
cls,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
return Route(
path=path,
endpoint=endpoint,
methods=["PUT"],
tags=tags,
summary=summary,
**kwargs
)

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,4 @@
from core.table import Table, fields
from models.common import Table, fields
class MenuModel(Table):

View File

@ -1,4 +1,4 @@
from core.table import Table, fields
from models.common import Table, fields
class RoleRelationMixin:

View File

@ -1,4 +1,4 @@
from core.table import Table, fields
from models.common import Table, fields
class RoleModel(Table):

View File

@ -1,4 +1,4 @@
from core.table import Table, fields
from models.common import Table, fields
class UserModel(Table):

View File

@ -1,8 +1,108 @@
from typing import Any, Callable, get_type_hints
from fastapi import routing
from controller.common import login
from controller.menu import menu_add
from controller.role import role_add, role_has_menu
from controller.user import create_user, user_arr, user_info, user_list
from core import Route
class Route(routing.APIRoute):
"""
https://github.com/tiangolo/fastapi/issues/620
Django挂载视图方法
def index() -> User:
pass
Route("/", endpoint=index)
"""
def __init__(
self,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
if kwargs.get("response_model") is None:
kwargs["response_model"] = get_type_hints(endpoint).get("return")
super(Route, self).__init__(
path=path, endpoint=endpoint, tags=tags, summary=summary, **kwargs
)
@classmethod
def post(
cls,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
return Route(
path=path,
endpoint=endpoint,
methods=["POST"],
tags=tags,
summary=summary,
**kwargs
)
@classmethod
def get(
cls,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
return Route(
path=path,
endpoint=endpoint,
methods=["GET"],
tags=tags,
summary=summary,
**kwargs
)
@classmethod
def delete(
cls,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
return Route(
path=path,
endpoint=endpoint,
methods=["DELETE"],
tags=tags,
summary=summary,
**kwargs
)
@classmethod
def put(
cls,
path: str,
endpoint: Callable[..., Any],
tags: list[str],
summary: str,
**kwargs: Any
):
return Route(
path=path,
endpoint=endpoint,
methods=["PUT"],
tags=tags,
summary=summary,
**kwargs
)
routes = [
Route.post("/login", endpoint=login, tags=["公共"], summary="登录"),

View File

@ -0,0 +1,4 @@
from schemas.common import *
from schemas.menu import *
from schemas.role import *
from schemas.user import *

View File

@ -1,5 +1,6 @@
"""公共模型"""
from typing import Generic, TypeVar
from datetime import datetime
from typing import Generic, Optional, TypeVar
from pydantic import BaseModel, Field
from pydantic.generics import GenericModel
@ -7,6 +8,21 @@ from pydantic.generics import GenericModel
T = TypeVar("T")
class Response(GenericModel, Generic[T]):
code: int = 200
data: Optional[T]
msg: str = "请求成功"
class ReadBase(BaseModel):
"""数据读取的基类"""
id: int
status: int = Field(default=1, description="数据状态 1正常默认值 9 删除 5使用中 ")
created: datetime
modified: datetime
class LoginForm(BaseModel):
"""用户登录参数"""

View File

@ -1,8 +1,8 @@
from pydantic import BaseModel, Field
from tortoise.contrib.pydantic import pydantic_model_creator
from core import ReadBase
from models import RoleModel
from schemas.common import ReadBase
RoleRed = pydantic_model_creator(RoleModel, name="RoleOut")

View File

@ -8,7 +8,7 @@ from schemas.common import QueryData
UserRole = pydantic_model_creator(UserRoleModel, name="UserRole", exclude_readonly=True)
from core import ReadBase
from schemas.common import ReadBase
class UserBasic(BaseModel):