updated list options and response validatore tested

This commit is contained in:
berkay 2025-01-30 17:19:23 +03:00
parent 9781cae858
commit 602ab2ffcc
6 changed files with 62 additions and 42 deletions

View File

@ -6,31 +6,11 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="b5202e0c-6ddf-4a56-a13a-e18798c4c7cf" name="Changes" comment=""> <list default="true" id="b5202e0c-6ddf-4a56-a13a-e18798c4c7cf" name="Changes" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ApiLayers/ApiValidations/Response/default_response.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/ApiValidations/Response/default_response.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ApiLayers/LanguageModels/Database/Mixins/crud_mixin.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/LanguageModels/Database/Mixins/crud_mixin.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ApiLayers/LanguageModels/Response/all_responses.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/LanguageModels/Response/all_responses.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ApiLayers/Middleware/token_event_middleware.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/Middleware/token_event_middleware.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ApiLayers/Schemas/identity/identity.py" beforeDir="false" afterPath="$PROJECT_DIR$/ApiLayers/Schemas/identity/identity.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/DockerApiServices/EventServiceApi/Dockerfile" beforeDir="false" afterPath="$PROJECT_DIR$/DockerApiServices/EventServiceApi/Dockerfile" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/api_events.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/api_events.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/info.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/authentication/auth/info.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/events/__init__.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/events/__init__.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/__init__.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/account_records.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/events/account/account_records.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/account_records.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/events/account/account_records.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/api_events.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/events/account/api_events.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/api_events.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/events/account/api_events.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/cluster.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/events/account/cluster.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/endpoints.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/function_handlers.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/events/account/function_handlers.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/function_handlers.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/events/account/function_handlers.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/events/account/models.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/events/account/models.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/template/template/function_handlers.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/template/template/function_handlers.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/AllEvents/validations/validation/api_events.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/AllEvents/validations/validation/api_events.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/Engine/abstract_class.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/Engine/abstract_class.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Events/base_request_model.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/base_request_model.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Events/base_request_model.py" beforeDir="false" afterPath="$PROJECT_DIR$/Events/base_request_model.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Services/PostgresDb/Models/crud_alchemy.py" beforeDir="false" afterPath="$PROJECT_DIR$/Services/PostgresDb/Models/crud_alchemy.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Services/PostgresDb/Models/pagination.py" beforeDir="false" afterPath="$PROJECT_DIR$/Services/PostgresDb/Models/pagination.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Services/PostgresDb/Models/pagination.py" beforeDir="false" afterPath="$PROJECT_DIR$/Services/PostgresDb/Models/pagination.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Services/PostgresDb/Models/response.py" beforeDir="false" afterPath="$PROJECT_DIR$/Services/PostgresDb/Models/response.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Services/PostgresDb/how_to.py" beforeDir="false" afterPath="$PROJECT_DIR$/Services/PostgresDb/how_to.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/docker-compose-services.yml" beforeDir="false" afterPath="$PROJECT_DIR$/docker-compose-services.yml" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />

View File

