updated Query options
This commit is contained in:
@@ -140,7 +140,9 @@ class QueryModel(ArgumentModel):
|
||||
|
||||
@classmethod
|
||||
def filter_all_system(
|
||||
cls: Type[T], *args: Union[BinaryExpression, ColumnExpressionArgument], db: Session
|
||||
cls: Type[T],
|
||||
*args: Union[BinaryExpression, ColumnExpressionArgument],
|
||||
db: Session,
|
||||
) -> PostgresResponse:
|
||||
"""
|
||||
Filter multiple records by expressions without status filtering.
|
||||
@@ -158,7 +160,11 @@ class QueryModel(ArgumentModel):
|
||||
return PostgresResponse(pre_query=cls._query(db), query=query, is_array=True)
|
||||
|
||||
@classmethod
|
||||
def filter_all(cls: Type[T], *args: Union[BinaryExpression, ColumnExpressionArgument], db: Session) -> PostgresResponse:
|
||||
def filter_all(
|
||||
cls: Type[T],
|
||||
*args: Union[BinaryExpression, ColumnExpressionArgument],
|
||||
db: Session,
|
||||
) -> PostgresResponse:
|
||||
"""
|
||||
Filter multiple records by expressions.
|
||||
|
||||
|
||||
@@ -2,7 +2,8 @@ from __future__ import annotations
|
||||
from typing import Any, Dict, Optional, Union
|
||||
from sqlalchemy import desc, asc
|
||||
from pydantic import BaseModel
|
||||
from AllConfigs.SqlDatabase.configs import PaginateConfig
|
||||
from ApiLayers.AllConfigs.SqlDatabase.configs import PaginateConfig
|
||||
from ApiLayers.ApiValidations.Request import ListOptions
|
||||
from Services.PostgresDb.Models.response import PostgresResponse
|
||||
|
||||
|
||||
@@ -183,3 +184,44 @@ class PaginationResult:
|
||||
if self.response_type
|
||||
else queried_data.get_dict()
|
||||
)
|
||||
|
||||
|
||||
class QueryOptions:
|
||||
|
||||
def __init__(self, table, data: Union[dict, ListOptions] = None, model_query: Optional[Any] = None):
|
||||
self.table = table
|
||||
self.data = data
|
||||
self.model_query = model_query
|
||||
if isinstance(data, dict):
|
||||
self.data = ListOptions(**data)
|
||||
self.validate_query()
|
||||
if not self.data.order_type:
|
||||
self.data.order_type = ["created_at"]
|
||||
if not self.data.order_field:
|
||||
self.data.order_field = ["uu_id"]
|
||||
|
||||
def validate_query(self):
|
||||
if not self.data.query or not self.model_query:
|
||||
return ()
|
||||
cleaned_query, cleaned_query_by_model, last_dict = {}, {}, {}
|
||||
for key, value in self.data.query.items():
|
||||
cleaned_query[str(str(key).split("__")[0])] = value
|
||||
cleaned_query_by_model[str(str(key).split("__")[0])] = (key, value)
|
||||
cleaned_model = self.model_query(**cleaned_query)
|
||||
for i in cleaned_query:
|
||||
if hasattr(cleaned_model, i):
|
||||
last_dict[str(cleaned_query_by_model[i][0])] = str(cleaned_query_by_model[i][1])
|
||||
self.data.query = last_dict
|
||||
|
||||
def convert(self) -> tuple:
|
||||
"""
|
||||
self.table.convert(query)
|
||||
(<sqlalchemy.sql.elements.BinaryExpression object at 0x7caaeacf0080>, <sqlalchemy.sql.elements.BinaryExpression object at 0x7caaea729b80>)
|
||||
"""
|
||||
if not self.data:
|
||||
return ()
|
||||
if not self.data.query:
|
||||
return ()
|
||||
print('query', self.data)
|
||||
print('query', self.data.query)
|
||||
return tuple(self.table.convert(self.data.query))
|
||||
|
||||
@@ -1,18 +1,26 @@
|
||||
from Schemas import AddressNeighborhood
|
||||
from typing import Optional
|
||||
|
||||
from ApiLayers.ApiValidations.Request import ListOptions
|
||||
from ApiLayers.Schemas import AddressNeighborhood
|
||||
from Services.PostgresDb.Models.crud_alchemy import Credentials
|
||||
from Services.PostgresDb.Models.mixin import BasicMixin
|
||||
from Services.PostgresDb.Models.pagination import Pagination, PaginationResult
|
||||
from Services.PostgresDb.Models.pagination import Pagination, PaginationResult, QueryOptions
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
listing = False
|
||||
listing = True
|
||||
creating = False
|
||||
updating = True
|
||||
updating = False
|
||||
|
||||
new_session = AddressNeighborhood.new_session()
|
||||
new_session_test = AddressNeighborhood.new_session()
|
||||
|
||||
BasicMixin.creds = Credentials(person_id=10, person_name="Berkay Super User")
|
||||
|
||||
class QueryModel(BaseModel):
|
||||
neighborhood_name: Optional[str]
|
||||
neighborhood_code: Optional[str]
|
||||
|
||||
|
||||
if listing:
|
||||
"""List Options and Queries"""
|
||||
@@ -20,25 +28,36 @@ if listing:
|
||||
AddressNeighborhood.neighborhood_code.icontains("10"),
|
||||
db=new_session,
|
||||
).query
|
||||
query_of_list_options = {
|
||||
"neighborhood_name__ilike": "A%",
|
||||
"neighborhood_code__contains": "3",
|
||||
}
|
||||
address_neighborhoods = AddressNeighborhood.filter_all(
|
||||
*AddressNeighborhood.convert(query_of_list_options),
|
||||
db=new_session,
|
||||
)
|
||||
pagination = Pagination(data=address_neighborhoods)
|
||||
pagination.page = 9
|
||||
pagination.size = 10
|
||||
pagination.orderField = ["type_code", "neighborhood_code"]
|
||||
pagination.orderType = ["desc", "asc"]
|
||||
|
||||
pagination_result = PaginationResult(
|
||||
data=address_neighborhoods, pagination=pagination
|
||||
)
|
||||
print(pagination_result.pagination.as_dict())
|
||||
print(pagination_result.data)
|
||||
list_options = {
|
||||
"page": 1,
|
||||
"size": 11,
|
||||
"order_field": ["type_code", "neighborhood_code"],
|
||||
"order_type": ["asc", "desc"],
|
||||
"query": {
|
||||
"neighborhood_name__ilike": "A%",
|
||||
"neighborhood_code__contains": "3",
|
||||
"my_other_field__ilike": "B%",
|
||||
"other_other_field__ilike": "C%",
|
||||
}
|
||||
}
|
||||
|
||||
query_options = QueryOptions(table=AddressNeighborhood, data=list_options, model_query=QueryModel)
|
||||
address_neighborhoods = AddressNeighborhood.filter_all(*query_options.convert(), db=new_session)
|
||||
|
||||
pagination = Pagination(data=address_neighborhoods)
|
||||
pagination.change(**list_options)
|
||||
|
||||
pagination_result = PaginationResult(data=address_neighborhoods, pagination=pagination)
|
||||
print("as_dict", pagination_result.pagination.as_dict())
|
||||
for i, row in enumerate(pagination_result.data):
|
||||
print(i+1, row)
|
||||
|
||||
# list_options_valid = ListOptions(**list_options)
|
||||
# pagination.page = 9
|
||||
# pagination.size = 10
|
||||
# pagination.orderField = ["type_code", "neighborhood_code"]
|
||||
# pagination.orderType = ["asc", "asc"]
|
||||
|
||||
if creating:
|
||||
"""Create Queries"""
|
||||
@@ -62,9 +81,7 @@ if creating:
|
||||
if updating:
|
||||
"""Update Queries"""
|
||||
|
||||
query_of_list_options = {
|
||||
"uu_id": str("33a89767-d2dc-4531-8f66-7b650e22a8a7"),
|
||||
}
|
||||
query_of_list_options = {"uu_id": str("33a89767-d2dc-4531-8f66-7b650e22a8a7")}
|
||||
print("query_of_list_options", query_of_list_options)
|
||||
address_neighborhoods_one = AddressNeighborhood.filter_one(
|
||||
*AddressNeighborhood.convert(query_of_list_options),
|
||||
|
||||
Reference in New Issue
Block a user