From 92d84aec5bed1299cca12f687c037ec90fe38b37 Mon Sep 17 00:00:00 2001
From: mlbonhomme <bonhomme@teklia.com>
Date: Tue, 30 Aug 2022 12:48:17 +0000
Subject: [PATCH] Remove corpus.thumbnail

---
 arkindex/documents/admin.py                   |   1 -
 arkindex/documents/api/elements.py            |   1 -
 arkindex/documents/fixtures/data.json         |   3 +-
 .../0059_remove_corpus_thumbnail.py           |  17 ++
 arkindex/documents/models.py                  |   9 -
 arkindex/documents/serializers/elements.py    |  17 +-
 arkindex/documents/tests/test_corpus.py       | 154 ------------------
 arkindex/sql_validation/corpus_delete.sql     |   3 +-
 .../corpus_delete_top_level_type.sql          |   3 +-
 .../sql_validation/corpus_rights_filter.sql   |   1 -
 .../corpus_rights_filter_public.sql           |   2 -
 arkindex/sql_validation/element_links.sql     |   3 +-
 .../element_links_not_found.sql               |   3 +-
 arkindex/sql_validation/list_elements.sql     |   3 +-
 .../process_elements_filter_type.sql          |   3 +-
 .../process_elements_top_level.sql            |   3 +-
 .../process_elements_with_image.sql           |   3 +-
 17 files changed, 27 insertions(+), 202 deletions(-)
 create mode 100644 arkindex/documents/migrations/0059_remove_corpus_thumbnail.py

diff --git a/arkindex/documents/admin.py b/arkindex/documents/admin.py
index 9b4c5526e7..9d41d26f50 100644
--- a/arkindex/documents/admin.py
+++ b/arkindex/documents/admin.py
@@ -31,7 +31,6 @@ class CorpusExportInline(admin.TabularInline):
 
 class CorpusAdmin(admin.ModelAdmin):
     list_display = ('id', 'name', 'public', 'top_level_type', 'created')
-    raw_id_fields = ('thumbnail', )
     search_fields = ('name', )
     inlines = (ElementTypeInline, UserMembershipInline, GroupMembershipInline, CorpusExportInline)
     ordering = ('-created', )
diff --git a/arkindex/documents/api/elements.py b/arkindex/documents/api/elements.py
index 9c11403cf2..8e49058427 100644
--- a/arkindex/documents/api/elements.py
+++ b/arkindex/documents/api/elements.py
@@ -1369,7 +1369,6 @@ class CorpusList(ListCreateAPIView):
         corpora = Corpus.objects \
             .filter(id__in=corpora_level) \
             .annotate(authorized_users=Count('memberships')) \
-            .select_related('thumbnail__image__server') \
             .prefetch_related('types') \
             .order_by('name', 'id')
 
diff --git a/arkindex/documents/fixtures/data.json b/arkindex/documents/fixtures/data.json
index 0d06e59c37..7e68b92170 100644
--- a/arkindex/documents/fixtures/data.json
+++ b/arkindex/documents/fixtures/data.json
@@ -248,8 +248,7 @@
         "description": "",
         "top_level_type": null,
         "public": true,
-        "indexable": false,
-        "thumbnail": null
+        "indexable": false
     }
 },
 {
diff --git a/arkindex/documents/migrations/0059_remove_corpus_thumbnail.py b/arkindex/documents/migrations/0059_remove_corpus_thumbnail.py
new file mode 100644
index 0000000000..ed17dae056
--- /dev/null
+++ b/arkindex/documents/migrations/0059_remove_corpus_thumbnail.py
@@ -0,0 +1,17 @@
+# Generated by Django 4.0.4 on 2022-08-30 10:16
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('documents', '0058_remove_corpus_repository'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='corpus',
+            name='thumbnail',
+        ),
+    ]
diff --git a/arkindex/documents/models.py b/arkindex/documents/models.py
index dfe1c15d41..66b5aeb502 100644
--- a/arkindex/documents/models.py
+++ b/arkindex/documents/models.py
@@ -49,15 +49,6 @@ class Corpus(IndexableModel):
     # Is this corpus indexable ?
     indexable = models.BooleanField(default=False)
 
