update
This commit is contained in:
parent
aa5d78b1cd
commit
4e735b2d72
@ -52,7 +52,7 @@ async def create_api(
|
||||
async def update_api(
|
||||
api_in: ApiUpdate,
|
||||
):
|
||||
await api_controller.update(id=api_in.id, obj_in=api_in.update_dict())
|
||||
await api_controller.update(id=api_in.id, obj_in=api_in)
|
||||
return Success(msg="Update Successfully")
|
||||
|
||||
|
||||
|
||||
@ -16,13 +16,15 @@ async def list_menu(
|
||||
page: int = Query(1, description="页码"),
|
||||
page_size: int = Query(10, description="每页数量"),
|
||||
):
|
||||
parent_menus = await menu_controller.model.filter(parent_id=0).order_by("order")
|
||||
res_menu = []
|
||||
for menu in parent_menus:
|
||||
child_menu = await menu_controller.model.filter(parent_id=menu.id).order_by("order")
|
||||
async def get_menu_with_children(menu_id: int):
|
||||
menu = await menu_controller.model.get(id=menu_id)
|
||||
menu_dict = await menu.to_dict()
|
||||
menu_dict["children"] = [await obj.to_dict() for obj in child_menu]
|
||||
res_menu.append(menu_dict)
|
||||
child_menus = await menu_controller.model.filter(parent_id=menu_id).order_by("order")
|
||||
menu_dict["children"] = [await get_menu_with_children(child.id) for child in child_menus]
|
||||
return menu_dict
|
||||
|
||||
parent_menus = await menu_controller.model.filter(parent_id=0).order_by("order")
|
||||
res_menu = [await get_menu_with_children(menu.id) for menu in parent_menus]
|
||||
return SuccessExtra(data=res_menu, total=len(res_menu), page=page, page_size=page_size)
|
||||
|
||||
|
||||
@ -46,7 +48,7 @@ async def create_menu(
|
||||
async def update_menu(
|
||||
menu_in: MenuUpdate,
|
||||
):
|
||||
await menu_controller.update(id=menu_in.id, obj_in=menu_in.update_dict())
|
||||
await menu_controller.update(id=menu_in.id, obj_in=menu_in)
|
||||
return Success(msg="Updated Success")
|
||||
|
||||
|
||||
|
||||
@ -47,7 +47,7 @@ async def create_role(role_in: RoleCreate):
|
||||
|
||||
@router.post("/update", summary="更新角色")
|
||||
async def update_role(role_in: RoleUpdate):
|
||||
await role_controller.update(id=role_in.id, obj_in=role_in.update_dict())
|
||||
await role_controller.update(id=role_in.id, obj_in=role_in)
|
||||
return Success(msg="Updated Successfully")
|
||||
|
||||
|
||||
|
||||
@ -5,7 +5,6 @@ from fastapi.exceptions import HTTPException
|
||||
from tortoise.expressions import Q
|
||||
|
||||
from app.controllers.user import UserController
|
||||
from app.core.dependency import DependPermisson
|
||||
from app.schemas.base import Success, SuccessExtra
|
||||
from app.schemas.users import *
|
||||
|
||||
|
||||
@ -28,7 +28,7 @@ class UserController(CRUDBase[User, UserCreate, UserUpdate]):
|
||||
return obj
|
||||
|
||||
async def update(self, obj_in: UserUpdate) -> User:
|
||||
return await super().update(id=obj_in.id, obj_in=obj_in.update_dict())
|
||||
return await super().update(id=obj_in.id, obj_in=obj_in)
|
||||
|
||||
async def update_last_login(self, id: int) -> None:
|
||||
user = await self.model.get(id=id)
|
||||
|
||||
@ -34,7 +34,7 @@ class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
||||
if isinstance(obj_in, Dict):
|
||||
obj_dict = obj_in
|
||||
else:
|
||||
obj_dict = obj_in.model_dump(exclude_unset=True)
|
||||
obj_dict = obj_in.model_dump(exclude_unset=True, exclude={"id"})
|
||||
obj = await self.get(id=id)
|
||||
obj = obj.update_from_dict(obj_dict)
|
||||
await obj.save()
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
# 新增model需要在这里导入
|
||||
from .admin import *
|
||||
from .admin import *
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
import asyncio
|
||||
from datetime import datetime
|
||||
|
||||
from tortoise import fields, models
|
||||
@ -20,18 +21,18 @@ class BaseModel(models.Model):
|
||||
value = value.strftime(settings.DATETIME_FORMAT)
|
||||
d[field] = value
|
||||
if m2m:
|
||||
for field in self._meta.m2m_fields:
|
||||
if field not in exclude_fields:
|
||||
values = [value for value in await getattr(self, field).all().values()]
|
||||
for value in values:
|
||||
value.update(
|
||||
(k, v.strftime(settings.DATETIME_FORMAT))
|
||||
for k, v in value.items()
|
||||
if isinstance(v, datetime)
|
||||
)
|
||||
d[field] = values
|
||||
tasks = [self.__fetch_m2m_field(field) for field in self._meta.m2m_fields if field not in exclude_fields]
|
||||
results = await asyncio.gather(*tasks)
|
||||
for field, values in results:
|
||||
d[field] = values
|
||||
return d
|
||||
|
||||
async def __fetch_m2m_field(self, field):
|
||||
values = [value for value in await getattr(self, field).all().values()]
|
||||
for value in values:
|
||||
value.update((k, v.strftime(settings.DATETIME_FORMAT)) for k, v in value.items() if isinstance(v, datetime))
|
||||
return field, values
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
|
||||
@ -16,6 +16,3 @@ class ApiCreate(BaseApi):
|
||||
|
||||
class ApiUpdate(BaseApi):
|
||||
id: int
|
||||
|
||||
def update_dict(self):
|
||||
return self.model_dump(exclude_unset=True, exclude={"id"})
|
||||
|
||||
@ -50,6 +50,3 @@ class MenuUpdate(BaseModel):
|
||||
component: str = Field(example="/system/user")
|
||||
keepalive: Optional[bool] = False
|
||||
redirect: Optional[str] = ""
|
||||
|
||||
def update_dict(self):
|
||||
return self.model_dump(exclude_unset=True, exclude={"id"})
|
||||
|
||||
@ -25,9 +25,6 @@ class RoleUpdate(BaseModel):
|
||||
name: str = Field(example="管理员")
|
||||
desc: str = Field("", example="管理员角色")
|
||||
|
||||
def update_dict(self):
|
||||
return self.model_dump(exclude_unset=True, exclude={"id"})
|
||||
|
||||
|
||||
class RoleUpdateMenusApis(BaseModel):
|
||||
id: int
|
||||
|
||||
@ -36,9 +36,6 @@ class UserUpdate(BaseModel):
|
||||
is_superuser: Optional[bool] = False
|
||||
roles: Optional[List[int]] = []
|
||||
|
||||
def update_dict(self):
|
||||
return self.model_dump(exclude_unset=True, exclude={"roles", "id"})
|
||||
|
||||
|
||||
class UpdatePassword(BaseModel):
|
||||
id: int = Field(description="用户ID")
|
||||
|
||||
1275
poetry.lock
generated
1275
poetry.lock
generated
File diff suppressed because it is too large
Load Diff
@ -7,10 +7,10 @@ readme = "README.md"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = "^3.11"
|
||||
fastapi = "^0.100.0"
|
||||
uvicorn = "^0.23.1"
|
||||
tortoise-orm = "^0.19.3"
|
||||
pydantic = "^2.3.0"
|
||||
fastapi = "0.111.0"
|
||||
uvicorn = "^0.23.0"
|
||||
tortoise-orm = "^0.20.1"
|
||||
pydantic = "^2.7.1"
|
||||
email-validator = "^2.0.0.post2"
|
||||
passlib = "^1.7.4"
|
||||
pyjwt = "^2.7.0"
|
||||
@ -20,6 +20,8 @@ ruff = "^0.0.281"
|
||||
loguru = "^0.7.0"
|
||||
pydantic-settings = "^2.0.3"
|
||||
argon2-cffi = "^23.1.0"
|
||||
pydantic-core = "^2.18.2"
|
||||
annotated-types = "^0.6.0"
|
||||
|
||||
[tool.black]
|
||||
line-length = 120
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user