From 03c577316f2a9eaa71acc9dfc3466ada8913ec80 Mon Sep 17 00:00:00 2001 From: Lephe Date: Sun, 2 Aug 2020 11:01:08 +0200 Subject: [PATCH] thread: add a reference to the owner post --- app/models/program.py | 3 ++- app/models/thread.py | 13 +++++++++++++ app/models/topic.py | 3 ++- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/app/models/program.py b/app/models/program.py index b16a484..ef762f2 100644 --- a/app/models/program.py +++ b/app/models/program.py @@ -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 diff --git a/app/models/thread.py b/app/models/thread.py index 1f69f3c..6b62341 100644 --- a/app/models/thread.py +++ b/app/models/thread.py @@ -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: # 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'' diff --git a/app/models/topic.py b/app/models/topic.py index 2c656d3..7b9465f 100644 --- a/app/models/topic.py +++ b/app/models/topic.py @@ -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)