-    thumbnail = models.OneToOneField(
-        'documents.Element',
-        on_delete=models.SET_NULL,
-        related_name='thumbnail_corpus',
-        help_text='Optional element used as the thumbnail for this corpus',
-        null=True,
-        blank=True
-    )
-
     # Specific manager for ACL
     objects = CorpusManager()
 
diff --git a/arkindex/documents/serializers/elements.py b/arkindex/documents/serializers/elements.py
index 696ca5773f..36a1ec4dfa 100644
--- a/arkindex/documents/serializers/elements.py
+++ b/arkindex/documents/serializers/elements.py
@@ -276,13 +276,6 @@ class CorpusSerializer(serializers.ModelSerializer):
         read_only=True,
         help_text='Number of users or groups that have been granted access rights on this corpus.',
     )
-    thumbnail = serializers.PrimaryKeyRelatedField(
-        queryset=Element.objects.none(),
-        allow_null=True,
-        default=None,
-        style={'base_template': 'input.html'},
-    )
-    thumbnail_url = serializers.SerializerMethodField(read_only=True)
 
     class Meta:
         model = Corpus
@@ -297,8 +290,6 @@ class CorpusSerializer(serializers.ModelSerializer):
             'created',
             'authorized_users',
             'indexable',
-            'thumbnail',
-            'thumbnail_url',
         )
         extra_kwargs = {
             'public': {
@@ -313,8 +304,7 @@ class CorpusSerializer(serializers.ModelSerializer):
     def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)
         if isinstance(self.instance, Corpus):
-            # There is an existing corpus; allow its elements as thumbnails and its types as top level types
-            self.fields['thumbnail'].queryset = self.instance.elements.exclude(image_id=None)
+            # There is an existing corpus; allow its types as top level types
             self.fields['top_level_type'].queryset = self.instance.types.all()
 
     @extend_schema_field(serializers.ListField(child=serializers.ChoiceField(['read', 'write', 'admin'])))
@@ -339,11 +329,6 @@ class CorpusSerializer(serializers.ModelSerializer):
             count = corpus.memberships.count()
         return count
 
-    @extend_schema_field(serializers.URLField(allow_null=True))
-    def get_thumbnail_url(self, corpus):
-        if corpus.thumbnail:
-            return corpus.thumbnail.iiif_url
-
     def validate_public(self, public):
         """
         Only an admin can toggle a corpus public property
diff --git a/arkindex/documents/tests/test_corpus.py b/arkindex/documents/tests/test_corpus.py
index a9c3c0bce6..742c6f23c9 100644
--- a/arkindex/documents/tests/test_corpus.py
+++ b/arkindex/documents/tests/test_corpus.py
@@ -7,7 +7,6 @@ from django.urls import reverse
 from rest_framework import status
 
 from arkindex.documents.models import Corpus, Element
-from arkindex.images.models import Image
 from arkindex.project.default_corpus import DEFAULT_CORPUS_TYPES
 from arkindex.project.tests import FixtureAPITestCase
 from arkindex.users.models import Role, User
@@ -94,8 +93,6 @@ class TestCorpus(FixtureAPITestCase):
                     'description': '',
                     'created': DB_CREATED,
                     'authorized_users': 1,
-                    'thumbnail': None,
-                    'thumbnail_url': None,
                     'top_level_type': None,
                 }
             ]
@@ -128,8 +125,6 @@ class TestCorpus(FixtureAPITestCase):
                     'description': '',
                     'created': DB_CREATED,
                     'authorized_users': 2,
-                    'thumbnail': None,
-                    'thumbnail_url': None,
                     'top_level_type': None,
                 },
                 {
@@ -141,8 +136,6 @@ class TestCorpus(FixtureAPITestCase):
                     'description': '',
                     'created': DB_CREATED,
                     'authorized_users': 1,
-                    'thumbnail': None,
-                    'thumbnail_url': None,
                     'top_level_type': None,
                 }
             ]
@@ -176,8 +169,6 @@ class TestCorpus(FixtureAPITestCase):
                     'description': '',
                     'created': DB_CREATED,
                     'authorized_users': 2,
-                    'thumbnail': None,
-                    'thumbnail_url': None,
                     'top_level_type': None,
                 },
                 {
@@ -189,8 +180,6 @@ class TestCorpus(FixtureAPITestCase):
                     'description': '',
                     'created': DB_CREATED,
                     'authorized_users': 0,
-                    'thumbnail': None,
-                    'thumbnail_url': None,
                     'top_level_type': None,
                 },
                 {
@@ -202,8 +191,6 @@ class TestCorpus(FixtureAPITestCase):
                     'description': '',
                     'created': DB_CREATED,
                     'authorized_users': 1,
-                    'thumbnail': None,
-                    'thumbnail_url': None,
                     'top_level_type': None,
                 }
             ]
@@ -333,8 +320,6 @@ class TestCorpus(FixtureAPITestCase):
             'rights': ['read'],
             'created': DB_CREATED,
             'authorized_users': 1,
-            'thumbnail': None,
-            'thumbnail_url': None,
             'top_level_type': None,
         })
 
@@ -353,8 +338,6 @@ class TestCorpus(FixtureAPITestCase):
             'types': [],
             'created': DB_CREATED,
             'authorized_users': 2,
-            'thumbnail': None,
-            'thumbnail_url': None,
             'top_level_type': None,
         })
 
@@ -388,8 +371,6 @@ class TestCorpus(FixtureAPITestCase):
             'rights': ['read', 'write', 'admin'],
             'created': DB_CREATED,
             'authorized_users': 1,
-            'thumbnail': None,
-            'thumbnail_url': None,
             'top_level_type': None,
         })
 
@@ -417,8 +398,6 @@ class TestCorpus(FixtureAPITestCase):
             'types': [],
             'created': DB_CREATED,
             'authorized_users': 2,
-            'thumbnail': None,
-            'thumbnail_url': None,
             'top_level_type': None,
         })
 
@@ -439,8 +418,6 @@ class TestCorpus(FixtureAPITestCase):
             'types': [],
             'created': DB_CREATED,
             'authorized_users': 2,
-            'thumbnail': None,
-            'thumbnail_url': None,
             'top_level_type': None,
         })
 
@@ -455,69 +432,6 @@ class TestCorpus(FixtureAPITestCase):
         self.assertEqual(self.corpus_private.name, 'new name')
         self.assertEqual(self.corpus_private.description, 'new description')
 
-    def test_partial_update_thumbnail(self):
-        self.client.force_login(self.corpus_admin)
-        element = self.corpus_private.elements.create(
-            type=self.corpus_private.types.create(
-                slug='my_type',
-                display_name='My type',
-            ),
-            image=Image.objects.first(),
-            polygon=((0, 0), (0, 10), (10, 10), (10, 0), (0, 0)),
-            name='An element',
-        )
-        response = self.client.patch(reverse('api:corpus-retrieve', kwargs={'pk': self.corpus_private.id}), {
-            'thumbnail': str(element.id),
-        })
-        self.assertEqual(response.status_code, status.HTTP_200_OK)
-        self.corpus_private.refresh_from_db()
-        self.assertEqual(self.corpus_private.thumbnail, element)
-
-    def test_partial_update_thumbnail_none(self):
-        self.client.force_login(self.corpus_admin)
-        element = self.corpus_private.elements.create(
-            type=self.corpus_private.types.create(
-                slug='my_type',
-                display_name='My type',
-            ),
-            image=Image.objects.first(),
-            polygon=((0, 0), (0, 10), (10, 10), (10, 0), (0, 0)),
-            name='An element',
-        )
-        self.corpus_private.thumbnail = element
-        self.corpus_private.save()
-        response = self.client.patch(reverse('api:corpus-retrieve', kwargs={'pk': self.corpus_private.id}), {
-            'thumbnail': None
-        }, format='json')
-        self.assertEqual(response.status_code, status.HTTP_200_OK)
-        self.corpus_private.refresh_from_db()
-        self.assertIsNone(self.corpus_private.thumbnail)
-
-    def test_partial_update_thumbnail_no_zone(self):
-        self.client.force_login(self.corpus_admin)
-        folder = self.corpus_private.elements.create(
-            type=self.corpus_private.types.create(slug='folder', folder=True),
-            name='A folder',
-        )
-        response = self.client.patch(reverse('api:corpus-retrieve', kwargs={'pk': self.corpus_private.id}), {
-            'thumbnail': str(folder.id)
-        })
-        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
-        self.assertDictEqual(response.json(), {
-            'thumbnail': [f'Invalid pk "{folder.id}" - object does not exist.']
-        })
-
-    def test_partial_update_thumbnail_wrong_corpus(self):
-        self.client.force_login(self.corpus_admin)
-        element = self.corpus_public.elements.filter(polygon__isnull=False).first()
-        response = self.client.patch(reverse('api:corpus-retrieve', kwargs={'pk': self.corpus_private.id}), {
-            'thumbnail': str(element.id)
-        })
-        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
-        self.assertDictEqual(response.json(), {
-            'thumbnail': [f'Invalid pk "{element.id}" - object does not exist.']
-        })
-
     def test_partial_update_private_to_public_normal_user(self):
         """
         A normal user should not be able to make a private corpus public
@@ -625,74 +539,6 @@ class TestCorpus(FixtureAPITestCase):
             'name': ['This field is required.'],
         })
 
