32 lines
1.1 KiB
Python
32 lines
1.1 KiB
Python
from sqlalchemy import Column, Integer, DateTime, Boolean, String, JSON, func
|
|
from sqlalchemy.orm import declarative_mixin, declarative_base
|
|
|
|
Base = declarative_base()
|
|
|
|
@declarative_mixin
|
|
class CRUDMixin:
|
|
id = Column(Integer, primary_key=True)
|
|
created_at = Column(DateTime, default=func.now())
|
|
updated_at = Column(DateTime, default=func.now(), onupdate=func.now())
|
|
|
|
def as_dict(self):
|
|
# Combine all columns from all inherited tables
|
|
result = {}
|
|
for cls in self.__class__.__mro__:
|
|
if hasattr(cls, "__table__"):
|
|
for column in cls.__table__.columns:
|
|
result[column.name] = getattr(self, column.name)
|
|
return result
|
|
|
|
class Version(Base):
|
|
__tablename__ = "versions"
|
|
|
|
id = Column(Integer, primary_key=True)
|
|
model_name = Column(String, nullable=False)
|
|
object_id = Column(Integer, nullable=False)
|
|
change_type = Column(String, nullable=False)
|
|
data = Column(JSON, nullable=True)
|
|
timestamp = Column(DateTime, default=func.now())
|
|
|
|
actor = Column(String, nullable=True)
|
|
meta = Column('metadata', JSON, nullable=True)
|