From ace0c5bb46ee78b0fe6533b5d50a2dd8c04198a4 Mon Sep 17 00:00:00 2001 From: Alejandro Sirgo Rica Date: Tue, 6 Aug 2024 12:33:22 +0200 Subject: views: enable multi-image deletion Allow the deletion of multiple images in image/delete. --- ogcp/templates/actions/delete_image.html | 3 ++- ogcp/views.py | 37 +++++++++++++++++--------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/ogcp/templates/actions/delete_image.html b/ogcp/templates/actions/delete_image.html index 3eeab42..69acc79 100644 --- a/ogcp/templates/actions/delete_image.html +++ b/ogcp/templates/actions/delete_image.html @@ -8,8 +8,9 @@ {{ args }} +{% set img_count = image_ids | length %}

- {{ _('Delete image %(image_name)s', image_name=image_name) }} + {{ _('Delete %(img_count)d image(s)', img_count=img_count) }}

{{ wtf.quick_form(form, diff --git a/ogcp/views.py b/ogcp/views.py index 6d729f3..3725899 100644 --- a/ogcp/views.py +++ b/ogcp/views.py @@ -3582,26 +3582,29 @@ def action_image_delete(): form = GenericForm(request.form) if request.method == 'POST': ids = form.ids.data.split(' ') - if not validate_elements(ids, max_len=1): + if not validate_elements(ids): return redirect(url_for('images')) - id = ids.pop() - payload = {'image': id} + server = get_server_from_ip_port(form.server.data) - r = server.post('/image/delete', payload) - if not r: - return ogserver_down('images') - if r.status_code != requests.codes.ok: - flash(_('OgServer replied with a non ok status code'), category='error') - else: - flash(_('Image deletion request sent successfully'), category='info') + + for id in ids: + payload = {'image': id} + r = server.post('/image/delete', payload) + if not r: + return ogserver_down('images') + if r.status_code != requests.codes.ok: + return ogserver_error('images') + + flash(_('Image deletion request sent successfully'), category='info') return redirect(url_for('images')) else: params = request.args.to_dict() - images = [(name, imgid) for name, imgid in params.items() + image_ids = [imgid for name, imgid in params.items() if name != 'csrf_token' and name != 'image-server'] - if not validate_elements(images, max_len=1): + + if not validate_elements(image_ids): return redirect(url_for('images')) - image_name, image_id = images[0] + server = get_server_from_ip_port(params['image-server']) try: responses = get_images_grouped_by_repos() @@ -3609,12 +3612,12 @@ def action_image_delete(): return ogserver_down('images') except ServerErrorCode: return ogserver_error('images') - form.ids.data = image_id + + form.ids.data = ' '.join(image_ids) form.server.data = params['image-server'] - if not validate_elements(images, max_len=1): - return redirect(url_for('images')) + return render_template('actions/delete_image.html', form=form, - image_name=image_name.split('_', 1)[0], image_id=image_id, + image_ids=image_ids, responses=responses) @app.route('/action/image/config', methods=['GET', 'POST']) -- cgit v1.2.3-18-g5258