new api service and logic implemented

This commit is contained in:
2025-01-23 22:27:25 +03:00
parent d91ecda9df
commit 32022ca521
245 changed files with 28004 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
import enum
from typing import Optional, List, Any
from pydantic import BaseModel
# Company / Priority / Department / Duty / Employee / Occupant / Module / Endpoint are changeable dynamics
class UserType(enum.Enum):
employee = 1
occupant = 2
class Credentials(BaseModel):
person_id: int
person_name: str
class ApplicationToken(BaseModel):
# Application Token Object -> is the main object for the user
domain: Optional[str] = "app.evyos.com.tr"
lang: Optional[str] = "TR"
timezone: Optional[str] = "GMT+3"
user_type: int = UserType.occupant.value
credentials: dict = None
user_uu_id: str
user_id: int
person_id: int
person_uu_id: str
request: Optional[dict] = None # Request Info of Client
expires_at: Optional[float] = None # Expiry timestamp
class OccupantToken(BaseModel):
# Selection of the occupant type for a build part is made by the user
living_space_id: int # Internal use
living_space_uu_id: str # Outer use
occupant_type_id: int
occupant_type_uu_id: str
occupant_type: str
build_id: int
build_uuid: str
build_part_id: int
build_part_uuid: str
responsible_company_id: Optional[int] = None
responsible_company_uuid: Optional[str] = None
responsible_employee_id: Optional[int] = None
responsible_employee_uuid: Optional[str] = None
reachable_event_codes: Optional[list[str]] = None # ID list of reachable modules
reachable_event_endpoints: Optional[list[str]] = None
class CompanyToken(BaseModel): # Required Company Object for an employee
company_id: int
company_uu_id: str
department_id: int # ID list of departments
department_uu_id: str # ID list of departments
duty_id: int
duty_uu_id: str
staff_id: int
staff_uu_id: str
employee_id: int
employee_uu_id: str
bulk_duties_id: int
reachable_event_codes: Optional[list[str]] = None # ID list of reachable modules
reachable_event_endpoints: Optional[list[str]] = None
class OccupantTokenObject(ApplicationToken):
# Occupant Token Object -> Requires selection of the occupant type for a specific build part
available_occupants: dict = None
selected_occupant: Optional[OccupantToken] = None # Selected Occupant Type
@property
def is_employee(self) -> bool:
return False
@property
def is_occupant(self) -> bool:
return True
class EmployeeTokenObject(ApplicationToken):
# Full hierarchy Employee[staff_id] -> Staff -> Duty -> Department -> Company
companies_id_list: List[int] # List of company objects
companies_uu_id_list: List[str] # List of company objects
duty_id_list: List[int] # List of duty objects
duty_uu_id_list: List[str] # List of duty objects
selected_company: Optional[CompanyToken] = None # Selected Company Object
@property
def is_employee(self) -> bool:
return True
@property
def is_occupant(self) -> bool:
return False

View File

@@ -0,0 +1,83 @@
import json
from typing import Any, ClassVar, TypeVar, Dict, Tuple, List
from pydantic import BaseModel
from ErrorHandlers import HTTPExceptionApi
from ApiLibrary.common.line_number import get_line_number_for_error
from ApiValidations.Request.base_validations import CrudRecords, PydanticBaseModel
class ValidationParser:
def __init__(self, active_validation: BaseModel):
self.core_validation = active_validation
self.annotations = active_validation.model_json_schema()
self.annotations = json.loads(json.dumps(self.annotations))
self.schema = {}
self.parse()
def parse(self):
from ApiValidations.Request.base_validations import (
CrudRecords,
PydanticBaseModel,
)
properties = dict(self.annotations.get("properties")).items()
total_class_annotations = {
**self.core_validation.__annotations__,
**PydanticBaseModel.__annotations__,
**CrudRecords.__annotations__,
}
for key, value in properties:
default, required, possible_types = (
dict(value).get("default", None),
True,
[],
)
if dict(value).get("anyOf", None):
for _ in dict(value).get("anyOf") or []:
type_opt = json.loads(json.dumps(_))
if not type_opt.get("type") == "null":
possible_types.append(type_opt.get("type"))
field_type = possible_types[0]
required = False
else:
field_type = dict(value).get("type", "string")
attribute_of_class = total_class_annotations.get(key, None)
aoc = str(attribute_of_class) if attribute_of_class else None
if attribute_of_class:
if aoc in ("<class 'str'>", "typing.Optional[str]"):
field_type, required = "string", aoc == "<class 'str'>"
elif aoc in ("<class 'int'>", "typing.Optional[int]"):
field_type, required = "integer", aoc == "<class 'int'>"
elif aoc in ("<class 'bool'>", "typing.Optional[bool]"):
field_type, required = "boolean", aoc == "<class 'bool'>"
elif aoc in ("<class 'float'>", "typing.Optional[float]"):
field_type, required = "float", aoc == "<class 'float'>"
elif aoc in (
"<class 'datetime.datetime'>",
"typing.Optional[datetime.datetime]",
):
field_type, required = (
"datetime",
aoc == "<class 'datetime.datetime'>",
)
self.schema[key] = {
"type": field_type,
"required": required,
"default": default,
}
class ValidationModel:
def __init__(self, response_model: BaseModel, language_model, language_models):
self.response_model = response_model
self.validation = None
self.headers = language_model
self.language_models = language_models
self.get_validation()
def get_validation(self) -> Tuple:
self.headers = self.language_models
self.validation = ValidationParser(self.response_model).schema