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:
parent
bb6450bda2
commit
cf9cb8a8c6
|
@ -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}>'
|
||||
|
|
|
@ -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}>'
|
||||
|
|
|
@ -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}>'
|
||||
|
|
|
@ -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}>'
|
||||
|
|
Loading…
Reference in New Issue