2019-08-20 17:34:00 +02:00
|
|
|
from app import db
|
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
class Thread(db.Model):
|
|
|
|
"""Some thread, such as a topic, program, tutorial."""
|
2019-08-20 17:34:00 +02:00
|
|
|
|
|
|
|
__tablename__ = 'thread'
|
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
# Unique ID
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
|
|
|
|
|
|
# Top comment
|
2021-07-07 19:38:28 +02:00
|
|
|
top_comment_id = db.Column(db.Integer,
|
|
|
|
db.ForeignKey('comment.id', use_alter=True))
|
2020-08-05 22:52:56 +02:00
|
|
|
top_comment = db.relationship('Comment', foreign_keys=top_comment_id)
|
2019-08-20 17:34:00 +02:00
|
|
|
|
2020-08-02 11:01:08 +02:00
|
|
|
# Post owning the thread, set only by Topic, Program, etc. In general, you
|
|
|
|
# should use [owner_post] which groups them together.
|
2020-08-05 22:52:56 +02:00
|
|
|
owner_topic = db.relationship('Topic')
|
2020-08-02 11:01:08 +02:00
|
|
|
owner_program = db.relationship('Program')
|
|
|
|
|
2019-11-21 15:31:46 +01:00
|
|
|
# Other fields populated automatically through relations:
|
|
|
|
# <comments> The list of comments (of type Comment)
|
2019-09-08 14:34:51 +02:00
|
|
|
|
2020-07-26 16:50:07 +02:00
|
|
|
COMMENTS_PER_PAGE = 20
|
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
def __init__(self):
|
|
|
|
"""
|
|
|
|
Create a empty Thread. Normally threads are not meant to be empty, so
|
|
|
|
you should create a Comment with this thread as parent, then assign it
|
|
|
|
as top comment with a call to set_top_comment().
|
|
|
|
"""
|
|
|
|
self.top_comment_id = None
|
|
|
|
|
|
|
|
def set_top_comment(self, top_comment):
|
|
|
|
"""
|
2019-11-21 15:31:46 +01:00
|
|
|
Changes the top comment of the thread. The old top comment will usually
|
|
|
|
become visible in the flow of posts instead of being pinned at the top.
|
2019-09-08 14:34:51 +02:00
|
|
|
|
|
|
|
Arguments:
|
|
|
|
top_comment -- new top comment, must belong to this thread
|
|
|
|
"""
|
|
|
|
|
|
|
|
if top_comment not in self.comments:
|
|
|
|
raise Exception("Cannot set foreign comment as top thread comment")
|
|
|
|
|
|
|
|
self.top_comment = top_comment
|
2019-08-20 17:34:00 +02:00
|
|
|
|
2020-08-02 11:01:08 +02:00
|
|
|
@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
|
|
|
|
|
2022-04-26 23:29:11 +02:00
|
|
|
def is_default_accessible(self):
|
|
|
|
if self.owner_program != []:
|
|
|
|
return True
|
|
|
|
if self.owner_topic != []:
|
|
|
|
return self.owner_topic[0].forum.is_default_accessible()
|
|
|
|
return False
|
|
|
|
|
2020-11-01 10:49:24 +01:00
|
|
|
def delete(self):
|
|
|
|
"""Recursively delete thread and all associated contents."""
|
|
|
|
# Remove reference to top comment
|
|
|
|
self.top_comment = None
|
|
|
|
db.session.add(self)
|
|
|
|
db.session.commit()
|
|
|
|
# Remove comments
|
|
|
|
for c in self.comments:
|
|
|
|
c.delete()
|
|
|
|
# Remove thread
|
|
|
|
db.session.commit()
|
|
|
|
db.session.delete(self)
|
|
|
|
|
2019-08-20 17:34:00 +02:00
|
|
|
def __repr__(self):
|
2019-09-08 14:34:51 +02:00
|
|
|
return f'<Thread: #{self.id}>'
|