forum: add true, recursive deletion methods

Each object has a .delete() which removes it and its owned subcontents.
This delete() unfortunately has to commit the database session because
circularly-dependent objects (such as a thread and its top comment)
cannot be deleted together; two commits are needed.
This commit is contained in:
Lephe 2020-11-01 10:49:24 +01:00
parent bb6450bda2
commit cf9cb8a8c6
Signed by untrusted user: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 30 additions and 0 deletions

View File

@ -39,5 +39,10 @@ class Comment(Post):
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}>'

View File

@ -41,5 +41,12 @@ class Forum(db.Model):
# TODO: optimize this with real ORM
return sum(t.thread.comments.count() for t in self.topics)
def delete(self):
"""Recursively delete forum and all associated contents."""
for t in self.topics:
t.delete()
db.session.commit()
db.session.delete(self)
def __repr__(self):
return f'<Forum: {self.name}>'

View File

@ -52,5 +52,18 @@ class Thread(db.Model):
return self.owner_program[0]
return None
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)
def __repr__(self):
return f'<Thread: #{self.id}>'

View File

@ -51,5 +51,10 @@ class Topic(Post):
self.thread = thread
self.forum = forum
def delete(self):
"""Recursively delete topic and all associated contents."""
self.thread.delete()
db.session.delete(self)
def __repr__(self):
return f'<Topic: #{self.id}>'