auth endpoints added
This commit is contained in:
@@ -14,6 +14,7 @@ class Credentials(BaseModel):
|
||||
"""
|
||||
Class to store user credentials.
|
||||
"""
|
||||
|
||||
person_id: int
|
||||
person_name: str
|
||||
full_name: Optional[str] = None
|
||||
@@ -23,6 +24,7 @@ class MetaData:
|
||||
"""
|
||||
Class to store metadata for a query.
|
||||
"""
|
||||
|
||||
created: bool = False
|
||||
updated: bool = False
|
||||
|
||||
@@ -30,7 +32,7 @@ class MetaData:
|
||||
class CRUDModel:
|
||||
"""
|
||||
Base class for CRUD operations on PostgreSQL models.
|
||||
|
||||
|
||||
Features:
|
||||
- User credential tracking
|
||||
- Metadata tracking for operations
|
||||
@@ -38,21 +40,21 @@ class CRUDModel:
|
||||
- Automatic timestamp management
|
||||
- Soft delete support
|
||||
"""
|
||||
|
||||
|
||||
__abstract__ = True
|
||||
|
||||
creds: Credentials = None
|
||||
meta_data: MetaData = MetaData()
|
||||
|
||||
|
||||
# Define required columns for CRUD operations
|
||||
required_columns = {
|
||||
'expiry_starts': TIMESTAMP,
|
||||
'expiry_ends': TIMESTAMP,
|
||||
'created_by': str,
|
||||
'created_by_id': int,
|
||||
'updated_by': str,
|
||||
'updated_by_id': int,
|
||||
'deleted': bool
|
||||
"expiry_starts": TIMESTAMP,
|
||||
"expiry_ends": TIMESTAMP,
|
||||
"created_by": str,
|
||||
"created_by_id": int,
|
||||
"updated_by": str,
|
||||
"updated_by_id": int,
|
||||
"deleted": bool,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
@@ -65,24 +67,25 @@ class CRUDModel:
|
||||
"""
|
||||
if not cls.creds:
|
||||
return
|
||||
|
||||
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
|
||||
|
||||
|
||||
@classmethod
|
||||
def raise_exception(cls, message: str = "Exception raised.", status_code: int = 400):
|
||||
def raise_exception(
|
||||
cls, message: str = "Exception raised.", status_code: int = 400
|
||||
):
|
||||
"""
|
||||
Raise HTTP exception with custom message and status code.
|
||||
|
||||
|
||||
Args:
|
||||
message: Error message
|
||||
status_code: HTTP status code
|
||||
"""
|
||||
raise HTTPException(
|
||||
status_code=status_code,
|
||||
detail={"message": message}
|
||||
)
|
||||
raise HTTPException(status_code=status_code, detail={"message": message})
|
||||
|
||||
@classmethod
|
||||
def create_or_abort(cls, db: Session, **kwargs):
|
||||
@@ -111,7 +114,7 @@ class CRUDModel:
|
||||
query = query.filter(getattr(cls, key) == value)
|
||||
|
||||
already_record = query.first()
|
||||
|
||||
|
||||
# Handle existing record
|
||||
if already_record and already_record.deleted:
|
||||
cls.raise_exception("Record already exists and is deleted")
|
||||
@@ -122,12 +125,12 @@ class CRUDModel:
|
||||
created_record = cls()
|
||||
for key, value in kwargs.items():
|
||||
setattr(created_record, key, value)
|
||||
|
||||
|
||||
cls.create_credentials(created_record)
|
||||
db.add(created_record)
|
||||
db.flush()
|
||||
return created_record
|
||||
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
cls.raise_exception(f"Failed to create record: {str(e)}", status_code=500)
|
||||
@@ -146,7 +149,7 @@ class CRUDModel:
|
||||
"""
|
||||
try:
|
||||
key_ = cls.__annotations__.get(key, None)
|
||||
is_primary = key in getattr(cls, 'primary_keys', [])
|
||||
is_primary = key in getattr(cls, "primary_keys", [])
|
||||
row_attr = bool(getattr(getattr(cls, key), "foreign_keys", None))
|
||||
|
||||
# Skip primary keys and foreign keys
|
||||
@@ -167,12 +170,16 @@ class CRUDModel:
|
||||
elif key_ == Mapped[float] or key_ == Mapped[NUMERIC]:
|
||||
return True, round(float(val), 3)
|
||||
elif key_ == Mapped[TIMESTAMP]:
|
||||
return True, str(arrow.get(str(val)).format("YYYY-MM-DD HH:mm:ss ZZ"))
|
||||
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
|
||||
if isinstance(val, datetime.datetime):
|
||||
return True, str(arrow.get(str(val)).format("YYYY-MM-DD HH:mm:ss ZZ"))
|
||||
return True, str(
|
||||
arrow.get(str(val)).format("YYYY-MM-DD HH:mm:ss ZZ")
|
||||
)
|
||||
elif isinstance(val, bool):
|
||||
return True, bool(val)
|
||||
elif isinstance(val, (float, Decimal)):
|
||||
@@ -185,17 +192,19 @@ class CRUDModel:
|
||||
return True, None
|
||||
|
||||
return False, None
|
||||
|
||||
|
||||
except Exception as e:
|
||||
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.
|
||||
|
||||
|
||||
Args:
|
||||
exclude_list: List of fields to exclude from the dictionary
|
||||
|
||||
|
||||
Returns:
|
||||
Dictionary representation of the model
|
||||
"""
|
||||
@@ -207,7 +216,7 @@ class CRUDModel:
|
||||
# Get all column names from the model
|
||||
columns = [col.name for col in self.__table__.columns]
|
||||
columns_set = set(columns)
|
||||
|
||||
|
||||
# Filter columns
|
||||
columns_list = set([col for col in columns_set if str(col)[-2:] != "id"])
|
||||
columns_extend = set(
|
||||
@@ -223,7 +232,7 @@ class CRUDModel:
|
||||
return_dict[key] = value_of_database
|
||||
|
||||
return return_dict
|
||||
|
||||
|
||||
except Exception as e:
|
||||
return {}
|
||||
|
||||
@@ -251,10 +260,10 @@ class CRUDModel:
|
||||
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]
|
||||
|
||||
|
||||
for key, value in kwargs.items():
|
||||
if hasattr(cls, key) and key not in exclude_args:
|
||||
query = query.filter(getattr(cls, key) == value)
|
||||
@@ -268,16 +277,18 @@ class CRUDModel:
|
||||
created_record = cls()
|
||||
for key, value in kwargs.items():
|
||||
setattr(created_record, key, value)
|
||||
|
||||
|
||||
cls.create_credentials(created_record)
|
||||
db.add(created_record)
|
||||
db.flush()
|
||||
cls.meta_data.created = True
|
||||
return created_record
|
||||
|
||||
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
cls.raise_exception(f"Failed to find or create record: {str(e)}", status_code=500)
|
||||
cls.raise_exception(
|
||||
f"Failed to find or create record: {str(e)}", status_code=500
|
||||
)
|
||||
|
||||
def update(self, db: Session, **kwargs):
|
||||
"""
|
||||
@@ -301,7 +312,7 @@ class CRUDModel:
|
||||
db.flush()
|
||||
self.meta_data.updated = True
|
||||
return self
|
||||
|
||||
|
||||
except Exception as e:
|
||||
self.meta_data.updated = False
|
||||
db.rollback()
|
||||
@@ -313,10 +324,10 @@ class CRUDModel:
|
||||
"""
|
||||
if not self.creds:
|
||||
return
|
||||
|
||||
|
||||
person_id = getattr(self.creds, "person_id", None)
|
||||
person_name = getattr(self.creds, "person_name", None)
|
||||
|
||||
|
||||
if person_id and person_name:
|
||||
self.updated_by_id = self.creds.person_id
|
||||
self.updated_by = self.creds.person_name
|
||||
|
||||
Reference in New Issue
Block a user