thread: add a reference to the owner post

This commit is contained in:
Lephe 2020-08-02 11:01:08 +02:00
parent b51ca8291f
commit 03c577316f
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
3 changed files with 17 additions and 2 deletions

View File

@ -16,7 +16,8 @@ class Program(Post):
# TODO: Compatible calculator models
thread_id = db.Column(db.Integer,db.ForeignKey('thread.id'),nullable=False)
thread = db.relationship('Thread', foreign_keys=thread_id)
thread = db.relationship('Thread', foreign_keys=thread_id,
back_populates='owner_program')
# TODO: Number of views, statistics, attached files, etc

View File

@ -12,6 +12,11 @@ class Thread(db.Model):
top_comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'))
top_comment = db.relationship('Comment', foreign_keys=top_comment_id)
# Post owning the thread, set only by Topic, Program, etc. In general, you
# should use [owner_post] which groups them together.
owner_topic = db.relationship('Topic')
owner_program = db.relationship('Program')
# Other fields populated automatically through relations:
# <comments> The list of comments (of type Comment)
@ -39,5 +44,13 @@ class Thread(db.Model):
self.top_comment = top_comment
@property
def owner_post(self):
if self.owner_topic != []:
return self.owner_topic[0]
if self.owner_program != []:
return self.owner_program[0]
return None
def __repr__(self):
return f'<Thread: #{self.id}>'

View File

@ -26,7 +26,8 @@ class Topic(Post):
# Associated thread
thread_id = db.Column(db.Integer,db.ForeignKey('thread.id'),nullable=False)
thread = db.relationship('Thread', foreign_keys=thread_id)
thread = db.relationship('Thread', foreign_keys=thread_id,
back_populates='owner_topic')
# Number of views in the forum
views = db.Column(db.Integer)