From d43bfc29f39ddd37ec93f5c2caa865b01f62a6a2 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 21 May 2023 11:53:48 +0200 Subject: sort images by their original file name This is better than the "random" sort we get otherwise, but maybe one day we can allow the user to re-arrange the images and save the sorting in the database. But this is rather low-priority, this way at least the images are shown in the correct order if they are named systematically. --- fietsboek/views/detail.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/fietsboek/views/detail.py b/fietsboek/views/detail.py index f540a11..ec66922 100644 --- a/fietsboek/views/detail.py +++ b/fietsboek/views/detail.py @@ -20,6 +20,17 @@ from ..models.track import TrackWithMetadata LOGGER = logging.getLogger(__name__) +def _sort_key(image_name: str) -> str: + """Returns the "key" by which the image should be sorted. + + :param image_name: Name of the image (on disk). + :return: The value that should be used to sort the image. + """ + if "_" not in image_name: + return image_name + return image_name.split("_", 1)[1] + + @view_config( route_name="details", renderer="fietsboek:templates/details.jinja2", permission="track.view" ) @@ -51,9 +62,13 @@ def details(request): query = select(models.ImageMetadata).filter_by(track=track, image_name=image_name) image_metadata = request.dbsession.execute(query).scalar_one_or_none() if image_metadata: - images.append((img_src, image_metadata.description)) + images.append((_sort_key(image_name), img_src, image_metadata.description)) else: - images.append((img_src, "")) + images.append((_sort_key(image_name), img_src, "")) + + images.sort(key=lambda element: element[0]) + # Strip off the sort key again + images = [(image[1], image[2]) for image in images] with_meta = TrackWithMetadata(track, request.data_manager) return { -- cgit v1.2.3