@ -35,7 +35,7 @@ def account_insert_event_endpoint(
event_2_catch = AccountRecordsEventMethods.retrieve_event( event_2_catch = AccountRecordsEventMethods.retrieve_event(
event_function_code=f"{account_insert_super_user_event.key}" event_function_code=f"{account_insert_super_user_event.key}"
) )
context_retriever.event = event_2_catch context_retriever.RESPONSE_VALIDATOR = event_2_catch.RESPONSE_VALIDATOR
data = event_2_catch.REQUEST_VALIDATOR(**data.data) data = event_2_catch.REQUEST_VALIDATOR(**data.data)
AccountListEventMethods.context_retriever = context_retriever AccountListEventMethods.context_retriever = context_retriever
pagination_result = event_2_catch.endpoint_callable(data=data) pagination_result = event_2_catch.endpoint_callable(data=data)

View File

@ -1,9 +1,11 @@
from pydantic import BaseModel
from ApiLayers.ApiValidations.Request import ListOptions from ApiLayers.ApiValidations.Request import ListOptions
from ApiLayers.Schemas import AccountRecords from ApiLayers.Schemas import AccountRecords
from ApiLayers.LanguageModels.Request import ( # from ApiLayers.LanguageModels.Request import (
LoginRequestLanguageModel, # LoginRequestLanguageModel,
SelectRequestLanguageModel, # SelectRequestLanguageModel,
) # )
from Events.Engine.abstract_class import Event from Events.Engine.abstract_class import Event
from .models import AccountRequestValidators from .models import AccountRequestValidators
@ -12,12 +14,22 @@ from .function_handlers import (
) )
class SelectResponseAccount(BaseModel):
"""
Response model for account list.
"""
neighborhood_code: str
neighborhood_name: str
type_code: str
type_description: str
# Auth Login # Auth Login
account_insert_super_user_event = Event( account_insert_super_user_event = Event(
name="account_insert_super_user_event", name="account_insert_super_user_event",
key="36a165fe-a2f3-437b-80ee-1ee44670fe70", key="36a165fe-a2f3-437b-80ee-1ee44670fe70",
request_validator=ListOptions, request_validator=ListOptions,
# response_validator=SelectRequestLanguageModel, response_validator=SelectResponseAccount,
# language_models=[AccountRecords.__language_model__], # language_models=[AccountRecords.__language_model__],
language_models=[], language_models=[],
statics="ACCOUNTS_LIST", statics="ACCOUNTS_LIST",

View File

@ -44,26 +44,51 @@ class AccountListEventMethods(BaseRouteModel):
} }
""" """
@classmethod @classmethod
def account_records_list(cls, data: Optional[Union[dict, ListOptions]]) -> PaginationResult: def account_records_list(cls, data: Optional[Union[dict, ListOptions]]) -> PaginationResult:
from ApiLayers.Schemas import AddressNeighborhood
list_options_base = ListOptionsBase( list_options_base = ListOptionsBase(
table=AccountRecords, list_options=data, model_query=None, table=AddressNeighborhood, list_options=data, model_query=None,
) )
db_session, query_options = list_options_base.init_list_options() db_session, query_options = list_options_base.init_list_options()
if cls.context_retriever.token.is_occupant: if cls.context_retriever.token.is_occupant:
AccountRecords.pre_query = AccountRecords.filter_all( AddressNeighborhood.pre_query = AddressNeighborhood.filter_all(
AccountRecords.company_id AddressNeighborhood.neighborhood_code.icontains("10"),
== cls.context_retriever.token.selected_occupant.responsible_company_id,
db=db_session, db=db_session,
).query ).query
elif cls.context_retriever.token.is_employee: elif cls.context_retriever.token.is_employee:
AccountRecords.pre_query = AccountRecords.filter_all( AddressNeighborhood.pre_query = AddressNeighborhood.filter_all(
AccountRecords.company_id == AddressNeighborhood.neighborhood_code.icontains("9"),
cls.context_retriever.token.selected_company.company_id,
db=db_session, db=db_session,
).query ).query
records = AccountRecords.filter_all(*query_options.convert(), db=db_session) records = AddressNeighborhood.filter_all(*query_options.convert(), db=db_session)
return list_options_base.paginated_result(records=records) return list_options_base.paginated_result(
records=records, response_model=cls.context_retriever.RESPONSE_VALIDATOR
)
# @classmethod
# def account_records_list(cls, data: Optional[Union[dict, ListOptions]]) -> PaginationResult:
# list_options_base = ListOptionsBase(
# table=AccountRecords, list_options=data, model_query=None,
# )
# db_session, query_options = list_options_base.init_list_options()
# if cls.context_retriever.token.is_occupant:
# AccountRecords.pre_query = AccountRecords.filter_all(
# AccountRecords.company_id
# == cls.context_retriever.token.selected_occupant.responsible_company_id,
# db=db_session,
# ).query
# elif cls.context_retriever.token.is_employee:
# AccountRecords.pre_query = AccountRecords.filter_all(
# AccountRecords.company_id ==
# cls.context_retriever.token.selected_company.company_id,
# db=db_session,
# ).query
# records = AccountRecords.filter_all(*query_options.convert(), db=db_session)
# return list_options_base.paginated_result(
# records=records, response_model=cls.context_retriever.RESPONSE_VALIDATOR
# )
# @classmethod # @classmethod
# def account_records_list_flt_res(cls, list_options: ListOptions) -> PaginationResult: # def account_records_list_flt_res(cls, list_options: ListOptions) -> PaginationResult:

View File

@ -34,6 +34,7 @@ class ContextRetrievers:
is_auth: bool = False is_auth: bool = False
is_event: bool = False is_event: bool = False
key_: str = "" key_: str = ""
RESPONSE_VALIDATOR = None
def __init__(self, func): def __init__(self, func):
self.func = func self.func = func
@ -105,10 +106,10 @@ class ListOptionsBase:
query_options = QueryOptions(table=self.table, data=self.list_options, model_query=self.model_query) query_options = QueryOptions(table=self.table, data=self.list_options, model_query=self.model_query)
return db_session, query_options return db_session, query_options
def paginated_result(self, records) -> PaginationResult: def paginated_result(self, records, response_model: Optional[BaseModel] = None) -> PaginationResult:
pagination = Pagination(data=records) pagination = Pagination(data=records)
if isinstance(self.list_options, dict): if isinstance(self.list_options, dict):
pagination.change(**self.list_options) pagination.change(**self.list_options)
elif isinstance(self.list_options, BaseModel): elif isinstance(self.list_options, BaseModel):
pagination.change(**self.list_options.model_dump()) pagination.change(**self.list_options.model_dump())
return PaginationResult(data=records, pagination=pagination) return PaginationResult(data=records, pagination=pagination, response_model=response_model)

View File

@ -136,13 +136,14 @@ class PaginationResult:
pagination: Pagination state pagination: Pagination state
""" """
def __init__(self, data: PostgresResponse, pagination: Pagination): def __init__(self, data: PostgresResponse, pagination: Pagination, response_model: Any = None):
self._query = data.query self._query = data.query
self.pagination = pagination self.pagination = pagination
self.response_type = data.is_list self.response_type = data.is_list
self.limit = self.pagination.size self.limit = self.pagination.size
self.offset = self.pagination.size * (self.pagination.page - 1) self.offset = self.pagination.size * (self.pagination.page - 1)
self.order_by = self.pagination.orderField self.order_by = self.pagination.orderField
self.response_model = response_model
def dynamic_order_by(self): def dynamic_order_by(self):
""" """
@ -179,11 +180,14 @@ class PaginationResult:
queried_data = ( queried_data = (
query_paginated.all() if self.response_type else query_paginated.first() query_paginated.all() if self.response_type else query_paginated.first()
) )
return ( data = (
[result.get_dict() for result in queried_data] [result.get_dict() for result in queried_data]
if self.response_type if self.response_type
else queried_data.get_dict() else queried_data.get_dict()
) )
if self.response_model:
return [self.response_model(**item).model_dump() for item in data]
return data
class QueryOptions: class QueryOptions:
@ -210,7 +214,7 @@ class QueryOptions:
cleaned_model = self.model_query(**cleaned_query) cleaned_model = self.model_query(**cleaned_query)
for i in cleaned_query: for i in cleaned_query:
if hasattr(cleaned_model, i): if hasattr(cleaned_model, i):
last_dict[str(cleaned_query_by_model[i][0])] = str(cleaned_query_by_model[i][1]) last_dict[str(cleaned_query_by_model[str(i)][0])] = str(cleaned_query_by_model[str(i)][1])
self.data.query = last_dict self.data.query = last_dict
def convert(self) -> tuple: def convert(self) -> tuple:
@ -222,6 +226,4 @@ class QueryOptions:
return () return ()
if not self.data.query: if not self.data.query:
return () return ()
print('query', self.data)
print('query', self.data.query)
return tuple(self.table.convert(self.data.query)) return tuple(self.table.convert(self.data.query))