updated docs
This commit is contained in:
@@ -16,12 +16,14 @@ class BaseAlchemyModel:
|
||||
Session: Session object for model
|
||||
Actions: save, flush, rollback, commit
|
||||
"""
|
||||
|
||||
__abstract__ = True
|
||||
|
||||
@classmethod
|
||||
def new_session(cls) -> Session:
|
||||
"""Get database session."""
|
||||
from Services.PostgresDb.database import get_db
|
||||
|
||||
with get_db() as session:
|
||||
return session
|
||||
|
||||
@@ -143,6 +145,3 @@ class BaseAlchemyModel:
|
||||
db: Database session
|
||||
"""
|
||||
db.rollback()
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -27,7 +27,9 @@ class Credentials(BaseModel):
|
||||
class CrudActions(SystemFields):
|
||||
|
||||
@classmethod
|
||||
def extract_system_fields(cls, filter_kwargs: dict, create: bool = True) -> Dict[str, Any]:
|
||||
def extract_system_fields(
|
||||
cls, filter_kwargs: dict, create: bool = True
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Remove system-managed fields from input dictionary.
|
||||
|
||||
@@ -63,8 +65,6 @@ class CrudActions(SystemFields):
|
||||
if key in cls.columns + cls.hybrid_properties + cls.settable_relations
|
||||
}
|
||||
|
||||
|
||||
|
||||
@classmethod
|
||||
def iterate_over_variables(cls, val: Any, key: str) -> tuple[bool, Optional[Any]]:
|
||||
"""
|
||||
@@ -187,9 +187,9 @@ class CrudActions(SystemFields):
|
||||
return_dict[key] = value_of_database
|
||||
else:
|
||||
# Handle default field selection
|
||||
exclude_list = (
|
||||
getattr(self, "__exclude__fields__", []) or []
|
||||
) + list(self.__system_default_model__)
|
||||
exclude_list = (getattr(self, "__exclude__fields__", []) or []) + list(
|
||||
self.__system_default_model__
|
||||
)
|
||||
columns_list = list(set(self.columns).difference(set(exclude_list)))
|
||||
columns_list = [col for col in columns_list if str(col)[-2:] != "id"]
|
||||
columns_list.extend(
|
||||
@@ -230,18 +230,18 @@ class CRUDModel(BaseAlchemyModel, CrudActions):
|
||||
"""
|
||||
|
||||
if getattr(cls.creds, "person_id", None) and getattr(
|
||||
cls.creds, "person_name", None
|
||||
cls.creds, "person_name", None
|
||||
):
|
||||
record_created.created_by_id = cls.creds.person_id
|
||||
record_created.created_by = cls.creds.person_name
|
||||
return
|
||||
|
||||
@classmethod
|
||||
def update_metadata(cls, created: bool, error_case: str = None, message: str = None) -> None:
|
||||
def update_metadata(
|
||||
cls, created: bool, error_case: str = None, message: str = None
|
||||
) -> None:
|
||||
cls.meta_data = MetaDataRow(
|
||||
created=created,
|
||||
error_case=error_case,
|
||||
message=message
|
||||
created=created, error_case=error_case, message=message
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@@ -250,7 +250,7 @@ class CRUDModel(BaseAlchemyModel, CrudActions):
|
||||
error_code=cls.meta_data.error_case,
|
||||
lang=cls.lang,
|
||||
loc=get_line_number_for_error(),
|
||||
sys_msg=cls.meta_data.message
|
||||
sys_msg=cls.meta_data.message,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
@@ -385,11 +385,15 @@ class CRUDModel(BaseAlchemyModel, CrudActions):
|
||||
raise ValueError("Confirm field cannot be updated with other fields")
|
||||
|
||||
if is_confirmed_argument:
|
||||
if getattr(self.creds, "person_id", None) and getattr(self.creds, "person_name", None):
|
||||
if getattr(self.creds, "person_id", None) and getattr(
|
||||
self.creds, "person_name", None
|
||||
):
|
||||
self.confirmed_by_id = self.creds.person_id
|
||||
self.confirmed_by = self.creds.person_name
|
||||
else:
|
||||
if getattr(self.creds, "person_id", None) and getattr(self.creds, "person_name", None):
|
||||
if getattr(self.creds, "person_id", None) and getattr(
|
||||
self.creds, "person_name", None
|
||||
):
|
||||
self.updated_by_id = self.creds.person_id
|
||||
self.updated_by = self.creds.person_name
|
||||
return
|
||||
|
||||
@@ -28,9 +28,7 @@ class ArgumentModel:
|
||||
@classmethod
|
||||
def _query(cls: Type[T], db: Session) -> Query:
|
||||
"""Returns the query to use in the model."""
|
||||
return (
|
||||
cls.pre_query if cls.pre_query else db.query(cls)
|
||||
)
|
||||
return cls.pre_query if cls.pre_query else db.query(cls)
|
||||
|
||||
@classmethod
|
||||
def add_new_arg_to_args(cls: Type[T], args_list, argument, value):
|
||||
@@ -79,7 +77,7 @@ class QueryModel(ArgumentModel, BaseModel, SmartQueryMixin):
|
||||
|
||||
@classmethod
|
||||
def convert(
|
||||
cls: Type[T], smart_options: dict, validate_model: Any = None
|
||||
cls: Type[T], smart_options: dict, validate_model: Any = None
|
||||
) -> tuple[BinaryExpression]:
|
||||
if not validate_model:
|
||||
return tuple(cls.filter_expr(**smart_options))
|
||||
@@ -107,11 +105,11 @@ class QueryModel(ArgumentModel, BaseModel, SmartQueryMixin):
|
||||
|
||||
@classmethod
|
||||
def filter_one(
|
||||
cls: Type[T],
|
||||
*args: Any,
|
||||
db: Session,
|
||||
system: bool = False,
|
||||
expired: bool = False,
|
||||
cls: Type[T],
|
||||
*args: Any,
|
||||
db: Session,
|
||||
system: bool = False,
|
||||
expired: bool = False,
|
||||
) -> PostgresResponse:
|
||||
"""
|
||||
Filter single record by expressions.
|
||||
@@ -132,7 +130,6 @@ class QueryModel(ArgumentModel, BaseModel, SmartQueryMixin):
|
||||
query = cls._query(db).filter(*args)
|
||||
return PostgresResponse(pre_query=cls._query(db), query=query, is_array=False)
|
||||
|
||||
|
||||
@classmethod
|
||||
def filter_all_system(
|
||||
cls: Type[T], *args: BinaryExpression, db: Session
|
||||
@@ -152,9 +149,7 @@ class QueryModel(ArgumentModel, BaseModel, SmartQueryMixin):
|
||||
return PostgresResponse(pre_query=cls._query(db), query=query, is_array=True)
|
||||
|
||||
@classmethod
|
||||
def filter_all(
|
||||
cls: Type[T], *args: Any, db: Session
|
||||
) -> PostgresResponse:
|
||||
def filter_all(cls: Type[T], *args: Any, db: Session) -> PostgresResponse:
|
||||
"""
|
||||
Filter multiple records by expressions.
|
||||
|
||||
@@ -170,9 +165,7 @@ class QueryModel(ArgumentModel, BaseModel, SmartQueryMixin):
|
||||
return PostgresResponse(pre_query=cls._query(db), query=query, is_array=True)
|
||||
|
||||
@classmethod
|
||||
def filter_by_all_system(
|
||||
cls: Type[T], db: Session, **kwargs
|
||||
) -> PostgresResponse:
|
||||
def filter_by_all_system(cls: Type[T], db: Session, **kwargs) -> PostgresResponse:
|
||||
"""
|
||||
Filter multiple records by keyword arguments.
|
||||
|
||||
|
||||
@@ -1,7 +1,2 @@
|
||||
|
||||
|
||||
|
||||
class LanguageModel:
|
||||
__language_model__ = None
|
||||
|
||||
|
||||
|
||||
@@ -37,7 +37,6 @@ class CrudMixin(BasicMixin, SerializeMixin, ReprMixin):
|
||||
|
||||
__abstract__ = True
|
||||
|
||||
|
||||
# Primary and reference fields
|
||||
id: Mapped[int] = mapped_column(Integer, primary_key=True)
|
||||
uu_id: Mapped[str] = mapped_column(
|
||||
@@ -171,6 +170,3 @@ class CrudCollection(CrudMixin):
|
||||
# )
|
||||
#
|
||||
# return headers_and_validation
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -155,14 +155,18 @@ class PaginationResult:
|
||||
)
|
||||
order_criteria = zip(self.order_by, self.pagination.orderType)
|
||||
for field, direction in order_criteria:
|
||||
if hasattr(self._query.column_descriptions[0]['entity'], field):
|
||||
if hasattr(self._query.column_descriptions[0]["entity"], field):
|
||||
if direction.lower().startswith("d"):
|
||||
self._query = self._query.order_by(
|
||||
desc(getattr(self._query.column_descriptions[0]['entity'], field))
|
||||
desc(
|
||||
getattr(self._query.column_descriptions[0]["entity"], field)
|
||||
)
|
||||
)
|
||||
else:
|
||||
self._query = self._query.order_by(
|
||||
asc(getattr(self._query.column_descriptions[0]['entity'], field))
|
||||
asc(
|
||||
getattr(self._query.column_descriptions[0]["entity"], field)
|
||||
)
|
||||
)
|
||||
return self._query
|
||||
|
||||
@@ -171,6 +175,11 @@ class PaginationResult:
|
||||
"""Get query object."""
|
||||
query_ordered = self.dynamic_order_by()
|
||||
query_paginated = query_ordered.limit(self.limit).offset(self.offset)
|
||||
queried_data = query_paginated.all() if self.response_type else query_paginated.first()
|
||||
return [result.get_dict() for result in queried_data] if self.response_type else queried_data.get_dict()
|
||||
|
||||
queried_data = (
|
||||
query_paginated.all() if self.response_type else query_paginated.first()
|
||||
)
|
||||
return (
|
||||
[result.get_dict() for result in queried_data]
|
||||
if self.response_type
|
||||
else queried_data.get_dict()
|
||||
)
|
||||
|
||||
@@ -26,11 +26,11 @@ class PostgresResponse(Generic[T]):
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
pre_query: Query,
|
||||
query: Query,
|
||||
is_array: bool = True,
|
||||
metadata: Any = None,
|
||||
self,
|
||||
pre_query: Query,
|
||||
query: Query,
|
||||
is_array: bool = True,
|
||||
metadata: Any = None,
|
||||
):
|
||||
self._is_list = is_array
|
||||
self._query = query
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
|
||||
|
||||
class SystemFields:
|
||||
|
||||
__abstract__ = True
|
||||
@@ -50,4 +48,3 @@ class SystemFields:
|
||||
"updated_by_id",
|
||||
"created_by_id",
|
||||
)
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ class TokenModel:
|
||||
def __post_init__(self):
|
||||
self.lang = str(self.lang or "tr").lower()
|
||||
self.credentials = self.credentials or {}
|
||||
if 'GMT' in self.timezone:
|
||||
if "GMT" in self.timezone:
|
||||
raise HTTPExceptionApi(
|
||||
error_code="HTTP_400_BAD_REQUEST",
|
||||
lang=self.lang,
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
|
||||
from Schemas import AddressNeighborhood
|
||||
from Services.PostgresDb.Models.crud_alchemy import Credentials
|
||||
from Services.PostgresDb.Models.mixin import BasicMixin
|
||||
@@ -12,13 +11,13 @@ updating = True
|
||||
new_session = AddressNeighborhood.new_session()
|
||||
new_session_test = AddressNeighborhood.new_session()
|
||||
|
||||
BasicMixin.creds = Credentials(person_id=10, person_name='Berkay Super User')
|
||||
BasicMixin.creds = Credentials(person_id=10, person_name="Berkay Super User")
|
||||
|
||||
|
||||
if listing:
|
||||
"""List Options and Queries """
|
||||
AddressNeighborhood.pre_query = AddressNeighborhood.filter_all(
|
||||
AddressNeighborhood.neighborhood_code.icontains('10'),
|
||||
"""List Options and Queries"""
|
||||
AddressNeighborhood.pre_query = AddressNeighborhood.filter_all(
|
||||
AddressNeighborhood.neighborhood_code.icontains("10"),
|
||||
db=new_session,
|
||||
).query
|
||||
query_of_list_options = {
|
||||
@@ -32,18 +31,20 @@ if listing:
|
||||
pagination = Pagination(data=address_neighborhoods)
|
||||
pagination.page = 9
|
||||
pagination.size = 10
|
||||
pagination.orderField = ['type_code','neighborhood_code']
|
||||
pagination.orderType = ['desc', 'asc']
|
||||
pagination.orderField = ["type_code", "neighborhood_code"]
|
||||
pagination.orderType = ["desc", "asc"]
|
||||
|
||||
pagination_result = PaginationResult(data=address_neighborhoods, pagination=pagination)
|
||||
pagination_result = PaginationResult(
|
||||
data=address_neighborhoods, pagination=pagination
|
||||
)
|
||||
print(pagination_result.pagination.as_dict())
|
||||
print(pagination_result.data)
|
||||
|
||||
if creating:
|
||||
"""Create Queries """
|
||||
"""Create Queries"""
|
||||
find_or_create = AddressNeighborhood.find_or_create(
|
||||
neighborhood_code='100',
|
||||
neighborhood_name='Test',
|
||||
neighborhood_code="100",
|
||||
neighborhood_name="Test",
|
||||
locality_id=15334,
|
||||
db=new_session,
|
||||
)
|
||||
@@ -51,26 +52,26 @@ if creating:
|
||||
find_or_create.destroy(db=new_session)
|
||||
find_or_create.save_via_metadata(db=new_session)
|
||||
find_or_create = AddressNeighborhood.find_or_create(
|
||||
neighborhood_code='100',
|
||||
neighborhood_name='Test',
|
||||
neighborhood_code="100",
|
||||
neighborhood_name="Test",
|
||||
locality_id=15334,
|
||||
db=new_session,
|
||||
)
|
||||
find_or_create.save_via_metadata(db=new_session)
|
||||
|
||||
if updating:
|
||||
"""Update Queries """
|
||||
"""Update Queries"""
|
||||
|
||||
query_of_list_options = {
|
||||
"uu_id": str("33a89767-d2dc-4531-8f66-7b650e22a8a7"),
|
||||
}
|
||||
print('query_of_list_options', query_of_list_options)
|
||||
print("query_of_list_options", query_of_list_options)
|
||||
address_neighborhoods_one = AddressNeighborhood.filter_one(
|
||||
*AddressNeighborhood.convert(query_of_list_options),
|
||||
db=new_session,
|
||||
).data
|
||||
address_neighborhoods_one.update(
|
||||
neighborhood_name='Test 44',
|
||||
neighborhood_name="Test 44",
|
||||
db=new_session,
|
||||
)
|
||||
address_neighborhoods_one.save(db=new_session)
|
||||
@@ -78,4 +79,4 @@ if updating:
|
||||
*AddressNeighborhood.convert(query_of_list_options),
|
||||
db=new_session,
|
||||
).data_as_dict
|
||||
print('address_neighborhoods_one', address_neighborhoods_one)
|
||||
print("address_neighborhoods_one", address_neighborhoods_one)
|
||||
|
||||
Reference in New Issue
Block a user