2019-08-20 17:34:00 +02:00
|
|
|
from app import db
|
|
|
|
from app.models.post import Post
|
2022-05-26 21:08:01 +02:00
|
|
|
from app.models.attachment import Attachment
|
2019-12-07 16:06:00 +01:00
|
|
|
from sqlalchemy.orm import backref
|
2019-08-20 17:34:00 +02:00
|
|
|
|
2021-01-12 16:40:52 +01:00
|
|
|
|
2019-08-20 17:34:00 +02:00
|
|
|
class Comment(Post):
|
|
|
|
__tablename__ = 'comment'
|
|
|
|
__mapper_args__ = {'polymorphic_identity': __tablename__}
|
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
# ID of the underlying Post object
|
2019-08-20 17:34:00 +02:00
|
|
|
id = db.Column(db.Integer, db.ForeignKey('post.id'), primary_key=True)
|
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
# Comment contents
|
2019-08-20 17:34:00 +02:00
|
|
|
text = db.Column(db.UnicodeText)
|
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
# Parent thread
|
2019-08-24 19:17:13 +02:00
|
|
|
thread_id = db.Column(db.Integer, db.ForeignKey('thread.id'),
|
2021-07-07 16:30:21 +02:00
|
|
|
nullable=False, index=True)
|
2019-12-07 16:06:00 +01:00
|
|
|
thread = db.relationship('Thread',
|
|
|
|
backref=backref('comments', lazy='dynamic'),
|
2019-08-24 19:17:13 +02:00
|
|
|
foreign_keys=thread_id)
|
2021-02-20 01:36:04 +01:00
|
|
|
|
2022-05-12 21:45:00 +02:00
|
|
|
attachments = db.relationship('Attachment', back_populates='comment',
|
|
|
|
lazy='joined')
|
2021-07-08 16:47:39 +02:00
|
|
|
|
2021-07-14 19:55:16 +02:00
|
|
|
@property
|
|
|
|
def is_top_comment(self):
|
|
|
|
return self.id == self.thread.top_comment_id
|
2020-11-12 00:11:33 +01:00
|
|
|
|
2019-08-20 17:34:00 +02:00
|
|
|
def __init__(self, author, text, thread):
|
2019-09-08 14:34:51 +02:00
|
|
|
"""
|
|
|
|
Create a new Comment in a thread.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
author -- comment poster (User)
|
|
|
|
text -- contents (unicode string)
|
|
|
|
thread -- parent discussion thread (Thread)
|
|
|
|
"""
|
|
|
|
|
|
|
|
Post.__init__(self, author)
|
|
|
|
self.thread = thread
|
2019-12-03 20:32:01 +01:00
|
|
|
self.text = text
|
2019-09-08 14:34:51 +02:00
|
|
|
|
|
|
|
def edit(self, new_text):
|
|
|
|
"""Edit a Comment's contents."""
|
|
|
|
|
|
|
|
self.text = new_text
|
|
|
|
self.touch()
|
|
|
|
|
2020-11-01 10:49:24 +01:00
|
|
|
def delete(self):
|
|
|
|
"""Recursively delete post and all associated contents."""
|
2021-07-07 21:23:35 +02:00
|
|
|
for a in self.attachments:
|
|
|
|
a.delete()
|
|
|
|
db.session.commit()
|
2020-11-01 10:49:24 +01:00
|
|
|
db.session.delete(self)
|
|
|
|
|
2022-05-26 21:08:01 +02:00
|
|
|
def create_attachments(self, multiple_file_field_data):
|
|
|
|
"""Create attachements from a form's MultipleFileField.data."""
|
|
|
|
attachments = []
|
|
|
|
for file in multiple_file_field_data:
|
|
|
|
if file.filename != "":
|
|
|
|
a = Attachment(file, self)
|
|
|
|
attachments.append((a, file))
|
|
|
|
db.session.add(a)
|
|
|
|
db.session.commit()
|
|
|
|
for a, file in attachments:
|
|
|
|
a.set_file(file)
|
|
|
|
|
2019-09-08 14:34:51 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return f'<Comment: #{self.id}>'
|