mirror of
https://github.com/creyD/creyPY.git
synced 2026-04-12 19:30:30 +02:00
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:
@@ -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)
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
from .base import * # noqa
|
||||
from .response_schema import * #noqa
|
||||
from .schema_optional import * #noqa
|
||||
|
||||
19
creyPY/fastapi/schemas/schema_optional.py
Normal file
19
creyPY/fastapi/schemas/schema_optional.py
Normal 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
|
||||
Reference in New Issue
Block a user