From ceee8751e77b9e49af63355cca4e31a74846b1e1 Mon Sep 17 00:00:00 2001
From: George Katsikas <giorgakis.katsikas@gmail.com>
Date: Mon, 21 Oct 2024 11:52:34 +0200
Subject: [PATCH] prevent citation item delete crashes

---
 scipost_django/journals/views.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/scipost_django/journals/views.py b/scipost_django/journals/views.py
index 98184c03e..953ad128b 100644
--- a/scipost_django/journals/views.py
+++ b/scipost_django/journals/views.py
@@ -1201,7 +1201,18 @@ def _hx_citation_list_item_delete(request, doi_label, index: int):
             "You do not have permission to delete a citation in this non-draft Publication"
         )
 
-    publication.metadata["citation_list"].pop(index)
+    if publication.metadata is None:
+        return HTMXResponse("No metadata found", tag="danger")
+    elif "citation_list" not in publication.metadata:
+        return HTMXResponse("No citation list found", tag="danger")
+
+    try:
+        publication.metadata["citation_list"].pop(index)
+    except IndexError:
+        return HTMXResponse(
+            f"Index {index} out of range ({len(publication.metadata['citation_list'])})",
+            tag="danger",
+        )
     publication.save()
 
     return HttpResponse("")
-- 
GitLab