From dd3ab4614f40e13e4f36127f8fd9f9ae96671e95 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Tue, 26 Jul 2022 19:11:27 +0200 Subject: de-duplicate image editing code --- fietsboek/views/edit.py | 15 ++++++++++++--- fietsboek/views/upload.py | 18 +++--------------- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/fietsboek/views/edit.py b/fietsboek/views/edit.py index 2c46527..946a862 100644 --- a/fietsboek/views/edit.py +++ b/fietsboek/views/edit.py @@ -83,13 +83,22 @@ def do_edit(request): tags = request.params.getall("tag[]") track.sync_tags(tags) - _edit_images(request) + edit_images(request, request.context) return HTTPFound(request.route_url('details', track_id=track.id)) -def _edit_images(request): - track = request.context +def edit_images(request, track): + """Edit the images from the given request. + + This deletes and adds images and image descriptions as needed, based on the + image[...] and image-description[...] fields. + + :param request: The request: + :type request: pyramid.request.Request + :param track: The track to edit. + :type track: fietsboek.models.track.Track + """ # Delete requested images for image in request.params.getall("delete-image[]"): diff --git a/fietsboek/views/upload.py b/fietsboek/views/upload.py index ea04e3f..ea05c74 100644 --- a/fietsboek/views/upload.py +++ b/fietsboek/views/upload.py @@ -1,7 +1,6 @@ """Upload functionality.""" import datetime import logging -import re from pyramid.httpexceptions import HTTPFound, HTTPBadRequest from pyramid.response import Response @@ -12,6 +11,7 @@ from sqlalchemy import select import gpxpy +from . import edit from .. import models, util from ..models.track import Visibility @@ -171,20 +171,8 @@ def do_finish_upload(request): request.dbsession.add(track.cache) # Don't forget to add the images - for param_name, image in request.params.items(): - match = re.match("image\\[(\\d+)\\]$", param_name) - if not match: - continue - # Sent for the multi input - if image == b"": - continue - - upload_id = match.group(1) - image_name = request.data_manager.add_image(track.id, image.file, image.filename) - image_meta = models.ImageMetadata(track=track, image_name=image_name) - image_meta.description = request.params.get(f"image-description[{upload_id}]", "") - request.dbsession.add(image_meta) - LOGGER.debug("Uploaded image %s %s", track.id, image_name) + LOGGER.debug("Starting to edit images for the upload") + edit.edit_images(request, track) request.session.flash(request.localizer.translate(_("flash.upload_success"))) -- cgit v1.2.3