feat: Response filter async (#47)

* fix: get_object alter for async response filter

* fix: Alter async response

* feat: Decorator for schema out
This commit is contained in:
vikynoah
2025-04-09 14:00:30 +02:00
committed by GitHub
parent 7afb8e2fd8
commit e160cc5fea
3 changed files with 23 additions and 3 deletions

View File

@@ -54,20 +54,20 @@ def get_object_or_404(
query = select(*selected_columns).where(getattr(db_class, lookup_column) == id)
result = await db.execute(query)
row = result.first()
if row is None:
raise HTTPException(status_code=404, detail="The object does not exist.")
if hasattr(row, "_mapping"):
obj_dict = dict(row._mapping)
else:
obj_dict = {column.key: getattr(row, column.key) for column in selected_columns}
obj_dict = {column.key: getattr(row, column.key)
for column in selected_columns}
else:
query = select(db_class).where(getattr(db_class, lookup_column) == id)
result = await db.execute(query)
row = result.scalar_one_or_none()
if row is None:
raise HTTPException(status_code=404, detail="The object does not exist.")
obj_dict = row
if expunge:
await db.expunge(obj_dict)

View File

@@ -1,2 +1,3 @@
from .base import * # noqa
from .response_schema import * #noqa
from .schema_optional import * #noqa

View File

@@ -0,0 +1,19 @@
from typing import Optional, Type, Union, get_args, get_origin, get_type_hints
from pydantic import BaseModel, create_model
def optional_fields(cls: Type[BaseModel]) -> Type[BaseModel]:
fields = {}
for name, hint in get_type_hints(cls).items():
if name.startswith("_"):
continue
if get_origin(hint) is not Union or type(None) not in get_args(hint):
hint = Optional[hint]
fields[name] = (hint, None)
new_model = create_model(cls.__name__, __base__=cls, **fields)
return new_model