88from pydantic ._internal ._model_construction import ModelMetaclass
99from sqlalchemy_to_pydantic import sqlalchemy_to_pydantic
1010from sqlalchemy import select , inspect
11- from sqlalchemy .orm import InstrumentedAttribute , DeclarativeMeta
11+ from sqlalchemy .orm import InstrumentedAttribute , DeclarativeBase
1212from sqlalchemy .sql .elements import BinaryExpression , UnaryExpression
1313from sqlalchemy .sql .expression import and_ , or_
1414from starlette import status
@@ -27,9 +27,9 @@ class FilterCore:
2727
2828 def __init__ (
2929 self ,
30- model : Type [DeclarativeMeta ],
30+ model : Type [DeclarativeBase ],
3131 allowed_filters : dict [str , list [ops ]],
32- select_query_part : Select [Any ] = None
32+ select_query_part : Select [Any ] | None = None
3333 ) -> None :
3434 """
3535 Produce a class:`FilterCore` object against a function
@@ -57,6 +57,7 @@ def get_query(self, custom_filter: str) -> Select[Any]:
5757 created_at__between=2023-05-01,2023-05-05|
5858 category__eq=Medicine&
5959 order_by=-id
60+ :param select_query_part: custom select query part (select(model).join(model1))
6061
6162 :return:
6263 select(model)
@@ -69,16 +70,16 @@ def get_query(self, custom_filter: str) -> Select[Any]:
6970 model.category == 'Medicine'
7071 ).order_by(model.id.desc())
7172 """
72- split_query = self .split_by_order_by (custom_filter )
73+ split_query = self ._split_by_order_by (custom_filter )
7374 try :
74- complete_query = self .get_complete_query (* split_query )
75+ complete_query = self ._get_complete_query (* split_query )
7576 except SAFilterOrmException as e :
7677 raise HTTPException (status_code = status .HTTP_400_BAD_REQUEST , detail = e .args [0 ])
7778 return complete_query
7879
79- def get_complete_query (self , filter_query_str : str , order_by_query_str : str | None = None ) -> Select [Any ]:
80+ def _get_complete_query (self , filter_query_str : str , order_by_query_str : str | None = None ) -> Select [Any ]:
8081 select_query_part = self .get_select_query_part ()
81- filter_query_part = self .get_filter_query_part (filter_query_str )
82+ filter_query_part = self ._get_filter_query_part (filter_query_str )
8283 complete_query = select_query_part .filter (* filter_query_part )
8384 group_query_part = self .get_group_by_query_part ()
8485 if group_query_part :
@@ -89,11 +90,11 @@ def get_complete_query(self, filter_query_str: str, order_by_query_str: str | No
8990 return complete_query
9091
9192 def get_select_query_part (self ) -> Select [Any ]:
92- if self .select_query_part :
93+ if self .select_query_part is not None :
9394 return self .select_query_part
9495 return select (self .model )
9596
96- def get_filter_query_part (self , filter_query_str : str ) -> list [Any ]:
97+ def _get_filter_query_part (self , filter_query_str : str ) -> list [Any ]:
9798 conditions = self ._get_filter_query (filter_query_str )
9899 if len (conditions ) == 0 :
99100 return conditions
@@ -123,7 +124,7 @@ def _get_filter_query(self, custom_filter: str) -> list[BinaryExpression]:
123124 filter_conditions .append (and_ (* and_condition ))
124125 return filter_conditions
125126
126- def _create_pydantic_serializers (self ) -> dict [str , ModelMetaclass ]:
127+ def _create_pydantic_serializers (self ) -> dict [str , dict [ str , ModelMetaclass ] ]:
127128 """
128129 Create two pydantic models (optional and list field types)
129130 for value: str serialization
@@ -139,7 +140,7 @@ class model.__name__(BaseModel):
139140 """
140141
141142 models = [self .model ]
142- models .extend (self .get_relations ())
143+ models .extend (self ._get_relations ())
143144
144145 serializers = {}
145146
@@ -154,7 +155,7 @@ class model.__name__(BaseModel):
154155
155156 return serializers
156157
157- def get_relations (self ) -> list :
158+ def _get_relations (self ) -> list :
158159 return [relation [1 ].mapper .class_ for relation in self .relationships ]
159160
160161 def _get_orm_for_field (
@@ -192,7 +193,7 @@ def _format_expression(
192193 raise SAFilterOrmException (f"Incorrect filter value '{ value } '" )
193194
194195 @staticmethod
195- def split_by_order_by (query ) -> list :
196+ def _split_by_order_by (query ) -> list :
196197 split_query = [query_part .strip ("&" ) for query_part in query .split ("order_by=" )]
197198 if len (split_query ) > 2 :
198199 raise SAFilterOrmException ("Use only one order_by directive" )
0 commit comments