Browse Source

Track whether topics were posted by a schedule

merge-requests/85/head
Deimos 5 years ago
parent
commit
96a1eb2148
  1. 38
      tildes/alembic/versions/9fc0033a2b61_topic_add_schedule_id_column.py
  2. 3
      tildes/tildes/models/topic/topic.py
  3. 5
      tildes/tildes/models/topic/topic_schedule.py

38
tildes/alembic/versions/9fc0033a2b61_topic_add_schedule_id_column.py

@ -0,0 +1,38 @@
"""Topic: add schedule_id column
Revision ID: 9fc0033a2b61
Revises: 8326f8cc5ddd
Create Date: 2019-10-12 01:51:26.045258
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "9fc0033a2b61"
down_revision = "8326f8cc5ddd"
branch_labels = None
depends_on = None
def upgrade():
op.add_column("topics", sa.Column("schedule_id", sa.Integer(), nullable=True))
op.create_index(
op.f("ix_topics_schedule_id"), "topics", ["schedule_id"], unique=False
)
op.create_foreign_key(
op.f("fk_topics_schedule_id_topic_schedule"),
"topics",
"topic_schedule",
["schedule_id"],
["schedule_id"],
)
def downgrade():
op.drop_constraint(
op.f("fk_topics_schedule_id_topic_schedule"), "topics", type_="foreignkey"
)
op.drop_index(op.f("ix_topics_schedule_id"), table_name="topics")
op.drop_column("topics", "schedule_id")

3
tildes/tildes/models/topic/topic.py

@ -78,6 +78,9 @@ class Topic(DatabaseModel):
user_id: int = Column( user_id: int = Column(
Integer, ForeignKey("users.user_id"), nullable=False, index=True Integer, ForeignKey("users.user_id"), nullable=False, index=True
) )
schedule_id: int = Column(
Integer, ForeignKey("topic_schedule.schedule_id"), index=True
)
created_time: datetime = Column( created_time: datetime = Column(
TIMESTAMP(timezone=True), TIMESTAMP(timezone=True),
nullable=False, nullable=False,

5
tildes/tildes/models/topic/topic_schedule.py

@ -8,7 +8,7 @@ from typing import List, Optional
from dateutil.rrule import rrule from dateutil.rrule import rrule
from sqlalchemy import CheckConstraint, Column, ForeignKey, Integer, Text, TIMESTAMP from sqlalchemy import CheckConstraint, Column, ForeignKey, Integer, Text, TIMESTAMP
from sqlalchemy.orm import relationship
from sqlalchemy.orm import backref, relationship
from sqlalchemy.orm.session import Session from sqlalchemy.orm.session import Session
from sqlalchemy.sql.expression import text from sqlalchemy.sql.expression import text
@ -48,6 +48,8 @@ class TopicSchedule(DatabaseModel):
group: Group = relationship("Group", innerjoin=True) group: Group = relationship("Group", innerjoin=True)
user: Optional[User] = relationship("User") user: Optional[User] = relationship("User")
topics: List[Topic] = relationship(Topic, backref=backref("schedule"))
def __init__( def __init__(
self, self,
group: Group, group: Group,
@ -82,6 +84,7 @@ class TopicSchedule(DatabaseModel):
topic = Topic.create_text_topic(self.group, user, self.title, self.markdown) topic = Topic.create_text_topic(self.group, user, self.title, self.markdown)
topic.tags = self.tags topic.tags = self.tags
topic.schedule = self
return topic return topic

Loading…
Cancel
Save