From 11725a28c496d66db18740a3a02760379df455ef Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Thu, 15 Dec 2022 15:55:38 +0100 Subject: [PATCH] Allow deletion of draft news articles When somebody posts a news article, make it possible to delete it before it's submitted to moderation (or after it's been withdrawn or bounced), instead of forcing the user to leave it around ForEver (TM). Do this by adding some generic functionality for confirmation popups, that can also be used for other things in the future. --- media/js/main.js | 15 +++++++++++++++ pgweb/util/helpers.py | 8 ++++++++ templates/base/form_contents.html | 3 +++ 3 files changed, 26 insertions(+) diff --git a/media/js/main.js b/media/js/main.js index ffc1d413..aec214a1 100644 --- a/media/js/main.js +++ b/media/js/main.js @@ -98,3 +98,18 @@ function showDistros(btn, osDiv) { btn.style.background = active_color; document.getElementById(osDiv).style.display = 'block'; } + + +/* + * Register a confirm handler for forms that, well, requires confirmation + * for someting. + */ +document.querySelectorAll('button[data-confirm]').forEach((button) => { + button.addEventListener('click', (event) => { + if (confirm(event.target.dataset.confirm)) { + return true; + } + event.preventDefault(); + return false; + }); +}); diff --git a/pgweb/util/helpers.py b/pgweb/util/helpers.py index 7c6d7647..3c436eb2 100644 --- a/pgweb/util/helpers.py +++ b/pgweb/util/helpers.py @@ -45,6 +45,11 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for raise PermissionDenied("You cannot edit this item") if request.method == 'POST': + if 'modstate' in (f.name for f in instance._meta.get_fields()) and instance.modstate == ModerationState.CREATED and request.POST.get('delete', '') == 'delete': + # Don't care to validate, just delete. + instance.delete() + return HttpResponseRedirect(redirect) + # Process this form form = formclass(data=request.POST, instance=instance) for fn in form.fields: @@ -183,6 +188,7 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for form_intro = None savebutton = 'Save' + deletebutton = None if itemid == 'new': if 'modstate' in (f.name for f in instance._meta.get_fields()): # This is a three-state moderated entry, so don't say "submit new" for new @@ -194,6 +200,7 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for if 'modstate' in (f.name for f in instance._meta.get_fields()): if instance.modstate == ModerationState.CREATED: savebutton = 'Save draft' + deletebutton = 'Delete draft' ctx = { 'form': form, @@ -201,6 +208,7 @@ def simple_form(instancetype, itemid, request, formclass, formtemplate='base/for 'form_intro': form_intro, 'described_checkboxes': getattr(form, 'described_checkboxes', {}), 'savebutton': savebutton, + 'deletebutton': deletebutton, 'operation': (itemid == "new") and "New" or "Edit", } ctx.update(extracontext) diff --git a/templates/base/form_contents.html b/templates/base/form_contents.html index 69083cf1..3d110c72 100644 --- a/templates/base/form_contents.html +++ b/templates/base/form_contents.html @@ -21,4 +21,7 @@ {%endfor%} {%endif%} +{%if deletebutton%} + +{%endif%} -- 2.39.5