kinit/kinit-api/core/dependencies.py
liujian8670 ab5df1da27
防止在提交批量删除数据的时候出现数据库操作异常而导致的500,接口异常。
Signed-off-by: liujian8670 <10318920+liujian8670@user.noreply.gitee.com>
2023-04-23 09:34:28 +00:00

64 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/python
# -*- coding: utf-8 -*-
# @version : 1.0
# @Creaet Time : 2022/8/8 14:18
# @File : dependencies.py
# @IDE : PyCharm
# @desc : 常用依赖项
"""
类依赖项-官方文档https://fastapi.tiangolo.com/zh/tutorial/dependencies/classes-as-dependencies/
"""
from typing import List
from fastapi import Body
import copy
class QueryParams:
def __init__(self, params=None):
if params:
self.page = params.page
self.limit = params.limit
self.v_order = params.v_order
self.v_order_field = params.v_order_field
def dict(self, exclude: List[str] = None) -> dict:
result = copy.deepcopy(self.__dict__)
if exclude:
for item in exclude:
try:
del result[item]
except KeyError:
pass
return result
def to_count(self, exclude: List[str] = None) -> dict:
params = self.dict(exclude=exclude)
del params["page"]
del params["limit"]
del params["v_order"]
del params["v_order_field"]
return params
class Paging(QueryParams):
"""
列表分页
"""
def __init__(self, page: int = 1, limit: int = 10, v_order_field: str = "id", v_order: str = None):
super().__init__()
self.page = page
self.limit = limit
self.v_order = v_order
self.v_order_field = v_order_field
class IdList:
"""
id 列表
"""
def __init__(self, ids: List[int] = Body(..., title="ID 列表")):
self.ids = ids