59 lines
1.7 KiB
Python
59 lines
1.7 KiB
Python
from datetime import UTC, datetime
|
|
|
|
from app import db
|
|
from app.models.base import SoftDeleteMixin
|
|
|
|
|
|
class Comment(db.Model, SoftDeleteMixin):
|
|
"""Comment model for card comments"""
|
|
|
|
__tablename__ = "comments"
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
text = db.Column(db.Text, nullable=False)
|
|
|
|
# Foreign keys
|
|
card_id = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey("cards.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
user_id = db.Column(
|
|
db.Integer, db.ForeignKey("users.id"), nullable=False, index=True
|
|
)
|
|
|
|
# Timestamps
|
|
created_at = db.Column(db.DateTime, default=lambda: datetime.now(UTC))
|
|
updated_at = db.Column(
|
|
db.DateTime,
|
|
default=lambda: datetime.now(UTC),
|
|
onupdate=lambda: datetime.now(UTC),
|
|
)
|
|
|
|
# Relationships
|
|
attachments = db.relationship(
|
|
"FileAttachment",
|
|
foreign_keys="FileAttachment.attachable_id",
|
|
primaryjoin="""and_(FileAttachment.attachable_id == Comment.id,
|
|
FileAttachment.attachable_type == 'Comment')""",
|
|
cascade="all, delete-orphan",
|
|
lazy="dynamic",
|
|
overlaps="attachments",
|
|
)
|
|
|
|
def to_dict(self):
|
|
"""Convert comment to dictionary"""
|
|
return {
|
|
"id": self.id,
|
|
"text": self.text,
|
|
"card_id": self.card_id,
|
|
"user_id": self.user_id,
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
"updated_at": self.updated_at.isoformat() if self.updated_at else None,
|
|
"status": self.status,
|
|
"deleted_at": self.deleted_at.isoformat() if self.deleted_at else None,
|
|
}
|
|
|
|
def __repr__(self):
|
|
return f"<Comment id={self.id} card_id={self.card_id}>"
|