model: add all deletion methods (except Guest/Member)

This commit is contained in:
Lephe 2021-07-07 21:23:35 +02:00
parent 3c5599adf7
commit bee912f88c
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
11 changed files with 28 additions and 25 deletions

View File

@ -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)

View File

@ -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):

View File

@ -22,3 +22,6 @@ class Notification(db.Model):
def __repr__(self):
return f'<Notification to {self.owner.name}: {self.text} ({self.href})>'
def delete(self):
db.session.delete(self)

View File

@ -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

View File

@ -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

View File

@ -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'<Program: #{self.id} "{self.title}">'

View File

@ -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'<Trophy: {self.name}>'

View File

@ -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

View File

@ -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'))

View File

@ -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')

View File

@ -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)