ref: response schema file
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
@@ -1,17 +0,0 @@
|
||||
from tortoise import fields, models
|
||||
|
||||
|
||||
class Table(models.Model):
|
||||
"""
|
||||
抽象模型
|
||||
"""
|
||||
|
||||
id = fields.IntField(pk=True, description="主键")
|
||||
status = fields.SmallIntField(default=1, description="状态 1有效 9 删除 5选中")
|
||||
created = fields.DatetimeField(auto_now_add=True, description="创建时间", null=True)
|
||||
modified = fields.DatetimeField(auto_now=True, description="更新时间", null=True)
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
ordering = ["-created"]
|
||||
indexes = ("status",)
|
Reference in New Issue
Block a user