build area updated
This commit is contained in:
@@ -1,5 +1,153 @@
|
||||
import typing
|
||||
|
||||
from databases import (
|
||||
Build,
|
||||
BuildArea,
|
||||
)
|
||||
|
||||
from api_validations.validations_request import (
|
||||
InsertBuildArea,
|
||||
UpdateBuildArea,
|
||||
ListOptions,
|
||||
)
|
||||
|
||||
from api_validations.core_response import AlchemyJsonResponse
|
||||
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
|
||||
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
|
||||
|
||||
|
||||
class BuildingBuildAreaEvents(MethodToEvent): ...
|
||||
class BuildListEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "SELECT"
|
||||
__event_keys__ = {
|
||||
"0bb51845-65a2-4340-8872-a3b5aad95468": "build_area_list",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build_area_list(
|
||||
cls,
|
||||
list_options: ListOptions,
|
||||
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
if isinstance(token_dict, OccupantTokenObject):
|
||||
build_ids = Build.filter_all(
|
||||
Build.id == token_dict.selected_occupant.build_id,
|
||||
*Build.valid_record_args(Build),
|
||||
).data
|
||||
build_id_list = [build.id for build in build_ids]
|
||||
BuildArea.pre_query = BuildArea.filter_all(
|
||||
BuildArea.build_id.in_(build_id_list),
|
||||
*BuildArea.valid_record_args(BuildArea),
|
||||
).query
|
||||
elif isinstance(token_dict, EmployeeTokenObject):
|
||||
build_ids = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
)
|
||||
build_id_list = [build.id for build in build_ids]
|
||||
BuildArea.pre_query = BuildArea.filter_all(
|
||||
BuildArea.build_id.in_(build_id_list),
|
||||
*BuildArea.valid_record_args(BuildArea),
|
||||
).query
|
||||
BuildArea.filter_attr = list_options
|
||||
records = BuildArea.filter_all(
|
||||
*BuildArea.get_smart_query(smart_query=list_options.query),
|
||||
*BuildArea.valid_record_args(BuildArea),
|
||||
)
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
message="Update Build record",
|
||||
result=records
|
||||
)
|
||||
|
||||
|
||||
class BuildCreateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "CREATE"
|
||||
__event_keys__ = {
|
||||
"a10571fa-ac1d-4546-9272-cacb911d8004": "build_area_create",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build_area_create(
|
||||
cls,
|
||||
data: InsertBuildArea,
|
||||
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
|
||||
):
|
||||
if isinstance(token_dict, OccupantTokenObject):
|
||||
if not token_dict.selected_occupant.build_uuid == data.build_uu_id:
|
||||
BuildArea.raise_http_exception(
|
||||
status_code="HTTP_403_FORBIDDEN",
|
||||
error_case="UNAUTHORIZED",
|
||||
message=f"Occupant can not create build area for {data.build_uu_id}",
|
||||
data={
|
||||
"build_uu_id": data.build_uu_id,
|
||||
},
|
||||
)
|
||||
elif isinstance(token_dict, EmployeeTokenObject):
|
||||
build_ids = Build.select_action(
|
||||
employee_id=token_dict.selected_company.employee_id
|
||||
).all()
|
||||
if not str(data.build_uu_id) in [str(build.uu_id) for build in build_ids]:
|
||||
BuildArea.raise_http_exception(
|
||||
status_code="HTTP_403_FORBIDDEN",
|
||||
error_case="UNAUTHORIZED",
|
||||
message=f"Employee can not create build area for {data.build_uu_id}",
|
||||
data={
|
||||
"build_uu_id": data.build_uu_id,
|
||||
},
|
||||
)
|
||||
data_dict = data.excluded_dump()
|
||||
created_build_part = BuildArea.find_or_create(**data_dict)
|
||||
created_build_part.save()
|
||||
created_build_part.update(is_confirmed=True)
|
||||
created_build_part.save()
|
||||
return AlchemyJsonResponse(
|
||||
completed=True,
|
||||
message="Update Build record",
|
||||
result=created_build_part,
|
||||
)
|
||||
|
||||
class BuildUpdateEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "UPDATE"
|
||||
__event_keys__ = {
|
||||
"58178738-7489-4f8f-954e-5c8f083c1845": "build_area_update",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build_area_update(cls, build_uu_id: str, data: UpdateBuildArea, token_dict):
|
||||
return AlchemyJsonResponse(
|
||||
completed=False,
|
||||
message="Update Build record",
|
||||
result=None,
|
||||
)
|
||||
|
||||
|
||||
class BuildPatchEventMethods(MethodToEvent):
|
||||
|
||||
event_type = "PATCH"
|
||||
__event_keys__ = {
|
||||
"d6bd8a5f-fa76-49da-b82e-4a95f1bcce39": "build_area_patch",
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def build_area_patch(cls, build_uu_id: str, data, token_dict):
|
||||
return AlchemyJsonResponse(
|
||||
completed=False,
|
||||
message="Update Build record",
|
||||
result=None,
|
||||
)
|
||||
|
||||
|
||||
BuildListEventMethod = BuildListEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/area/list")
|
||||
)
|
||||
BuildCreateEventMethod = BuildCreateEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/area/create")
|
||||
)
|
||||
BuildUpdateEventMethod = BuildUpdateEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/area/update")
|
||||
)
|
||||
BuildPatchEventMethod = BuildPatchEventMethods(
|
||||
action=ActionsSchema(endpoint="/building/area/patch")
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user