events are updated
This commit is contained in:
@@ -1,58 +1,96 @@
|
||||
from typing import Any, Union
|
||||
from fastapi import status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from databases.sql_models.response_model import AlchemyResponse
|
||||
|
||||
|
||||
class AlchemyJsonResponse:
|
||||
status_code: status
|
||||
message: str
|
||||
result: AlchemyResponse
|
||||
completed: bool
|
||||
filter_attributes: Any = None
|
||||
response_model: Any = None
|
||||
def __new__(
|
||||
cls,
|
||||
message: str,
|
||||
status_code: str = 'HTTP_200_OK',
|
||||
result: Union[Any, list] = None,
|
||||
completed: bool = True
|
||||
):
|
||||
cls.status_code = getattr(status, status_code, 'HTTP_200_OK')
|
||||
cls.message = message
|
||||
cls.result = result
|
||||
cls.completed = completed
|
||||
|
||||
first_item = cls.result.get(1)
|
||||
if not first_item:
|
||||
return JSONResponse(
|
||||
status_code=cls.status_code,
|
||||
content=dict(
|
||||
total_count=0,
|
||||
count=0,
|
||||
pagination=None,
|
||||
completed=cls.completed,
|
||||
message=cls.message,
|
||||
data=[],
|
||||
),
|
||||
)
|
||||
if cls.result.first:
|
||||
return JSONResponse(
|
||||
status_code=cls.status_code,
|
||||
content=dict(
|
||||
total_count=1,
|
||||
count=1,
|
||||
pagination=None,
|
||||
completed=cls.completed,
|
||||
message=cls.message,
|
||||
data=cls.result.data.get_dict(),
|
||||
),
|
||||
)
|
||||
|
||||
if not first_item.filter_attr:
|
||||
counts = cls.result.count
|
||||
return JSONResponse(
|
||||
status_code=cls.status_code,
|
||||
content=dict(
|
||||
total_count=counts,
|
||||
count=counts,
|
||||
pagination=None,
|
||||
completed=cls.completed,
|
||||
message=cls.message,
|
||||
data=[result_data.get_dict() for result_data in cls.result.data],
|
||||
),
|
||||
)
|
||||
|
||||
filter_model = first_item.filter_attr
|
||||
total_count = cls.result.query.limit(None).offset(None).count()
|
||||
total_page_number = round(total_count / int(first_item.filter_attr.size), 0) + 1
|
||||
|
||||
def return_json_response_from_alchemy(
|
||||
response, cls_obj=None, pagination=None, response_model=None
|
||||
):
|
||||
enums = cls_obj.__enums__ if cls_obj else []
|
||||
if response.count:
|
||||
total_count = response.query.limit(None).offset(None).count()
|
||||
total_page_number = round(total_count / int(pagination.size), 0) + 1
|
||||
pagination_dict = {
|
||||
"size/total_count": [response.count, total_count],
|
||||
"page/total_page": [pagination.page, total_page_number],
|
||||
"order_field": pagination.order_field,
|
||||
"order_type": pagination.order_type,
|
||||
"size/total_count": [cls.result.count, total_count],
|
||||
"page/total_page": [filter_model.page, total_page_number],
|
||||
"order_field": filter_model.order_field,
|
||||
"order_type": filter_model.order_type,
|
||||
}
|
||||
include_joins = dict(
|
||||
include_joins=pagination.include_joins if pagination.include_joins else None
|
||||
include_joins=filter_model.include_joins if filter_model.include_joins else []
|
||||
)
|
||||
|
||||
if not isinstance(response.data[0], dict):
|
||||
data = []
|
||||
for obj in response.data:
|
||||
data_object = obj.get_dict(**include_joins)
|
||||
if response_model:
|
||||
data_object = response_model(
|
||||
**obj.get_dict(**include_joins)
|
||||
).model_dump()
|
||||
data.append(data_object)
|
||||
else:
|
||||
data = []
|
||||
for obj in response.data:
|
||||
data_object = obj
|
||||
if response_model:
|
||||
data_object = response_model(**obj).model_dump()
|
||||
data.append(data_object)
|
||||
|
||||
data = []
|
||||
for data_object in cls.result.data:
|
||||
data_dict = data_object.get_dict(include_joins=include_joins)
|
||||
if cls.response_model:
|
||||
data_dict = cls.response_model(**data_object.get_dict(include_joins=include_joins)).dump()
|
||||
data.append(data_dict)
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_200_OK,
|
||||
status_code=cls.status_code,
|
||||
content=dict(
|
||||
total_count=total_count,
|
||||
count=cls.result.count,
|
||||
pagination=pagination_dict,
|
||||
message="Found records are listed",
|
||||
completed=True,
|
||||
message=cls.message,
|
||||
completed=cls.completed,
|
||||
data=data,
|
||||
),
|
||||
)
|
||||
|
||||
return JSONResponse(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
content=dict(
|
||||
total_count=0,
|
||||
count=0,
|
||||
message="No record has found",
|
||||
completed=False,
|
||||
data=[],
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user