updated events initializer
This commit is contained in:
@@ -1,23 +1,13 @@
|
||||
import arrow
|
||||
import datetime
|
||||
|
||||
from typing import Optional, Any, Dict, List
|
||||
from sqlalchemy.orm import Session, Mapped
|
||||
from pydantic import BaseModel
|
||||
from fastapi.exceptions import HTTPException
|
||||
from typing import Optional, Any, Dict
|
||||
from decimal import Decimal
|
||||
from fastapi.exceptions import HTTPException
|
||||
|
||||
from sqlalchemy import TIMESTAMP, NUMERIC
|
||||
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
||||
|
||||
|
||||
class Credentials(BaseModel):
|
||||
"""
|
||||
Class to store user credentials.
|
||||
"""
|
||||
|
||||
person_id: int
|
||||
person_name: str
|
||||
full_name: Optional[str] = None
|
||||
from sqlalchemy.orm import Session, Mapped
|
||||
|
||||
|
||||
class MetaData:
|
||||
@@ -27,6 +17,7 @@ class MetaData:
|
||||
|
||||
created: bool = False
|
||||
updated: bool = False
|
||||
deleted: bool = False
|
||||
|
||||
|
||||
class CRUDModel:
|
||||
@@ -43,7 +34,7 @@ class CRUDModel:
|
||||
|
||||
__abstract__ = True
|
||||
|
||||
creds: Credentials = None
|
||||
# creds: Credentials = None
|
||||
meta_data: MetaData = MetaData()
|
||||
|
||||
# Define required columns for CRUD operations
|
||||
@@ -57,23 +48,6 @@ class CRUDModel:
|
||||
"deleted": bool,
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def create_credentials(cls, record_created) -> None:
|
||||
"""
|
||||
Save user credentials for tracking.
|
||||
|
||||
Args:
|
||||
record_created: Record that created or updated
|
||||
"""
|
||||
if not cls.creds:
|
||||
return
|
||||
|
||||
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
|
||||
@@ -126,7 +100,6 @@ class CRUDModel:
|
||||
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
|
||||
@@ -194,6 +167,7 @@ class CRUDModel:
|
||||
return False, None
|
||||
|
||||
except Exception as e:
|
||||
err = e
|
||||
return False, None
|
||||
|
||||
def get_dict(
|
||||
@@ -234,6 +208,7 @@ class CRUDModel:
|
||||
return return_dict
|
||||
|
||||
except Exception as e:
|
||||
err = e
|
||||
return {}
|
||||
|
||||
@classmethod
|
||||
@@ -278,7 +253,6 @@ class CRUDModel:
|
||||
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
|
||||
@@ -308,7 +282,6 @@ class CRUDModel:
|
||||
for key, value in kwargs.items():
|
||||
setattr(self, key, value)
|
||||
|
||||
self.update_credentials()
|
||||
db.flush()
|
||||
self.meta_data.updated = True
|
||||
return self
|
||||
@@ -317,17 +290,3 @@ class CRUDModel:
|
||||
self.meta_data.updated = False
|
||||
db.rollback()
|
||||
self.raise_exception(f"Failed to update record: {str(e)}", status_code=500)
|
||||
|
||||
def update_credentials(self) -> None:
|
||||
"""
|
||||
Save user credentials for tracking.
|
||||
"""
|
||||
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
|
||||
|
||||
@@ -72,27 +72,6 @@ class CrudMixin(BasicMixin):
|
||||
comment="Record validity end timestamp",
|
||||
)
|
||||
|
||||
|
||||
class CrudCollection(CrudMixin):
|
||||
"""
|
||||
Full-featured model class with all common fields.
|
||||
|
||||
Includes:
|
||||
- UUID and reference ID
|
||||
- Timestamps
|
||||
- User tracking
|
||||
- Confirmation status
|
||||
- Soft delete
|
||||
- Notification flags
|
||||
"""
|
||||
|
||||
__abstract__ = True
|
||||
__repr__ = ReprMixin.__repr__
|
||||
|
||||
ref_id: Mapped[str] = mapped_column(
|
||||
String(100), nullable=True, index=True, comment="External reference ID"
|
||||
)
|
||||
|
||||
# Timestamps
|
||||
created_at: Mapped[TIMESTAMP] = mapped_column(
|
||||
TIMESTAMP(timezone=True),
|
||||
@@ -110,36 +89,51 @@ class CrudCollection(CrudMixin):
|
||||
comment="Last update timestamp",
|
||||
)
|
||||
|
||||
|
||||
class CrudCollection(CrudMixin):
|
||||
"""
|
||||
Full-featured model class with all common fields.
|
||||
|
||||
Includes:
|
||||
- UUID and reference ID
|
||||
- Timestamps
|
||||
- User tracking
|
||||
- Confirmation status
|
||||
- Soft delete
|
||||
- Notification flags
|
||||
"""
|
||||
|
||||
__abstract__ = True
|
||||
__repr__ = ReprMixin.__repr__
|
||||
|
||||
# Outer reference fields
|
||||
ref_id: Mapped[str] = mapped_column(
|
||||
String(100), nullable=True, index=True, comment="External reference ID"
|
||||
)
|
||||
replication_id: Mapped[int] = mapped_column(
|
||||
SmallInteger, server_default="0", comment="Replication identifier"
|
||||
)
|
||||
|
||||
# Cryptographic and user tracking
|
||||
cryp_uu_id: Mapped[str] = mapped_column(
|
||||
String, nullable=True, index=True, comment="Cryptographic UUID"
|
||||
)
|
||||
# created_by: Mapped[str] = mapped_column(
|
||||
# String, nullable=True, comment="Creator name"
|
||||
# )
|
||||
# created_by_id: Mapped[int] = mapped_column(
|
||||
# Integer, nullable=True, comment="Creator ID"
|
||||
# )
|
||||
# updated_by: Mapped[str] = mapped_column(
|
||||
# String, nullable=True, comment="Last modifier name"
|
||||
# )
|
||||
# updated_by_id: Mapped[int] = mapped_column(
|
||||
# Integer, nullable=True, comment="Last modifier ID"
|
||||
# )
|
||||
confirmed_by: Mapped[str] = mapped_column(
|
||||
String, nullable=True, comment="Confirmer name"
|
||||
|
||||
# Token fields of modification
|
||||
created_credentials_token: Mapped[str] = mapped_column(
|
||||
String, nullable=True, comment="Created Credentials token"
|
||||
)
|
||||
confirmed_by_id: Mapped[int] = mapped_column(
|
||||
Integer, nullable=True, comment="Confirmer ID"
|
||||
updated_credentials_token: Mapped[str] = mapped_column(
|
||||
String, nullable=True, comment="Updated Credentials token"
|
||||
)
|
||||
confirmed_credentials_token: Mapped[str] = mapped_column(
|
||||
String, nullable=True, comment="Confirmed Credentials token"
|
||||
)
|
||||
|
||||
# Status flags
|
||||
is_confirmed: Mapped[bool] = mapped_column(
|
||||
Boolean, server_default="0", comment="Record confirmation status"
|
||||
)
|
||||
replication_id: Mapped[int] = mapped_column(
|
||||
SmallInteger, server_default="0", comment="Replication identifier"
|
||||
)
|
||||
deleted: Mapped[bool] = mapped_column(
|
||||
Boolean, server_default="0", comment="Soft delete flag"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user