Allow deletion of draft news articles
authorMagnus Hagander <magnus@hagander.net>
Thu, 15 Dec 2022 14:55:38 +0000 (15:55 +0100)
committerMagnus Hagander <magnus@hagander.net>
Thu, 15 Dec 2022 16:25:42 +0000 (17:25 +0100)
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
pgweb/util/helpers.py
templates/base/form_contents.html

index ffc1d4138617d996c9176174b2a90e3249f82854..aec214a179de9833a06bd0341348720fab284e91 100644 (file)
@@ -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;
+    });
+});
index 7c6d76470e783a8907411e5c9dabf9f586a2c789..3c436eb202d701fd2c2b5e3d283fe816887560e1 100644 (file)
@@ -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)
index 69083cf1c57aea9895035e15d573875412f4eea1..3d110c72ffa2aadd8abfec2a18ff50d4e8426245 100644 (file)
@@ -21,4 +21,7 @@
   {%endfor%}
 {%endif%}
   <button type="submit" class="btn btn-primary">{{savebutton|default:"Save"}}</button>
+{%if deletebutton%}
+  <button type="submit" name="delete" value="delete" class="btn btn-secondary" data-confirm="Are you sure you want to permanently delete this draft?">{{deletebutton}}</button>
+{%endif%}
 </form>