-    def test_update_thumbnail(self):
-        self.client.force_login(self.corpus_admin)
-        element = self.corpus_private.elements.create(
-            type=self.corpus_private.types.create(
-                slug='my_type',
-                display_name='My type',
-            ),
-            image=Image.objects.first(),
-            polygon=((0, 0), (0, 10), (10, 10), (10, 0), (0, 0)),
-            name='An element',
-        )
-        response = self.client.put(reverse('api:corpus-retrieve', kwargs={'pk': self.corpus_private.id}), {
-            'name': 'new name',
-            'thumbnail': str(element.id),
-        })
-        self.assertEqual(response.status_code, status.HTTP_200_OK)
-        self.corpus_private.refresh_from_db()
-        self.assertEqual(self.corpus_private.name, 'new name')
-        self.assertEqual(self.corpus_private.thumbnail, element)
-
-    def test_update_thumbnail_none(self):
-        self.client.force_login(self.corpus_admin)
-        element = self.corpus_private.elements.create(
-            type=self.corpus_private.types.create(
-                slug='my_type',
-                display_name='My type',
-            ),
-            image=Image.objects.first(),
-            polygon=((0, 0), (0, 10), (10, 10), (10, 0), (0, 0)),
-            name='An element',
-        )
-        self.corpus_private.thumbnail = element
-        self.corpus_private.save()
-        response = self.client.put(reverse('api:corpus-retrieve', kwargs={'pk': self.corpus_private.id}), {
-            'name': 'new name',
-            'thumbnail': None
-        }, format='json')
-        self.assertEqual(response.status_code, status.HTTP_200_OK)
-        self.corpus_private.refresh_from_db()
-        self.assertIsNone(self.corpus_private.thumbnail)
-
-    def test_update_thumbnail_no_zone(self):
-        self.client.force_login(self.corpus_admin)
-        folder = self.corpus_private.elements.create(
-            type=self.corpus_private.types.create(slug='folder', folder=True),
-            name='A folder',
-        )
-        response = self.client.put(reverse('api:corpus-retrieve', kwargs={'pk': self.corpus_private.id}), {
-            'name': 'new name',
-            'thumbnail': str(folder.id)
-        })
-        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
-        self.assertDictEqual(response.json(), {
-            'thumbnail': [f'Invalid pk "{folder.id}" - object does not exist.']
-        })
-
-    def test_update_thumbnail_wrong_corpus(self):
-        self.client.force_login(self.corpus_admin)
-        element = self.corpus_public.elements.filter(polygon__isnull=False).first()
-        response = self.client.put(reverse('api:corpus-retrieve', kwargs={'pk': self.corpus_private.id}), {
-            'name': 'new name',
-            'thumbnail': str(element.id)
-        })
-        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
-        self.assertDictEqual(response.json(), {
-            'thumbnail': [f'Invalid pk "{element.id}" - object does not exist.']
-        })
-
     def test_update_private_to_public_normal_user(self):
         """
         A normal user should not be able to make a private corpus public
diff --git a/arkindex/sql_validation/corpus_delete.sql b/arkindex/sql_validation/corpus_delete.sql
index 00542b4d0a..42b188b4d1 100644
--- a/arkindex/sql_validation/corpus_delete.sql
+++ b/arkindex/sql_validation/corpus_delete.sql
@@ -5,8 +5,7 @@ SELECT "documents_corpus"."created",
        "documents_corpus"."description",
        "documents_corpus"."top_level_type_id",
        "documents_corpus"."public",
-       "documents_corpus"."indexable",
-       "documents_corpus"."thumbnail_id"
+       "documents_corpus"."indexable"
 FROM "documents_corpus"
 WHERE "documents_corpus"."id" = '{corpus_id}'::uuid
 LIMIT 21;
diff --git a/arkindex/sql_validation/corpus_delete_top_level_type.sql b/arkindex/sql_validation/corpus_delete_top_level_type.sql
index b1038ff21a..c02e8ef3c3 100644
--- a/arkindex/sql_validation/corpus_delete_top_level_type.sql
+++ b/arkindex/sql_validation/corpus_delete_top_level_type.sql
@@ -5,8 +5,7 @@ SELECT "documents_corpus"."created",
        "documents_corpus"."description",
        "documents_corpus"."top_level_type_id",
        "documents_corpus"."public",
-       "documents_corpus"."indexable",
-       "documents_corpus"."thumbnail_id"
+       "documents_corpus"."indexable"
 FROM "documents_corpus"
 WHERE "documents_corpus"."id" = '{corpus_id}'::uuid
 LIMIT 21;
diff --git a/arkindex/sql_validation/corpus_rights_filter.sql b/arkindex/sql_validation/corpus_rights_filter.sql
index 25a1bda56c..9122e1515e 100644
--- a/arkindex/sql_validation/corpus_rights_filter.sql
+++ b/arkindex/sql_validation/corpus_rights_filter.sql
@@ -22,7 +22,6 @@ SELECT "documents_corpus"."created",
        "documents_corpus"."top_level_type_id",
        "documents_corpus"."public",
        "documents_corpus"."indexable",
-       "documents_corpus"."thumbnail_id",
        LEAST("users_right"."level", T5."level") AS "max_level"
 FROM "documents_corpus"
 INNER JOIN "users_right" ON ("documents_corpus"."id" = "users_right"."content_id"
diff --git a/arkindex/sql_validation/corpus_rights_filter_public.sql b/arkindex/sql_validation/corpus_rights_filter_public.sql
index 2eeb47798d..03bc0851e0 100644
--- a/arkindex/sql_validation/corpus_rights_filter_public.sql
+++ b/arkindex/sql_validation/corpus_rights_filter_public.sql
@@ -23,7 +23,6 @@ LIMIT 21;
             "documents_corpus"."top_level_type_id",
             "documents_corpus"."public",
             "documents_corpus"."indexable",
-            "documents_corpus"."thumbnail_id",
             LEAST("users_right"."level", T5."level") AS "max_level"
      FROM "documents_corpus"
      INNER JOIN "users_right" ON ("documents_corpus"."id" = "users_right"."content_id"
@@ -43,7 +42,6 @@ UNION
             "documents_corpus"."top_level_type_id",
             "documents_corpus"."public",
             "documents_corpus"."indexable",
-            "documents_corpus"."thumbnail_id",
             10 AS "max_level"
      FROM "documents_corpus"
      WHERE "documents_corpus"."public")
diff --git a/arkindex/sql_validation/element_links.sql b/arkindex/sql_validation/element_links.sql
index d54f4c2bbf..f2fee404b2 100644
--- a/arkindex/sql_validation/element_links.sql
+++ b/arkindex/sql_validation/element_links.sql
@@ -7,8 +7,7 @@ SELECT "documents_element"."id",
        "documents_corpus"."description",
        "documents_corpus"."top_level_type_id",
        "documents_corpus"."public",
-       "documents_corpus"."indexable",
-       "documents_corpus"."thumbnail_id"
+       "documents_corpus"."indexable"
 FROM "documents_element"
 INNER JOIN "documents_corpus" ON ("documents_element"."corpus_id" = "documents_corpus"."id")
 WHERE "documents_element"."id" = '{element_id}'::uuid
diff --git a/arkindex/sql_validation/element_links_not_found.sql b/arkindex/sql_validation/element_links_not_found.sql
index 3ab9b4885f..81df4cee72 100644
--- a/arkindex/sql_validation/element_links_not_found.sql
+++ b/arkindex/sql_validation/element_links_not_found.sql
@@ -7,8 +7,7 @@ SELECT "documents_element"."id",
        "documents_corpus"."description",
        "documents_corpus"."top_level_type_id",
        "documents_corpus"."public",
-       "documents_corpus"."indexable",
-       "documents_corpus"."thumbnail_id"
+       "documents_corpus"."indexable"
 FROM "documents_element"
 INNER JOIN "documents_corpus" ON ("documents_element"."corpus_id" = "documents_corpus"."id")
 WHERE "documents_element"."id" = '{element_id}'::uuid
diff --git a/arkindex/sql_validation/list_elements.sql b/arkindex/sql_validation/list_elements.sql
index 579b3c735b..9d23eaa4b2 100644
--- a/arkindex/sql_validation/list_elements.sql
+++ b/arkindex/sql_validation/list_elements.sql
@@ -5,8 +5,7 @@ SELECT "documents_corpus"."created",
        "documents_corpus"."description",
        "documents_corpus"."top_level_type_id",
        "documents_corpus"."public",
-       "documents_corpus"."indexable",
-       "documents_corpus"."thumbnail_id"
+       "documents_corpus"."indexable"
 FROM "documents_corpus"
 WHERE "documents_corpus"."id" = '{corpus_id}'::uuid
 LIMIT 21;
diff --git a/arkindex/sql_validation/process_elements_filter_type.sql b/arkindex/sql_validation/process_elements_filter_type.sql
index 34b1efa60b..bbe7357519 100644
--- a/arkindex/sql_validation/process_elements_filter_type.sql
+++ b/arkindex/sql_validation/process_elements_filter_type.sql
@@ -51,8 +51,7 @@ SELECT "documents_corpus"."created",
        "documents_corpus"."description",
        "documents_corpus"."top_level_type_id",
        "documents_corpus"."public",
-       "documents_corpus"."indexable",
-       "documents_corpus"."thumbnail_id"
+       "documents_corpus"."indexable"
 FROM "documents_corpus"
 WHERE "documents_corpus"."id" = '{corpus_id}'::uuid
 LIMIT 21;
diff --git a/arkindex/sql_validation/process_elements_top_level.sql b/arkindex/sql_validation/process_elements_top_level.sql
index 378858eb7d..3eb749289f 100644
--- a/arkindex/sql_validation/process_elements_top_level.sql
+++ b/arkindex/sql_validation/process_elements_top_level.sql
@@ -51,8 +51,7 @@ SELECT "documents_corpus"."created",
        "documents_corpus"."description",
        "documents_corpus"."top_level_type_id",
        "documents_corpus"."public",
-       "documents_corpus"."indexable",
-       "documents_corpus"."thumbnail_id"
+       "documents_corpus"."indexable"
 FROM "documents_corpus"
 WHERE "documents_corpus"."id" = '{corpus_id}'::uuid
 LIMIT 21;
diff --git a/arkindex/sql_validation/process_elements_with_image.sql b/arkindex/sql_validation/process_elements_with_image.sql
index da5c5a83c5..364a9a26b3 100644
--- a/arkindex/sql_validation/process_elements_with_image.sql
+++ b/arkindex/sql_validation/process_elements_with_image.sql
@@ -51,8 +51,7 @@ SELECT "documents_corpus"."created",
        "documents_corpus"."description",
        "documents_corpus"."top_level_type_id",
        "documents_corpus"."public",
-       "documents_corpus"."indexable",
-       "documents_corpus"."thumbnail_id"
+       "documents_corpus"."indexable"
 FROM "documents_corpus"
 WHERE "documents_corpus"."id" = '{corpus_id}'::uuid
 LIMIT 21;
-- 
GitLab