updated and cleaned
This commit is contained in:
@@ -15,6 +15,7 @@ class Credentials(BaseModel):
|
||||
"""
|
||||
Class to store user credentials.
|
||||
"""
|
||||
|
||||
person_id: int
|
||||
person_name: str
|
||||
full_name: Optional[str] = None
|
||||
@@ -24,6 +25,7 @@ class MetaData:
|
||||
"""
|
||||
Class to store metadata for a query.
|
||||
"""
|
||||
|
||||
created: bool = False
|
||||
updated: bool = False
|
||||
|
||||
@@ -43,7 +45,9 @@ class CRUDModel:
|
||||
Args:
|
||||
record_created: Record that created or updated
|
||||
"""
|
||||
if getattr(cls.creds, "person_id", None) and getattr(cls.creds, "person_name", None):
|
||||
if getattr(cls.creds, "person_id", None) and getattr(
|
||||
cls.creds, "person_name", None
|
||||
):
|
||||
record_created.created_by_id = cls.creds.person_id
|
||||
record_created.created_by = cls.creds.person_name
|
||||
return
|
||||
@@ -72,7 +76,8 @@ class CRUDModel:
|
||||
|
||||
# Search for existing record
|
||||
query = db.query(cls).filter(
|
||||
cls.expiry_ends > str(arrow.now()), cls.expiry_starts <= str(arrow.now()),
|
||||
cls.expiry_ends > str(arrow.now()),
|
||||
cls.expiry_starts <= str(arrow.now()),
|
||||
)
|
||||
|
||||
for key, value in kwargs.items():
|
||||
@@ -119,7 +124,7 @@ class CRUDModel:
|
||||
if str(key[-5:]).lower() == "uu_id": # Special handling for UUID fields
|
||||
return True, str(val)
|
||||
|
||||
if key_: # Handle typed fields
|
||||
if key_: # Handle typed fields
|
||||
if key_ == Mapped[int]:
|
||||
return True, int(val)
|
||||
elif key_ == Mapped[bool]:
|
||||
@@ -130,7 +135,7 @@ class CRUDModel:
|
||||
return True, str(arrow.get(str(val)).format("YYYY-MM-DD HH:mm:ss ZZ"))
|
||||
elif key_ == Mapped[str]:
|
||||
return True, str(val)
|
||||
else: # Handle based on Python types
|
||||
else: # Handle based on Python types
|
||||
if isinstance(val, datetime.datetime):
|
||||
return True, str(arrow.get(str(val)).format("YYYY-MM-DD HH:mm:ss ZZ"))
|
||||
elif isinstance(val, bool):
|
||||
@@ -146,20 +151,24 @@ class CRUDModel:
|
||||
|
||||
return False, None
|
||||
|
||||
def get_dict(self, exclude_list: Optional[list[InstrumentedAttribute]] = None) -> Dict[str, Any]:
|
||||
def get_dict(
|
||||
self, exclude_list: Optional[list[InstrumentedAttribute]] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Convert model instance to dictionary with customizable fields.
|
||||
Returns:
|
||||
Dictionary representation of the model
|
||||
Dictionary returns only UUID fields and fields that are not in exclude_list
|
||||
"""
|
||||
return_dict: Dict[str, Any] = {} # Handle default field selection
|
||||
return_dict: Dict[str, Any] = {} # Handle default field selection
|
||||
exclude_list = exclude_list or []
|
||||
exclude_list = [exclude_arg.key for exclude_arg in exclude_list]
|
||||
|
||||
columns_set = set(self.columns)
|
||||
columns_list = set([col for col in list(columns_set) if str(col)[-2:] != "id"])
|
||||
columns_extend = set(col for col in list(columns_set) if str(col)[-5:].lower() == "uu_id")
|
||||
columns_extend = set(
|
||||
col for col in list(columns_set) if str(col)[-5:].lower() == "uu_id"
|
||||
)
|
||||
columns_list = set(columns_list) | set(columns_extend)
|
||||
columns_list = list(set(columns_list) - set(exclude_list))
|
||||
|
||||
@@ -173,7 +182,10 @@ class CRUDModel:
|
||||
|
||||
@classmethod
|
||||
def find_or_create(
|
||||
cls, db: Session, exclude_args: Optional[list[InstrumentedAttribute]] = None, **kwargs
|
||||
cls,
|
||||
db: Session,
|
||||
exclude_args: Optional[list[InstrumentedAttribute]] = None,
|
||||
**kwargs,
|
||||
):
|
||||
"""
|
||||
Find an existing record matching the criteria or create a new one.
|
||||
@@ -188,7 +200,8 @@ class CRUDModel:
|
||||
"""
|
||||
# Search for existing record
|
||||
query = db.query(cls).filter(
|
||||
cls.expiry_ends > str(arrow.now()), cls.expiry_starts <= str(arrow.now()),
|
||||
cls.expiry_ends > str(arrow.now()),
|
||||
cls.expiry_starts <= str(arrow.now()),
|
||||
)
|
||||
exclude_args = exclude_args or []
|
||||
exclude_args = [exclude_arg.key for exclude_arg in exclude_args]
|
||||
@@ -231,7 +244,7 @@ class CRUDModel:
|
||||
db.flush()
|
||||
self.meta_data.updated = True
|
||||
except Exception as e:
|
||||
print('Error:', e)
|
||||
print("Error:", e)
|
||||
self.meta_data.updated = False
|
||||
db.rollback()
|
||||
return self
|
||||
|
||||
@@ -8,8 +8,7 @@ from Services.PostgresService.controllers.response_controllers import PostgresRe
|
||||
from Configs.api import ApiConfigs
|
||||
|
||||
|
||||
class ListOptions:
|
||||
...
|
||||
class ListOptions: ...
|
||||
|
||||
|
||||
class PaginationConfig(BaseModel):
|
||||
@@ -164,25 +163,29 @@ class PaginationResult:
|
||||
"Order by fields and order types must have the same length."
|
||||
)
|
||||
order_criteria = zip(self.order_by, self.order_type)
|
||||
print('order_criteria', order_criteria)
|
||||
print("order_criteria", order_criteria)
|
||||
if not self._data.data:
|
||||
return self._core_query
|
||||
|
||||
for field, direction in order_criteria:
|
||||
print('field', field, direction)
|
||||
print("field", field, direction)
|
||||
columns = self._data.data[0].filterable_attributes
|
||||
print('columns', columns)
|
||||
print("columns", columns)
|
||||
if field in columns:
|
||||
if direction.lower().startswith("d"):
|
||||
self._core_query = self._core_query.order_by(
|
||||
desc(
|
||||
getattr(self._core_query.column_descriptions[0]["entity"], field)
|
||||
getattr(
|
||||
self._core_query.column_descriptions[0]["entity"], field
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
self._core_query = self._core_query.order_by(
|
||||
asc(
|
||||
getattr(self._core_query.column_descriptions[0]["entity"], field)
|
||||
getattr(
|
||||
self._core_query.column_descriptions[0]["entity"], field
|
||||
)
|
||||
)
|
||||
)
|
||||
return self._core_query
|
||||
|
||||
Reference in New Issue
Block a user