69 lines
2.1 KiB
Python
69 lines
2.1 KiB
Python
from datetime import UTC, datetime
|
|
|
|
from app import db
|
|
from app.models.base import SoftDeleteMixin
|
|
|
|
|
|
class CardLink(db.Model, SoftDeleteMixin):
|
|
"""CardLink model for bidirectional card-to-card relationships"""
|
|
|
|
__tablename__ = "card_links"
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
parent_card_id = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey("cards.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
child_card_id = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey("cards.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
created_by = db.Column(
|
|
db.Integer, db.ForeignKey("users.id"), nullable=True, index=True
|
|
)
|
|
|
|
# Timestamps
|
|
created_at = db.Column(db.DateTime, default=lambda: datetime.now(UTC))
|
|
|
|
# Relationships
|
|
parent_card = db.relationship(
|
|
"Card",
|
|
foreign_keys=[parent_card_id],
|
|
back_populates="child_links",
|
|
)
|
|
child_card = db.relationship(
|
|
"Card",
|
|
foreign_keys=[child_card_id],
|
|
back_populates="parent_links",
|
|
)
|
|
|
|
def to_dict(self, include_cards=False):
|
|
"""Convert card link to dictionary"""
|
|
result = {
|
|
"id": self.id,
|
|
"parent_card_id": self.parent_card_id,
|
|
"child_card_id": self.child_card_id,
|
|
"created_by": self.created_by,
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
"status": self.status,
|
|
"deleted_at": self.deleted_at.isoformat() if self.deleted_at else None,
|
|
}
|
|
if include_cards:
|
|
result["parent_card"] = (
|
|
self.parent_card.to_dict() if self.parent_card else None
|
|
)
|
|
result["child_card"] = (
|
|
self.child_card.to_dict() if self.child_card else None
|
|
)
|
|
return result
|
|
|
|
def __repr__(self):
|
|
return f"<CardLink {self.parent_card_id} -> {self.child_card_id}>"
|
|
|
|
__table_args__ = (
|
|
db.UniqueConstraint("parent_card_id", "child_card_id", name="unique_card_link"),
|
|
)
|