aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2022-07-18 20:37:56 +0200
committerDaniel Schadt <kingdread@gmx.de>2022-07-18 20:37:56 +0200
commit7b9e8b1ee61b9211dc2c9dac59e29f4b7a5a7f15 (patch)
treed7b0b4c1cf4a1c63eca524f084bf5ae5d8e2c120
parent22514b18f3f01287b88c235c91bdd8cacf8ac085 (diff)
downloadfietsboek-7b9e8b1ee61b9211dc2c9dac59e29f4b7a5a7f15.tar.gz
fietsboek-7b9e8b1ee61b9211dc2c9dac59e29f4b7a5a7f15.tar.bz2
fietsboek-7b9e8b1ee61b9211dc2c9dac59e29f4b7a5a7f15.zip
remove images when removing a track
-rw-r--r--fietsboek/data.py20
-rw-r--r--fietsboek/views/detail.py2
2 files changed, 22 insertions, 0 deletions
diff --git a/fietsboek/data.py b/fietsboek/data.py
index 992ddf7..c1cd214 100644
--- a/fietsboek/data.py
+++ b/fietsboek/data.py
@@ -7,10 +7,14 @@ import random
import string
import shutil
import uuid
+import logging
from .util import secure_filename
+LOGGER = logging.getLogger(__name__)
+
+
def generate_filename(filename):
"""Generates a safe-to-use filename for uploads.
@@ -63,6 +67,22 @@ class DataManager:
images.append(image.name)
return images
+ def purge(self, track_id):
+ """Purge all data pertaining to the given track.
+
+ This function logs errors but raises no exception, as such it can
+ always be used to clean up after a track.
+
+ :param track_id: The ID of the track.
+ :type track_id: int
+ """
+ def log_error(_, path, exc_info):
+ LOGGER.warning("Failed to remove %s", path, exc_info=exc_info)
+
+ path = self._track_data_dir(track_id)
+ if path.is_dir():
+ shutil.rmtree(path, ignore_errors=False, onerror=log_error)
+
def image_path(self, track_id, image_id):
"""Returns a path to a saved image.
diff --git a/fietsboek/views/detail.py b/fietsboek/views/detail.py
index 3953f47..0c72d64 100644
--- a/fietsboek/views/detail.py
+++ b/fietsboek/views/detail.py
@@ -73,7 +73,9 @@ def delete_track(request):
:rtype: pyramid.response.Response
"""
track = request.context
+ track_id = track.id
request.dbsession.delete(track)
+ request.data_manager.purge(track_id)
request.session.flash(request.localizer.translate(_("flash.track_deleted")))
return HTTPFound(request.route_url('home'))