alchemy functions updated

This commit is contained in:
berkay 2024-11-10 13:36:52 +03:00
parent eb947ecb3d
commit be100a6615
18 changed files with 83 additions and 61 deletions

View File

@ -13,7 +13,7 @@ class ActionsSchema(ABC):
def retrieve_action_from_endpoint(self): def retrieve_action_from_endpoint(self):
from databases import EndpointRestriction from databases import EndpointRestriction
endpoint_restriction = EndpointRestriction.filter_all( endpoint_restriction = EndpointRestriction.filter_one(
EndpointRestriction.endpoint_name.ilike(f"%{self.endpoint}%") EndpointRestriction.endpoint_name.ilike(f"%{self.endpoint}%")
).get(1) ).get(1)
if not endpoint_restriction: if not endpoint_restriction:

View File

@ -421,7 +421,7 @@ class AddressPostCodeListEventMethods(MethodToEvent):
records = AddressPostcode.filter_all( records = AddressPostcode.filter_all(
*Addresses.get_smart_query(list_options.query), *Addresses.get_smart_query(list_options.query),
*Addresses.valid_record_args(AddressPostcode), *Addresses.valid_record_args(AddressPostcode),
) ).data
return AlchemyJsonResponse( return AlchemyJsonResponse(
completed=True, completed=True,
message="List Address records", message="List Address records",

View File

@ -559,7 +559,7 @@ class AuthenticationRefreshTokenEventMethods(MethodToEvent):
): ):
token_refresher = UsersTokens.filter_by_one( token_refresher = UsersTokens.filter_by_one(
token=data.refresh_token, domain=data.domain, *UsersTokens.valid_record_dict token=data.refresh_token, domain=data.domain, *UsersTokens.valid_record_dict
) ).data
if not token_refresher: if not token_refresher:
return JSONResponse( return JSONResponse(
content={"completed": False, "message": "Invalid data", "data": {}}, content={"completed": False, "message": "Invalid data", "data": {}},

View File

@ -188,13 +188,13 @@ class AuthenticationSelectEventMethods(MethodToEvent):
status_code=status.HTTP_200_OK, status_code=status.HTTP_200_OK,
) )
elif token_user.user_type == 2: elif token_user.user_type == 2:
occupant_type = OccupantTypes.filter_by_one(uu_id=data.occupant_uu_id) occupant_type = OccupantTypes.filter_by_one(uu_id=data.occupant_uu_id).data
if not occupant_type: if not occupant_type:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
detail="Occupant Type is not found", detail="Occupant Type is not found",
) )
build_part = BuildParts.filter_by_one(uu_id=data.build_part_uu_id) build_part = BuildParts.filter_by_one(uu_id=data.build_part_uu_id).data
if not build_part: if not build_part:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,

View File

