From 1cf3c566ed63b3ceb9d4fb7ca20d6af18f631f5e Mon Sep 17 00:00:00 2001
From: Robin Tissot <tissotrobin@gmail.com>
Date: Tue, 30 Mar 2021 14:43:47 +0200
Subject: [PATCH] Fixes last edited link and add a delete button in the
 document list.

---
 app/apps/core/models.py                            |  7 +++++++
 app/escriptorium/templates/core/document_list.html | 11 +++++++++--
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/app/apps/core/models.py b/app/apps/core/models.py
index aad2b421..680f26f3 100644
--- a/app/apps/core/models.py
+++ b/app/apps/core/models.py
@@ -244,6 +244,13 @@ class Document(models.Model):
             else:
                 return 'ltr'
 
+    @cached_property
+    def last_edited_part(self):
+        try:
+            return self.parts.order_by('-updated_at')[0]
+        except IndexError:
+            return None
+
     @property
     def training_model(self):
         return self.ocr_models.filter(training=True).first()
diff --git a/app/escriptorium/templates/core/document_list.html b/app/escriptorium/templates/core/document_list.html
index 9def635d..4b881cf8 100644
--- a/app/escriptorium/templates/core/document_list.html
+++ b/app/escriptorium/templates/core/document_list.html
@@ -49,11 +49,18 @@
           <td class="text-right">
               {# <a href="{% url 'document-detail' pk=document.pk %}" class="btn btn-info disabled" title="{% trans 'View' %}"><i class="fas fa-eye"></i></a> #}
               {# Note that doing one query per row is a lot faster than a subquery for some reason #}
-              <a href="{% url 'document-part-edit' pk=document.pk part_pk=document.parts.last.pk %}"
+              {% if document.last_edited_part %}
+              <a href="{% url 'document-part-edit' pk=document.pk part_pk=document.last_edited_part.pk %}"
                  class="btn btn-info"
                  title="{% trans 'Edit last updated Element' %}">
-                 <i class="fas fa-edit"></i>
+                  <i class="fas fa-edit"></i>
               </a>
+              {% endif %}
+
+              <form method="post" class="inline-form" action="{% url 'document-publish' pk=document.pk %}" onsubmit="return confirm('{% trans "Do you really want to delete the document?" %}');">{% csrf_token %}
+                  <input type="hidden" name="workflow_state" value="{{ document.WORKFLOW_STATE_ARCHIVED }}">
+                  <button type="submit" value="" class="nav-item btn btn-danger" title="{% trans 'Delete' %}"><i class="fas fa-trash"></i></button>
+              </form>
           </td>
         </tr>
         {% endfor %}
-- 
GitLab