From bee912f88c880974b1f38f8f5bc17ff8a5144ea0 Mon Sep 17 00:00:00 2001 From: Lephe Date: Wed, 7 Jul 2021 21:23:35 +0200 Subject: [PATCH] model: add all deletion methods (except Guest/Member) --- app/models/attachment.py | 6 +++++- app/models/comment.py | 4 +++- app/models/notification.py | 3 +++ app/models/poll.py | 7 ++----- app/models/post.py | 2 -- app/models/program.py | 3 +++ app/models/trophy.py | 6 ++++++ app/routes/account/notification.py | 6 +++--- app/routes/admin/trophies.py | 3 +-- app/routes/polls/delete.py | 6 +----- app/routes/posts/edit.py | 7 +------ 11 files changed, 28 insertions(+), 25 deletions(-) diff --git a/app/models/attachment.py b/app/models/attachment.py index c237fab..80945d7 100644 --- a/app/models/attachment.py +++ b/app/models/attachment.py @@ -45,6 +45,10 @@ class Attachment(db.Model): def delete_file(self): try: - os.delete(self.path) + os.remove(self.path) except FileNotFoundError: pass + + def delete(self): + self.delete_file() + db.session.delete(self) diff --git a/app/models/comment.py b/app/models/comment.py index e8e0882..ceb81ee 100644 --- a/app/models/comment.py +++ b/app/models/comment.py @@ -43,7 +43,9 @@ class Comment(Post): def delete(self): """Recursively delete post and all associated contents.""" - # FIXME: Attached files? + for a in self.attachments: + a.delete() + db.session.commit() db.session.delete(self) def __repr__(self): diff --git a/app/models/notification.py b/app/models/notification.py index 7ace4fa..8eccb70 100644 --- a/app/models/notification.py +++ b/app/models/notification.py @@ -22,3 +22,6 @@ class Notification(db.Model): def __repr__(self): return f'' + + def delete(self): + db.session.delete(self) diff --git a/app/models/poll.py b/app/models/poll.py index 9a6d0b2..91c3544 100644 --- a/app/models/poll.py +++ b/app/models/poll.py @@ -57,13 +57,10 @@ class Poll(db.Model): def delete(self): """Deletes a poll and its answers""" - # TODO: move this out of class definition? - for answer in SpecialPrivilege.query.filter_by(poll_id=self.id).all(): - db.session.delete(answer) + for a in self.answers: + db.session.delete(a) db.session.commit() - db.session.delete(self) - db.session.commit() # Common properties and methods @property diff --git a/app/models/post.py b/app/models/post.py index 0f6dc2c..d5d76a5 100644 --- a/app/models/post.py +++ b/app/models/post.py @@ -20,8 +20,6 @@ class Post(db.Model): author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) author = db.relationship('User', backref="posts", foreign_keys=author_id) - # TODO: Post attachments? - __mapper_args__ = { 'polymorphic_identity': __tablename__, 'polymorphic_on': type diff --git a/app/models/program.py b/app/models/program.py index 8ce6095..592e2e1 100644 --- a/app/models/program.py +++ b/app/models/program.py @@ -40,5 +40,8 @@ class Program(Post): p = Program(topic.author, topic.title, topic.thread) topic.promotion = p + def delete(self): + db.session.delete(self) + def __repr__(self): return f'' diff --git a/app/models/trophy.py b/app/models/trophy.py index 74936b0..e626707 100644 --- a/app/models/trophy.py +++ b/app/models/trophy.py @@ -28,6 +28,12 @@ class Trophy(db.Model): self.description = description self.hidden = hidden + def delete(self): + for tm in TrophyMember.query.filter_by(tid=self.id).all(): + db.session.delete(tm) + db.session.commit() + db.session.delete(self) + def __repr__(self): return f'' diff --git a/app/routes/account/notification.py b/app/routes/account/notification.py index c73269e..db7f631 100644 --- a/app/routes/account/notification.py +++ b/app/routes/account/notification.py @@ -27,11 +27,11 @@ def delete_notification(id=None): if notification: # Only current user or admin can delete notifications if notification.owner_id == current_user.id: - db.session.delete(notification) + notification.delete() db.session.commit() return redirect(url_for('list_notifications')) elif 'delete_notification' in current_user.privs: - db.session.delete(notification) + notification.delete() db.session.commit() if request.referrer: return redirect(request.referrer) @@ -41,7 +41,7 @@ def delete_notification(id=None): abort(404) elif id == "all": for n in current_user.notifications: - db.session.delete(n) + n.delete() db.session.commit() return redirect(url_for('list_notifications')) # TODO: add something to allow an admin to delete all notifs for a user diff --git a/app/routes/admin/trophies.py b/app/routes/admin/trophies.py index 0561660..b0ac084 100644 --- a/app/routes/admin/trophies.py +++ b/app/routes/admin/trophies.py @@ -67,8 +67,7 @@ def adm_delete_trophy(trophy_id): del_form = DeleteTrophyForm() if request.method == "POST": if del_form.validate_on_submit(): - # TODO: Remove relationship with users that have the trophy - db.session.delete(trophy) + trophy.delete() db.session.commit() flash('Trophée supprimé', 'ok') return redirect(url_for('adm_trophies')) diff --git a/app/routes/polls/delete.py b/app/routes/polls/delete.py index 1d41af8..d2cf665 100644 --- a/app/routes/polls/delete.py +++ b/app/routes/polls/delete.py @@ -18,11 +18,7 @@ def poll_delete(poll_id): form = DeletePollForm() if form.validate_on_submit(): - for a in poll.answers: - db.session.delete(a) - db.session.commit() - - db.session.delete(poll) + poll.delete() db.session.commit() flash('Le sondage a été supprimé', 'info') diff --git a/app/routes/posts/edit.py b/app/routes/posts/edit.py index cb0f610..aac8f6e 100644 --- a/app/routes/posts/edit.py +++ b/app/routes/posts/edit.py @@ -46,11 +46,6 @@ def delete_post(postid): if current_user.is_anonymous or not current_user.can_delete_post(p): abort(403) - for a in p.attachments: - a.delete_file() - db.session.delete(a) - db.session.commit() - - db.session.delete(p) + p.delete() db.session.commit() return redirect(request.referrer)