@ -49,7 +49,7 @@ class BuildingLivingSpacesPartsListEventMethods(MethodToEvent):
), ),
*BuildLivingSpace.get_smart_query(smart_query=list_options.query), *BuildLivingSpace.get_smart_query(smart_query=list_options.query),
*BuildLivingSpace.valid_record_args(BuildLivingSpace), *BuildLivingSpace.valid_record_args(BuildLivingSpace),
) ).data
return AlchemyJsonResponse( return AlchemyJsonResponse(
completed=True, completed=True,
message="Building Living Spaces are listed successfully", message="Building Living Spaces are listed successfully",

View File

@ -134,7 +134,7 @@ class DecisionBookCreateEventMethods(MethodToEvent):
elif isinstance(token_dict, OccupantTokenObject): elif isinstance(token_dict, OccupantTokenObject):
occupant_manager = OccupantTypes.filter_by_one( occupant_manager = OccupantTypes.filter_by_one(
occupant_category_type="BU", occupant_code="BU-MNG" occupant_category_type="BU", occupant_code="BU-MNG"
) ).data
if not token_dict.selected_occupant.occupant_type_id == occupant_manager.id: if not token_dict.selected_occupant.occupant_type_id == occupant_manager.id:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN, status_code=status.HTTP_403_FORBIDDEN,

View File

@ -281,6 +281,7 @@ class DecisionBookPersonAssignOccupantEventMethods(MethodToEvent):
build_parts_of_token = BuildParts.filter_all( build_parts_of_token = BuildParts.filter_all(
BuildParts.build_id == token_dict.selected_occupant.build_id, BuildParts.build_id == token_dict.selected_occupant.build_id,
*BuildParts.valid_record_args(BuildParts)
).data ).data
selected_living_space = BuildLivingSpace.filter_one( selected_living_space = BuildLivingSpace.filter_one(
BuildLivingSpace.uu_id == data.build_living_space_uu_id, BuildLivingSpace.uu_id == data.build_living_space_uu_id,

View File

@ -136,8 +136,8 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
# Get all the parts of the building that is occupant in token # Get all the parts of the building that is occupant in token
build_parts = BuildParts.filter_all( build_parts = BuildParts.filter_all(
BuildParts.build_id == occupant_building.id, BuildParts.active == True BuildParts.build_id == occupant_building.id, *BuildParts.valid_record_args(BuildParts)
).data )
# Get all build living spaces that is found in building with distinct person id # Get all build living spaces that is found in building with distinct person id
occupants = OccupantTypes.filter_all(system=True) occupants = OccupantTypes.filter_all(system=True)
@ -217,8 +217,8 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
manager_living_spaces = BuildLivingSpace.filter_all( manager_living_spaces = BuildLivingSpace.filter_all(
BuildLivingSpace.person_id == token_dict.person_id, BuildLivingSpace.person_id == token_dict.person_id,
BuildLivingSpace.active == True, *BuildLivingSpace.valid_record_args(BuildLivingSpace)
).data )
manager_people = BuildDecisionBookPerson.filter_all( manager_people = BuildDecisionBookPerson.filter_all(
BuildDecisionBookPerson.invite_id == book_invitation.id, BuildDecisionBookPerson.invite_id == book_invitation.id,
BuildDecisionBookPerson.build_living_space_id.in_( BuildDecisionBookPerson.build_living_space_id.in_(
@ -227,12 +227,12 @@ class BuildDecisionBookInvitationsCreateEventMethods(MethodToEvent):
for manager_living_space in manager_living_spaces.data for manager_living_space in manager_living_spaces.data
] ]
), ),
BuildLivingSpace.active == True, *BuildLivingSpace.valid_record_args(BuildLivingSpace)
) )
manager_people_occupants = BuildDecisionBookPersonOccupants.filter_all( manager_people_occupants = BuildDecisionBookPersonOccupants.filter_all(
BuildDecisionBookPersonOccupants.build_decision_book_person_id BuildDecisionBookPersonOccupants.build_decision_book_person_id
== manager_people.get(1).id, == manager_people.get(1).id,
BuildDecisionBookPersonOccupants.active == True, *BuildDecisionBookPersonOccupants.valid_record_args(BuildDecisionBookPersonOccupants)
) )
manager_people_occupants.query.delete() manager_people_occupants.query.delete()
manager_people.query.delete() manager_people.query.delete()

View File

@ -66,7 +66,7 @@ class EventBindOccupantEventMethods(MethodToEvent):
events_to_add_to_occupant = Events.filter_all( events_to_add_to_occupant = Events.filter_all(
Events.uu_id.in_(list(data.event_uu_id_list)), Events.uu_id.in_(list(data.event_uu_id_list)),
Events.active == True, *Events.valid_record_args(Events)
) )
if not events_to_add_to_occupant.data: if not events_to_add_to_occupant.data:
return JSONResponse( return JSONResponse(

View File

@ -38,15 +38,15 @@ class ServiceBindOccupantEventMethods(MethodToEvent):
living_space = BuildLivingSpace.filter_one( living_space = BuildLivingSpace.filter_one(
BuildLivingSpace.id == build_living_space_id, BuildLivingSpace.id == build_living_space_id,
BuildLivingSpace.active == True, *BuildLivingSpace.valid_record_args(BuildLivingSpace)
).data ).data
service = Services.filter_one( service = Services.filter_one(
Services.id == service_id, Services.id == service_id,
Services.active == True, *Services.valid_record_args(Services)
).data ).data
add_events_list = Service2Events.filter_all( add_events_list = Service2Events.filter_all(
Service2Events.service_id == service.id, Service2Events.service_id == service.id,
Service2Events.active == True, *Service2Events.valid_record_args(Service2Events)
).data ).data
if not add_events_list: if not add_events_list:
raise Exception( raise Exception(
@ -106,7 +106,7 @@ class ServiceBindOccupantEventMethods(MethodToEvent):
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
) )
occupant_occupant_type = OccupantTypes.filter_by_one(uu_id=data.occupant_uu_id) occupant_occupant_type = OccupantTypes.filter_by_one(uu_id=data.occupant_uu_id).data
if not occupant_occupant_type: if not occupant_occupant_type:
return JSONResponse( return JSONResponse(
content={ content={
@ -130,7 +130,7 @@ class ServiceBindOccupantEventMethods(MethodToEvent):
service_events = Service2Events.filter_all( service_events = Service2Events.filter_all(
Service2Events.service_id == service.id, Service2Events.service_id == service.id,
Service2Events.active == True, Service2Events.valid_record_args(Service2Events),
).data ).data
if not service_events: if not service_events:
raise HTTPException( raise HTTPException(
@ -142,7 +142,7 @@ class ServiceBindOccupantEventMethods(MethodToEvent):
BuildLivingSpace.build_parts_id == occupants_build_part.id, BuildLivingSpace.build_parts_id == occupants_build_part.id,
BuildLivingSpace.occupant_types_id == occupant_occupant_type.id, BuildLivingSpace.occupant_types_id == occupant_occupant_type.id,
BuildLivingSpace.person_id == token_dict.person_id, BuildLivingSpace.person_id == token_dict.person_id,
BuildLivingSpace.active == True, *BuildLivingSpace.valid_record_args(BuildLivingSpace)
).data ).data
if not living_space: if not living_space:
return JSONResponse( return JSONResponse(
@ -188,11 +188,11 @@ class ServiceBindEmployeeEventMethods(MethodToEvent):
def bind_services_employee(cls, service_id: int, employee_id: int): def bind_services_employee(cls, service_id: int, employee_id: int):
from sqlalchemy.dialects.postgresql import insert from sqlalchemy.dialects.postgresql import insert
employee = Employees.filter_by_one(id=employee_id, *Employees.valid_record_dict) employee = Employees.filter_by_one(id=employee_id, *Employees.valid_record_dict).data
service = Services.filter_by_one(id=service_id, *Services.valid_record_dict) service = Services.filter_by_one(id=service_id, *Services.valid_record_dict).data
service_events = Service2Events.filter_all( service_events = Service2Events.filter_all(
Service2Events.service_id == service.id, Service2Events.service_id == service.id,
Service2Events.active == True, *Service2Events.valid_record_args(Service2Events),
).data ).data
if not service_events: if not service_events:
raise Exception( raise Exception(
@ -263,7 +263,7 @@ class ServiceBindEmployeeEventMethods(MethodToEvent):
service_events = Service2Events.filter_all( service_events = Service2Events.filter_all(
Service2Events.service_id == service.id, Service2Events.service_id == service.id,
Service2Events.active == True, *Service2Events.valid_record_args(Service2Events)
).data ).data
if not service_events: if not service_events:
raise HTTPException( raise HTTPException(

View File

@ -83,7 +83,7 @@ class EventsUpdateEventMethods(MethodToEvent):
@classmethod @classmethod
def events_update(cls, data: CreateEvents, token_dict): def events_update(cls, data: CreateEvents, token_dict):
event = Events.filter_by_one(uu_id=data.uu_id, *Events.valid_record_dict) event = Events.filter_by_one(uu_id=data.uu_id, *Events.valid_record_dict).data
if not event: if not event:
raise HTTPException( raise HTTPException(
status_code=404, status_code=404,
@ -113,7 +113,7 @@ class EventsPatchEventMethods(MethodToEvent):
@classmethod @classmethod
def events_patch(cls, data: CreateEvents, token_dict): def events_patch(cls, data: CreateEvents, token_dict):
event = Events.filter_by_one(uu_id=data.uu_id, *Events.valid_record_dict) event = Events.filter_by_one(uu_id=data.uu_id, *Events.valid_record_dict).data
if not event: if not event:
raise HTTPException( raise HTTPException(
status_code=404, status_code=404,

View File

@ -60,7 +60,7 @@ class UserListEventMethods(MethodToEvent):
records = Users.filter_all( records = Users.filter_all(
*Users.get_smart_query(list_options.query), *Users.get_smart_query(list_options.query),
*Users.valid_record_args(Users), *Users.valid_record_args(Users),
) ).data
return AlchemyJsonResponse( return AlchemyJsonResponse(
completed=True, completed=True,
message="Users are listed successfully", message="Users are listed successfully",

View File

@ -124,11 +124,14 @@ class BuildDecisionBook(CrudCollection):
@classmethod @classmethod
def select_action(cls, duty_id, token=None): def select_action(cls, duty_id, token=None):
related_companies = Companies.select_action(duty_id=duty_id) related_companies = Companies.select_action(duty_id_list=[int(duty_id)])
related_companies_ids = list( related_companies_ids = list(
related_.id for related_ in related_companies.all() related_.id for related_ in related_companies.all()
) )
related_building = Build.filter_all(Build.company_id.in_(related_companies_ids)) related_building = Build.filter_all(
Build.company_id.in_(related_companies_ids)
*Build.valid_record_args(Build),
)
related_building_ids = list(related_.id for related_ in related_building.data) related_building_ids = list(related_.id for related_ in related_building.data)
return cls.filter_all(cls.build_id.in_(related_building_ids)).query return cls.filter_all(cls.build_id.in_(related_building_ids)).query
@ -158,11 +161,12 @@ class BuildDecisionBook(CrudCollection):
expiry_ends.replace(month=expiry_ends.month + 1, day=1) - timedelta(days=1) expiry_ends.replace(month=expiry_ends.month + 1, day=1) - timedelta(days=1)
) )
if decision_book := BuildDecisionBook.filter_all( if decision_book := BuildDecisionBook.filter_one(
BuildDecisionBook.build_id == building.id, BuildDecisionBook.build_id == building.id,
BuildDecisionBook.expiry_ends > data_dict["expiry_starts"], BuildDecisionBook.expiry_ends > data_dict["expiry_starts"],
BuildDecisionBook.decision_type == data_dict.get("decision_type"), BuildDecisionBook.decision_type == data_dict.get("decision_type"),
).count: # Decision book is already exist: *BuildDecisionBook.valid_record_args(BuildDecisionBook),
).data: # Decision book is already exist:
cls.raise_http_exception( cls.raise_http_exception(
status_code=status.HTTP_409_CONFLICT, status_code=status.HTTP_409_CONFLICT,
error_case="RECORDEXITS", error_case="RECORDEXITS",
@ -418,7 +422,7 @@ class BuildDecisionBookPerson(CrudCollection):
related_service = Services.filter_by_one( related_service = Services.filter_by_one(
related_responsibility=str(occupant_type.occupant_code), related_responsibility=str(occupant_type.occupant_code),
*Services.valid_record_dict, *Services.valid_record_dict,
) ).data
if not related_service: if not related_service:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, status_code=status.HTTP_404_NOT_FOUND,
@ -472,8 +476,9 @@ class BuildDecisionBookPerson(CrudCollection):
def get_occupant_types(self): def get_occupant_types(self):
if occupants := BuildDecisionBookPersonOccupants.filter_all( if occupants := BuildDecisionBookPersonOccupants.filter_all(
BuildDecisionBookPersonOccupants.build_decision_book_person_id == self.id, BuildDecisionBookPersonOccupants.build_decision_book_person_id == self.id,
): *BuildDecisionBookPersonOccupants.valid_record_args(BuildDecisionBookPersonOccupants)
return occupants.data ).data:
return occupants
return return
def check_occupant_type(self, occupant_type): def check_occupant_type(self, occupant_type):
@ -1028,29 +1033,35 @@ class BuildDecisionBookProjects(CrudCollection):
@classmethod @classmethod
def select_action(cls, duty_id, token=None): def select_action(cls, duty_id, token=None):
related_companies = Companies.select_action(duty_id=duty_id) related_companies = Companies.select_action(duty_id_list=[duty_id])
related_companies_ids = list( related_companies_ids = list(
related_.id for related_ in related_companies.all() related_.id for related_ in related_companies.all()
) )
related_building = Build.filter_all(Build.company_id.in_(related_companies_ids)) related_building = Build.filter_all(
Build.company_id.in_(related_companies_ids),
*Build.valid_record_args(Build)
)
related_building_ids = list(related_.id for related_ in related_building.data) related_building_ids = list(related_.id for related_ in related_building.data)
related_decision_books = BuildDecisionBook.query.filter( related_decision_books = BuildDecisionBook.filter_all(
BuildDecisionBook.build_id.in_(related_building_ids) BuildDecisionBook.build_id.in_(related_building_ids),
) *BuildDecisionBook.valid_record_args(BuildDecisionBook)
).data
related_decision_books_ids = list( related_decision_books_ids = list(
related_.id for related_ in related_decision_books.all() related_.id for related_ in related_decision_books
) )
related_decision_books_items = BuildDecisionBookItems.query.filter( related_decision_books_items = BuildDecisionBookItems.filter_all(
BuildDecisionBookItems.build_decision_book_id.in_( BuildDecisionBookItems.build_decision_book_id.in_(
related_decision_books_ids related_decision_books_ids
) ),
) *BuildDecisionBookItems.valid_record_args(BuildDecisionBookItems)
).data
related_decision_books_items_ids = list( related_decision_books_items_ids = list(
related_.id for related_ in related_decision_books_items.all() related_.id for related_ in related_decision_books_items
)
return cls.query.filter(
cls.build_decision_book_item_id.in_(related_decision_books_items_ids)
) )
return cls.filter_all(
cls.build_decision_book_item_id.in_(related_decision_books_items_ids),
*cls.valid_record_args(cls)
).query
@classmethod @classmethod
def create_action(cls, data: InsertBuildDecisionBookProjects, token=None): def create_action(cls, data: InsertBuildDecisionBookProjects, token=None):

View File

@ -175,9 +175,9 @@ class CrudMixin(Base, SmartQueryMixin, SessionMixin, FilterAttributes):
""" """
check_kwargs = cls.extract_system_fields(kwargs) check_kwargs = cls.extract_system_fields(kwargs)
cls.pre_query = cls.query.filter(cls.expiry_ends < system_arrow.now().date()) cls.pre_query = cls.query.filter(cls.expiry_ends < system_arrow.now().date())
already_record = cls.filter_by_one(**check_kwargs, system=True) already_record = cls.filter_by_one(**check_kwargs, system=True).data
cls.pre_query = None cls.pre_query = None
if already_record := already_record.data: if already_record:
if already_record.deleted: if already_record.deleted:
cls.raise_http_exception( cls.raise_http_exception(
status_code="HTTP_406_NOT_ACCEPTABLE", status_code="HTTP_406_NOT_ACCEPTABLE",

View File

@ -68,8 +68,11 @@ class Modules(CrudCollection):
is_default_module = mapped_column(Boolean, server_default="0") is_default_module = mapped_column(Boolean, server_default="0")
def retrieve_services(self): def retrieve_services(self):
services = Services.filter_all(Services.module_id == self.id) services = Services.filter_all(
if not services.count: Services.module_id == self.id,
*Services.valid_record_args(Services)
).data
if not services:
self.raise_http_exception( self.raise_http_exception(
status_code="HTTP_404_NOT_FOUND", status_code="HTTP_404_NOT_FOUND",
error_case="RECORD_NOT_FOUND", error_case="RECORD_NOT_FOUND",
@ -78,7 +81,7 @@ class Modules(CrudCollection):
"module_uu_id": str(self.uu_id), "module_uu_id": str(self.uu_id),
}, },
) )
return services.data return services
__table_args__ = ({"comment": "Modules Information"},) __table_args__ = ({"comment": "Modules Information"},)
@ -143,9 +146,14 @@ class Event2Employee(CrudCollection):
@classmethod @classmethod
def get_event_id_by_employee_id(cls, employee_id) -> (list, list): def get_event_id_by_employee_id(cls, employee_id) -> (list, list):
active_events = cls.filter_all(cls.employee_id == employee_id) active_events = cls.filter_all(
cls.employee_id == employee_id, *cls.valid_record_args(cls)
)
active_events_id = [event.event_id for event in active_events.data] active_events_id = [event.event_id for event in active_events.data]
active_events = Events.filter_all(Events.id.in_(active_events_id)) active_events = Events.filter_all(
Events.id.in_(active_events_id),
*Events.valid_record_args(Events)
)
active_events_uu_id = [str(event.uu_id) for event in active_events.data] active_events_uu_id = [str(event.uu_id) for event in active_events.data]
return active_events_id, active_events_uu_id return active_events_id, active_events_uu_id

View File

@ -496,7 +496,7 @@ class Addresses(CrudCollection):
post_code_list = RelationshipEmployee2PostCode.filter_all( post_code_list = RelationshipEmployee2PostCode.filter_all(
RelationshipEmployee2PostCode.employee_id RelationshipEmployee2PostCode.employee_id
== token_dict.selected_company.employee_id, == token_dict.selected_company.employee_id,
RelationshipEmployee2PostCode.active == True, *RelationshipEmployee2PostCode.valid_record_args(RelationshipEmployee2PostCode)
).data ).data
post_code_id_list = [post_code.member_id for post_code in post_code_list] post_code_id_list = [post_code.member_id for post_code in post_code_list]
if not post_code_id_list: if not post_code_id_list:
@ -504,10 +504,12 @@ class Addresses(CrudCollection):
status_code=404, status_code=404,
detail="User has no post code registered. User can not list addresses.", detail="User has no post code registered. User can not list addresses.",
) )
cls.pre_query = cls.filter_active(cls.post_code_id.in_(post_code_id_list)).query cls.pre_query = cls.filter_all(
cls.post_code_id.in_(post_code_id_list), cls.valid_record_args(cls)
).query
filter_cls = cls.filter_all(*filter_expr) filter_cls = cls.filter_all(*filter_expr)
cls.pre_query = None cls.pre_query = None
return filter_cls return filter_cls.data
# buildings: Mapped["Build"] = relationship( # buildings: Mapped["Build"] = relationship(
# "Build", back_populates="addresses", foreign_keys="Build.address_id" # "Build", back_populates="addresses", foreign_keys="Build.address_id"

View File

@ -96,15 +96,15 @@ def add_events_all_services_and_occupant_types():
def add_events_to_system_super_user(): def add_events_to_system_super_user():
add_service = Services.filter_by_one(service_code="SRE-SUE", **Services.valid_record_dict) add_service = Services.filter_by_one(service_code="SRE-SUE", **Services.valid_record_dict).data
if not add_service: if not add_service:
raise Exception("Service not found") raise Exception("Service not found")
find_staff = Staff.filter_by_one(staff_code="SUE", **Staff.valid_record_dict) find_staff = Staff.filter_by_one(staff_code="SUE", **Staff.valid_record_dict).data
if not find_staff: if not find_staff:
raise Exception("Super User not found") raise Exception("Super User not found")
add_employee = Employees.filter_by_one(staff_id=find_staff.id, **Employees.valid_record_dict) add_employee = Employees.filter_by_one(staff_id=find_staff.id, **Employees.valid_record_dict).data
if not add_employee: if not add_employee:
raise Exception("Super User Employee not found") raise Exception("Super User Employee not found")

View File

@ -202,7 +202,7 @@ def create_application_defaults():
company_uu_id=str(company_management.uu_id), company_uu_id=str(company_management.uu_id),
**Departments.valid_record_dict, **Departments.valid_record_dict,
is_confirmed=True, is_confirmed=True,
) ).data
Duty.find_or_create( Duty.find_or_create(
**dict( **dict(