PCv5/app/models/thread.py

36 lines
1.1 KiB
Python
Raw Normal View History

2019-08-20 17:34:00 +02:00
from app import db
from app.models.post import Post
from app.models.comment import Comment
from config import V5Config
class Thread(Post):
""" Some thread, such as a topic, program, tutorial """
# Foreign Post object ID
2019-08-20 17:34:00 +02:00
__tablename__ = 'thread'
id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True)
# Identify threads as a type of posts using the table name, and add a
# column to further discriminate types of threads
2019-08-20 17:34:00 +02:00
thread_type = db.Column(db.String(20))
__mapper_args__ = {
'polymorphic_identity': __tablename__,
'polymorphic_on': thread_type
}
# Properties
2019-08-20 17:34:00 +02:00
title = db.Column(db.Unicode(V5Config.THREAD_NAME_MAXLEN))
# Also a relation [comments] populated from the Comment class.
2019-08-20 17:34:00 +02:00
# Relations
top_comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'))
top_comment = db.relationship('Comment', foreign_keys=top_comment_id)
2019-08-20 17:34:00 +02:00
def __init__(self, author, text, title):
""" Create a Thread """
Post.__init__(author, text)
2019-08-20 17:34:00 +02:00
self.title = title
def __repr__(self):
return f'<Thread #{self.id}'