first commit

This commit is contained in:
2024-11-07 17:44:29 +03:00
commit 643d6d8f65
247 changed files with 420800 additions and 0 deletions

View File

View File

@@ -0,0 +1,146 @@
from fastapi import status
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
from databases import (
BuildLivingSpace,
BuildParts,
Build,
Events,
Event2Occupant,
OccupantTypes,
)
from api_validations import RegisterEvents2Occupant
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from api_validations.core_response import return_json_response_from_alchemy
class EventBindOccupantEventMethods(MethodToEvent):
event_type = "UPDATE"
__event_keys__ = {
"5702f0a9-fe8f-4aae-922e-6e04b497ef6a": "bind_events_occupant_super_user",
}
@classmethod
def bind_events_occupant_super_user(
cls, data: RegisterEvents2Occupant, token_dict: EmployeeTokenObject
):
if not str(token_dict.user_type) == "1":
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="This employee is not authorized to add event to this occupant",
)
occupants_build_part = BuildParts.find_one(uu_id=data.build_part_uu_id)
if not occupants_build_part:
return JSONResponse(
content={
"completed": False,
"message": "This employee is not authorized to add event to this building",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
occupant_occupant_type = OccupantTypes.find_one(uu_id=data.occupant_uu_id)
if not occupant_occupant_type:
return JSONResponse(
content={
"completed": False,
"message": "Occupant Types is not found",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
events_to_add_to_occupant = Events.filter_active(
Events.uu_id.in_(list(data.event_uu_id_list))
)
if not events_to_add_to_occupant.data:
return JSONResponse(
content={
"completed": False,
"message": "Events are not found. Please contact with your manager",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
employee_is_authorized = Build.select_action(
employee_id=token_dict.selected_company.employee_id,
filter_expr=[Build.id == occupants_build_part.build_id],
)
if not employee_is_authorized.first():
return JSONResponse(
content={
"completed": False,
"data": {},
},
status_code=status.HTTP_401_UNAUTHORIZED,
)
occupant_to_add_event = BuildLivingSpace.find_one(
build_parts_id=occupants_build_part.id,
occupant_type=occupant_occupant_type.id,
)
if not occupant_to_add_event:
return JSONResponse(
content={
"completed": False,
"message": "Occupant is not found or this employee is not authorized to add event to this occupant",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
events_added = []
for event in events_to_add_to_occupant.data:
if Event2Occupant.find_or_create(
event_id=event.id,
event_uu_id=str(event.uu_id),
build_living_space_id=occupant_to_add_event.id,
build_living_space_uu_id=str(occupant_to_add_event.uu_id),
is_confirmed=True,
):
events_added.append(event)
if events_added:
return JSONResponse(
content={
"completed": True,
"message": "Event added to occupant",
"data": [event.get_dict() for event in events_added],
},
status_code=status.HTTP_200_OK,
)
return JSONResponse(
content={
"completed": False,
"message": "Event is not added to occupant",
"data": [],
},
status_code=status.HTTP_404_NOT_FOUND,
)
class EventBindEmployeeEventMethods(MethodToEvent):
event_type = "UPDATE"
__event_keys__ = {
"c93a3009-65a0-498d-9191-04484d5cde81": "bind_events_employee",
}
@classmethod
def bind_events_employee(cls, data: RegisterEvents2Occupant, token_dict):
return token_dict.available_event(data=data, token_dict=token_dict)
EventBindOccupantEventMethod = EventBindOccupantEventMethods(
action=ActionsSchema(endpoint="/bind/events/occupant")
)
EventBindEmployeeEventMethod = EventBindEmployeeEventMethods(
action=ActionsSchema(endpoint="/bind/events/employee")
)

View File

@@ -0,0 +1,337 @@
import typing
from fastapi import status
from fastapi.responses import JSONResponse
from fastapi.exceptions import HTTPException
from databases import (
Modules,
Employees,
BuildParts,
BuildLivingSpace,
Service2Events,
Services,
OccupantTypes,
Event2Employee,
Event2Occupant,
)
from api_validations import RegisterServices2Occupant, RegisterServices2Employee
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from api_validations.core_response import return_json_response_from_alchemy
class ServiceBindOccupantEventMethods(MethodToEvent):
event_type = "UPDATE"
__event_keys__ = {
"0d2bc5c9-d4b1-4951-8305-69da4a687fdc": "bind_services_occupant",
}
@classmethod
def bind_services_occupant_system(
cls, build_living_space_id: int, service_id: int, expires_at: str = None
):
from sqlalchemy.dialects.postgresql import insert
living_space = BuildLivingSpace.find_or_abort(
id=build_living_space_id,
)
service = Services.find_or_abort(
id=service_id,
)
default_module = Modules.find_one(module_code="USR-PUB")
add_default_service = Services.find_one(module_id=default_module.id)
service_events = Service2Events.filter_all(
Service2Events.service_id == service.id,
)
default_service_events = Service2Events.filter_all(
Service2Events.service_id == add_default_service.id,
)
add_events_list = service_events.data + default_service_events.data
if not add_events_list:
raise Exception(
"Service has no events registered. Please contact with your manager"
)
event_ids_list = [
{
"build_living_space_id": living_space.id,
"build_living_space_uu_id": str(living_space.uu_id),
"event_id": service_event.event_id,
"event_uu_id": str(service_event.event_uu_id),
"is_confirmed": True,
"expiry_ends": str(expires_at) if expires_at else "2099-12-31 03:00:00",
}
for service_event in add_events_list
]
session_execute = Services.session.execute(
insert(Event2Occupant)
.values(event_ids_list)
.on_conflict_do_nothing(
index_elements=["build_living_space_id", "event_id"],
)
)
count_row = session_execute.rowcount
print(f"{count_row} events are added to occupant {str(living_space.uu_id)}")
Services.session.commit()
Services.session.flush()
@classmethod
def bind_services_occupant(
cls,
data: RegisterServices2Occupant,
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
):
from sqlalchemy.dialects.postgresql import insert
if isinstance(token_dict, EmployeeTokenObject):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Employee is not authorized to add service to any occupant",
)
occupants_build_part = BuildParts.find_one(
uu_id=data.build_part_uu_id,
build_id=token_dict.selected_occupant.build_id,
)
print("occupants_build_part", occupants_build_part)
if not occupants_build_part:
return JSONResponse(
content={
"completed": False,
"message": "User is not authorized to add service to this occupant or flat",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
occupant_occupant_type = OccupantTypes.find_one(uu_id=data.occupant_uu_id)
if not occupant_occupant_type:
return JSONResponse(
content={
"completed": False,
"message": "Occupant Types is not found",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
service = Services.find_one(uu_id=data.service_uu_id)
if not service:
return JSONResponse(
content={
"completed": False,
"message": "Service is not found",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
default_module = Modules.find_one(module_code="USR-PUB")
add_default_service = Services.find_one(module_id=default_module.id)
service_events = Service2Events.filter_all(
Service2Events.service_id == service.id,
)
default_service_events = Service2Events.filter_all(
Service2Events.service_id == add_default_service.id,
)
add_events_list = service_events.data + default_service_events.data
if not add_events_list:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Service has no events registered. Please contact with your manager",
)
living_space = BuildLivingSpace.find_one(
build_parts_id=occupants_build_part.id,
occupant_types_id=occupant_occupant_type.id,
person_id=token_dict.person_id,
)
if not living_space:
return JSONResponse(
content={
"completed": False,
"message": "Living Space is not found with given data. Please check flat and occupant type",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
event_ids_list = [
{
"build_living_space_id": living_space.id,
"build_living_space_uu_id": str(living_space.uu_id),
"event_id": service_event.event_id,
"event_uu_id": str(service_event.event_uu_id),
"is_confirmed": True,
}
for service_event in add_events_list
]
session_execute = Services.session.execute(
insert(Event2Occupant)
.values(event_ids_list)
.on_conflict_do_nothing(
index_elements=["employee_id", "event_id"],
)
)
count_row = session_execute.rowcount
print(f"{count_row} events are added to employee {str(living_space.uu_id)}")
Services.session.commit()
Services.session.flush()
class ServiceBindEmployeeEventMethods(MethodToEvent):
event_type = "UPDATE"
__event_keys__ = {
"50f84023-d8ec-4257-bfce-08ddf077c101": "bind_services_employee_super_user",
}
@classmethod
def bind_services_employee(cls, service_id: int, employee_id: int):
from sqlalchemy.dialects.postgresql import insert
employee = Employees.find_or_abort(
id=employee_id,
)
service = Services.find_or_abort(
id=service_id,
)
default_module = Modules.find_one(module_code="USR-PUB")
add_default_service = Services.find_one(module_id=default_module.id)
service_events = Service2Events.filter_all(
Service2Events.service_id == service.id,
)
default_service_events = Service2Events.filter_all(
Service2Events.service_id == add_default_service.id,
)
add_events_list = service_events.data + default_service_events.data
if not add_events_list:
raise Exception(
"Service has no events registered. Please contact with your manager"
)
event_ids_list = [
{
"employee_id": employee_id,
"employee_uu_id": str(employee.uu_id),
"event_id": service_event.event_id,
"event_uu_id": str(service_event.event_uu_id),
"is_confirmed": True,
}
for service_event in add_events_list
]
session_execute = Services.session.execute(
insert(Event2Employee)
.values(event_ids_list)
.on_conflict_do_nothing(
index_elements=["employee_id", "event_id"],
)
)
count_row = session_execute.rowcount
print(f"{count_row} events are added to employee {employee.uu_id}")
Services.session.commit()
Services.session.flush()
@classmethod
def bind_services_employee_super_user(
cls,
data: RegisterServices2Employee,
token_dict: typing.Union[EmployeeTokenObject, OccupantTokenObject],
):
from sqlalchemy.dialects.postgresql import insert
if isinstance(token_dict, OccupantTokenObject):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Occupant is not authorized to add service to any employee",
)
employee = Employees.find_one(uu_id=data.employee_uu_id)
if not employee:
return JSONResponse(
content={
"completed": False,
"message": "This employee is not authorized to add event to this employee",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
service = Services.find_one(uu_id=data.service_uu_id)
if not service:
return JSONResponse(
content={
"completed": False,
"message": "Service is not found",
"data": {},
},
status_code=status.HTTP_404_NOT_FOUND,
)
service_events = Service2Events.filter_all(
Service2Events.service_id == service.id,
)
default_module = Modules.find_one(module_code="USR-PUB")
add_default_service = Services.find_one(module_id=default_module.id)
default_service_events = Service2Events.filter_all(
Service2Events.service_id == add_default_service.id,
)
add_events_list = service_events.data + default_service_events.data
if not add_events_list:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Service has no events registered. Please contact with your manager",
)
event_ids_list = [
{
"employee_id": employee.id,
"employee_uu_id": employee.uu_id,
"event_id": service_event.event_id,
"event_uu_id": service_event.event_uu_id,
"is_confirmed": True,
}
for service_event in add_events_list
]
session_execute = Services.session.execute(
insert(Event2Employee)
.values(event_ids_list)
.on_conflict_do_nothing(
index_elements=["employee_id", "event_id"],
)
)
count_row = session_execute.rowcount
Services.session.commit()
Services.session.flush()
if not count_row:
return JSONResponse(
content={
"completed": False,
"message": "No events are added to employee",
"data": {},
},
status_code=status.HTTP_200_OK,
)
return JSONResponse(
content={
"completed": True,
"message": f"{count_row} events are added to employee",
"data": {},
},
status_code=status.HTTP_200_OK,
)
ServiceBindOccupantEventMethod = ServiceBindOccupantEventMethods(
action=ActionsSchema(endpoint="/bind/services/occupant")
)
ServiceBindEmployeeEventMethod = ServiceBindEmployeeEventMethods(
action=ActionsSchema(endpoint="/bind/services/employee")
)

View File

@@ -0,0 +1,218 @@
from fastapi.exceptions import HTTPException
from databases import (
Events,
Employees,
Staff,
Duties,
Event2Occupant,
Event2Employee,
BuildLivingSpace,
)
from api_validations import (
RegisterEvents2Employee,
RegisterEvents2Occupant,
CreateEvents,
ListOptions,
)
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from api_validations.core_response import return_json_response_from_alchemy
class EventsListEventMethods(MethodToEvent):
event_type = "SELECT"
__event_keys__ = {
"9fa01bef-c0e8-4fe1-b9ed-2ff1c4f35faa": "events_list",
}
@classmethod
def events_list(cls, list_options: ListOptions, token_dict):
records = Events.filter_active(
*Events.get_smart_query(list_options.query),
)
return return_json_response_from_alchemy(
response=records, pagination=list_options
)
class EventsCreateEventMethods(MethodToEvent):
event_type = "CREATE"
__event_keys__ = {
"514a9f8f-e5e5-4e10-9d0b-2de8f461fc1b": "events_create",
}
@classmethod
def events_create(cls, data: CreateEvents, token_dict):
event = Events.find_or_create(
**token_dict.user_creds,
event_name=data.event_name,
event_description=data.event_description,
event_date=data.event_date,
event_location=data.event_location,
active=True,
deleted=False,
)
return {
"status": "success",
"message": "Event created successfully.",
"event": event.uu_id,
}
class EventsUpdateEventMethods(MethodToEvent):
event_type = "UPDATE"
__event_keys__ = {
"f94e7b79-2369-4840-bf2b-244934ca3136": "events_update",
}
@classmethod
def events_update(cls, data: CreateEvents, token_dict):
event = Events.find_one(uu_id=data.uu_id)
if not event:
raise HTTPException(
status_code=404,
detail="No event found. Please contact your responsible company.",
)
event.update(
**token_dict.user_creds,
event_name=data.event_name,
event_description=data.event_description,
event_date=data.event_date,
event_location=data.event_location,
)
return {
"status": "success",
"message": "Event updated successfully.",
"event": event.uu_id,
}
class EventsPatchEventMethods(MethodToEvent):
event_type = "PATCH"
__event_keys__ = {
"41944c63-22d3-4866-affd-34bcd49da58b": "events_patch",
}
@classmethod
def events_patch(cls, data: CreateEvents, token_dict):
event = Events.find_one(uu_id=data.uu_id)
if not event:
raise HTTPException(
status_code=404,
detail="No event found. Please contact your responsible company.",
)
event.update(
**token_dict.user_creds,
event_name=data.event_name,
event_description=data.event_description,
event_date=data.event_date,
event_location=data.event_location,
)
return {
"status": "success",
"message": "Event patched successfully.",
"event": event.uu_id,
}
class EventsBindEventToOccupantMethods(MethodToEvent):
event_type = "UPDATE"
__event_keys__ = {
"d9aa58aa-37f7-4c27-861d-3105f76f5cdc": "bind_events_employee",
}
@classmethod
def bind_events_employee(cls, data: RegisterEvents2Employee, token_dict):
events = Events.filter_active(Events.uu_id.in_(data.event_id))
if not events:
raise HTTPException(
status_code=401,
detail="No event found. Please contact your super user.",
)
employee = Employees.find_one(employee_uu_id=data.employee_uu_id)
employee_is_not_valid = False
if employee:
staff = Staff.find_one(id=employee.staff_id)
duties = Duties.find_one(id=staff.duties_id)
if duties.company_id not in token_dict.companies_id_list:
employee_is_not_valid = True
if employee_is_not_valid:
raise HTTPException(
status_code=401,
detail="This employee can not be reached by this user. Please contact your super user.",
)
for event in events.data:
employee = Event2Employee.find_or_create(
**token_dict.user_creds, employee_id=employee.id, event_id=event.id
)
return {
"status": "success",
"message": "Events registered successfully.",
"events": [event.uu_id for event in events.data],
}
class EventsBindEventToEmployeeMethods(MethodToEvent):
event_type = "UPDATE"
__event_keys__ = {
"8bb4f4fc-b474-427e-90b3-d8681f308bb5": "bind_events_occupant",
}
@classmethod
def bind_events_occupant(cls, data: RegisterEvents2Occupant, token_dict):
events = Events.filter_active(Events.uu_id.in_(data.event_id))
if not events:
raise HTTPException(
status_code=401,
detail="No event found. Please contact your super user.",
)
occupant = BuildLivingSpace.find_one(uu_id=data.build_living_space_uu_id)
occupant_is_not_valid = False
if occupant_is_not_valid:
raise HTTPException(
status_code=401,
detail="This occupant can not be reached by this user. Please contact your super user.",
)
for event in events.data:
occupant = Event2Occupant.find_or_create(
**token_dict.user_creds,
build_living_space_id=occupant.id,
event_id=event.id,
)
return {
"status": "success",
"message": "Events registered successfully.",
"events": [event.uu_id for event in events.data],
}
EventsListEventMethod = EventsListEventMethods(
action=ActionsSchema(endpoint="/event/list")
)
EventsCreateEventMethod = EventsCreateEventMethods(
action=ActionsSchema(endpoint="/event/create")
)
EventsUpdateEventMethod = EventsUpdateEventMethods(
action=ActionsSchema(endpoint="/event/update")
)
EventsPatchEventMethod = EventsPatchEventMethods(
action=ActionsSchema(endpoint="/event/patch")
)
EventsBindEventToOccupantMethod = EventsBindEventToOccupantMethods(
action=ActionsSchema(endpoint="/bind/events/occupant")
)
EventsBindEventToEmployeeMethod = EventsBindEventToEmployeeMethods(
action=ActionsSchema(endpoint="/bind/events/employee")
)

View File

@@ -0,0 +1,85 @@
from fastapi import status
from fastapi.responses import JSONResponse
from api_validations import DepartmentsPydantic, ListOptions
from databases import Events
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from api_validations.core_response import return_json_response_from_alchemy
class ModelEntitiesEvents(MethodToEvent):
@classmethod
def model_entities_list(cls, list_options: ListOptions, token_dict):
Events.filter_attr = list_options
records = Events.filter_active(
*Events.get_smart_query(smart_query=list_options.query),
Events.company_id == token_dict.selected_company.company_id
)
return return_json_response_from_alchemy(
response=records, pagination=list_options
)
@classmethod
def model_entities_create(cls, data: DepartmentsPydantic, token_dict):
created_events = Events.create_action(data=data, token=token_dict)
if created_events.is_found:
return JSONResponse(
content={
"completed": False,
"message": "Events record already exits here is the record",
"data": created_events.get_dict(),
},
status_code=status.HTTP_202_ACCEPTED,
)
return JSONResponse(
content={
"completed": True,
"message": "Create Events record",
"data": created_events.get_dict(),
},
status_code=status.HTTP_200_OK,
)
@classmethod
def model_entities_update(
cls, company_uu_id: str, data: DepartmentsPydantic, token_dict
):
find_one_events = Events.find_one_or_abort(uu_id=company_uu_id)
access_authorized_events = Events.select_action(
duty_id=getattr(token_dict, "duty_id", 5),
filter_expr=[Events.id == token_dict.get("")],
action="update",
data=data,
)
return JSONResponse(
content={
"completed": True,
"message": "Update Events record",
"data": access_authorized_events.get_dict(),
},
status_code=status.HTTP_200_OK,
)
@classmethod
def model_entities_patch(
cls, company_uu_id: str, data: DepartmentsPydantic, token_dict
):
find_one_events = Events.find_one_or_abort(uu_id=company_uu_id)
access_authorized_events = Events.select_action(
duty_id=getattr(token_dict, "duty_id", 5),
filter_expr=[Events.id == token_dict.get("")],
action="patch",
data=data,
)
return JSONResponse(
content={
"completed": True,
"message": "Patch Events record",
"data": access_authorized_events.get_dict(),
},
status_code=status.HTTP_200_OK,
)

View File

@@ -0,0 +1,24 @@
from validations import DepartmentsPydantic, PatchRecord, ListOptions
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from api_validations.core_response import return_json_response_from_alchemy
class ModelEvents(MethodToEvent):
@classmethod
def model_list(cls, list_options: ListOptions, token_dict):
return
@classmethod
def model_create(cls, data: DepartmentsPydantic, token_dict):
return
@classmethod
def model_update(cls, company_uu_id: str, data: DepartmentsPydantic, token_dict):
return
@classmethod
def model_patch(cls, company_uu_id: str, data: PatchRecord, token_dict):
return

View File

@@ -0,0 +1,24 @@
from validations import DepartmentsPydantic, PatchRecord
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from api_validations.core_response import return_json_response_from_alchemy
class ModulesEvents(MethodToEvent):
@classmethod
def modules_list(cls, request, list_options):
return
@classmethod
def modules_create(cls, data: DepartmentsPydantic, token_dict):
return
@classmethod
def modules_update(cls, module_uu_id: str, data: DepartmentsPydantic, token_dict):
return
@classmethod
def modules_patch(cls, module_uu_id: str, data: PatchRecord, token_dict):
return

View File

@@ -0,0 +1,36 @@
from validations import DepartmentsPydantic, PatchRecord, ListOptions
from api_events.events.abstract_class import MethodToEvent, ActionsSchema
from api_objects.auth.token_objects import EmployeeTokenObject, OccupantTokenObject
from api_validations.core_response import return_json_response_from_alchemy
class ServicesEvents(MethodToEvent):
@classmethod
def services_list(cls, list_options: ListOptions):
return
@classmethod
def services_create(cls, data: DepartmentsPydantic, token_dict):
return
@classmethod
def services_update(cls, service_uu_id: str, data: DepartmentsPydantic, token_dict):
return
@classmethod
def services_patch(cls, service_uu_id: str, data: PatchRecord, token_dict):
return
@classmethod
def bind_service_to_action(cls, data, token_dict):
return
@classmethod
def bind_module_to_service(cls, data, token_dict):
return
@classmethod
def bind_events_patch(cls, company_uu_id: str, data: PatchRecord):
return