From 2dcb4c7d384576738bc5fc6ec1f8ed70b2b47227 Mon Sep 17 00:00:00 2001
From: Erwan Rouchet <rouchet@teklia.com>
Date: Thu, 30 Mar 2023 18:31:29 +0000
Subject: [PATCH] Fix EntityType creation and deletion in the Django admin

---
 arkindex/documents/admin.py | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/arkindex/documents/admin.py b/arkindex/documents/admin.py
index 2602e9ab28..64f37857aa 100644
--- a/arkindex/documents/admin.py
+++ b/arkindex/documents/admin.py
@@ -157,9 +157,19 @@ class EntityRoleAdmin(admin.ModelAdmin):
 
 
 class EntityTypeAdmin(admin.ModelAdmin):
-    list_display = ('id', 'name', 'color')
+    list_display = ('id', 'corpus', 'name', 'color')
     list_filter = ('corpus', )
-    readonly_fields = ('id', 'corpus', )
+
+    def get_readonly_fields(self, request, obj=None):
+        # Make the corpus field read-only only for existing entity types.
+        # Otherwise, new EntityTypes would be created with corpus=None
+        if obj:
+            return ('id', 'corpus')
+        return ('id', )
+
+    def has_delete_permission(self, request, obj=None):
+        # Require everyone to use the frontend or DestroyEntityType
+        return False
 
 
 admin.site.register(Corpus, CorpusAdmin)
-- 
GitLab