PCv5/app/models/comment.py

51 lines
1.3 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 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__}
# 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)
# Comment contents
2019-08-20 17:34:00 +02:00
text = db.Column(db.UnicodeText)
# Parent thread
thread_id = db.Column(db.Integer, db.ForeignKey('thread.id'),
2021-01-12 16:40:52 +01:00
nullable=False)
thread = db.relationship('Thread',
backref=backref('comments', lazy='dynamic'),
foreign_keys=thread_id)
2019-08-20 17:34:00 +02:00
def __init__(self, author, text, thread):
"""
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
self.text = text
def edit(self, new_text):
"""Edit a Comment's contents."""
self.text = new_text
self.touch()
def delete(self):
"""Recursively delete post and all associated contents."""
# FIXME: Attached files?
db.session.delete(self)
def __repr__(self):
return f'<Comment: #{self.id}>'