core/orm/topic.py

36 lines
1.6 KiB
Python
Raw Normal View History

2021-08-20 08:08:32 +00:00
from datetime import datetime
2021-08-20 09:27:19 +00:00
from sqlalchemy import Table, Column, Integer, String, ForeignKey, DateTime, UniqueConstraint
2021-08-26 21:14:20 +00:00
from sqlalchemy.orm import relationship
2021-08-20 08:08:32 +00:00
from orm.base import Base
2021-08-20 09:27:19 +00:00
Connection = Table('topic_connections',
Base.metadata,
2021-08-25 21:20:53 +00:00
Column('child', Integer, ForeignKey('topic.id')),
Column('parent', Integer, ForeignKey('topic.id')),
2021-08-20 09:27:19 +00:00
UniqueConstraint('parent', 'child', name='unique_usage')
)
2021-10-28 10:42:34 +00:00
class TopicSubscription(Base):
2021-10-31 10:58:21 +00:00
__tablename__ = "topic_subscription"
2021-10-28 10:42:34 +00:00
id = None
2021-11-10 08:42:29 +00:00
topic = Column(ForeignKey('topic.id'), primary_key = True)
2021-10-28 10:42:34 +00:00
user = Column(ForeignKey('user.id'), primary_key = True)
createdAt: str = Column(DateTime, nullable=False, default = datetime.now, comment="Created at")
2021-08-20 09:27:19 +00:00
2021-08-20 08:08:32 +00:00
class Topic(Base):
__tablename__ = 'topic'
2021-08-25 21:20:53 +00:00
slug: str = Column(String, unique = True, nullable = False)
2021-10-14 05:28:52 +00:00
title: str = Column(String, nullable=False, comment="Title")
body: str = Column(String, nullable=True, comment="Body")
pic: str = Column(String, nullable=True, comment="Picture")
cat_id: str = Column(String, nullable=True, comment="Old Category ID")
2021-08-20 09:27:19 +00:00
# list of Topics where the current node is the "other party" or "child"
parents = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.parent, secondaryjoin=slug==Connection.c.child, viewonly=True)
# list of Topics where the current node is the "parent"
children = relationship(lambda: Topic, secondary=Connection, primaryjoin=slug==Connection.c.child, secondaryjoin=slug==Connection.c.parent)
2021-12-11 10:23:17 +00:00
community = Column(ForeignKey("community.slug"), nullable=False, comment="Community")
2021-08-20 09:30:52 +00:00