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=[],
|
||||
),
|
||||
)
|
||||
|
||||
@@ -6,18 +6,18 @@ from api_validations.validations_request import (
|
||||
)
|
||||
|
||||
|
||||
class DecisionBookDecisionBookInvitations(PydanticBaseModel):
|
||||
class DecisionBookDecisionBookInvitations(BaseModelRegular):
|
||||
build_decision_book_uu_id: str
|
||||
message: str
|
||||
planned_date: str
|
||||
|
||||
|
||||
class DecisionBookDecisionBookInvitationsAttend(PydanticBaseModel):
|
||||
class DecisionBookDecisionBookInvitationsAttend(BaseModelRegular):
|
||||
token: str
|
||||
is_attend: bool
|
||||
|
||||
|
||||
class DecisionBookDecisionBookInvitationsAssign(PydanticBaseModel):
|
||||
class DecisionBookDecisionBookInvitationsAssign(BaseModelRegular):
|
||||
token: str
|
||||
build_living_space_uu_id: str
|
||||
occupant_type_uu_id: str
|
||||
@@ -28,7 +28,7 @@ class DecisionBookDecisionBookInvitationsUpdate(PydanticBaseModel):
|
||||
occupant_type_uu_id: Optional[str] = None
|
||||
|
||||
|
||||
class ListDecisionBook(PydanticBaseModel):
|
||||
class ListDecisionBook(ListOptions, PydanticBaseModel):
|
||||
build_decision_book_uu_id: Optional[str] = None
|
||||
|
||||
|
||||
@@ -42,12 +42,12 @@ class InsertDecisionBook(PydanticBaseModel):
|
||||
resp_company_uu_id: Optional[str] = None
|
||||
|
||||
|
||||
class InsertDecisionBookCompleted(PydanticBaseModel):
|
||||
class InsertDecisionBookCompleted(BaseModelRegular):
|
||||
build_decision_book_uu_id: str
|
||||
meeting_completed_date: str
|
||||
|
||||
|
||||
class InsertDecisionBookPerson(PydanticBaseModel):
|
||||
class InsertDecisionBookPerson(BaseModelRegular):
|
||||
person_uu_id: str
|
||||
build_decision_book_uu_id: str
|
||||
management_typecode_uu_id: str
|
||||
@@ -74,7 +74,7 @@ class UpdateDecisionBook(PydanticBaseModel):
|
||||
resp_company_uu_id: Optional[str] = None
|
||||
|
||||
|
||||
class InsertBuildDecisionBookItems(PydanticBaseModel):
|
||||
class InsertBuildDecisionBookItems(BaseModelRegular):
|
||||
token: str
|
||||
info_type_uu_id: str
|
||||
unit_price: float
|
||||
@@ -94,7 +94,7 @@ class UpdateBuildDecisionBookItems(PydanticBaseModel):
|
||||
item_objection: Optional[str] = None
|
||||
|
||||
|
||||
class InsertBuildDecisionBookItemDebits(PydanticBaseModel):
|
||||
class InsertBuildDecisionBookItemDebits(BaseModelRegular):
|
||||
build_decision_book_item_uu_id: str
|
||||
dues_values: dict
|
||||
# dues_types_uu_id: str
|
||||
|
||||
Reference in New Issue
Block a user