From 9b19ad5c49057c4d8e41281b6428c65b65091e35 Mon Sep 17 00:00:00 2001
From: Jay Varner <jayvarner@gmail.com>
Date: Wed, 24 Apr 2024 16:55:11 -0400
Subject: [PATCH] Fix how starting canvas gets set.

---
 apps/iiif/canvases/models.py            | 11 -----------
 apps/iiif/manifests/models.py           | 19 +++++++------------
 apps/iiif/manifests/tests/test_views.py |  1 +
 apps/iiif/manifests/tests/tests.py      |  1 +
 4 files changed, 9 insertions(+), 23 deletions(-)

diff --git a/apps/iiif/canvases/models.py b/apps/iiif/canvases/models.py
index 59a0330f..1c873b78 100644
--- a/apps/iiif/canvases/models.py
+++ b/apps/iiif/canvases/models.py
@@ -179,20 +179,9 @@ def before_save(self):
                 self.width = self.image_info['width']
                 self.height = self.image_info['height']
 
-        print("$$$$$$$$$$$$$$$$$$")
-        print(self.pid)
-        print("$$$$$$$$$$$$$$$$$$")
-
         if self.resource is None:
             self.resource = self.pid
 
-        print("################")
-        print(self.resource_id)
-        print("################")
-
-        if self.manifest and self.manifest.start_canvas is None:
-            Manifest.objects.filter(id=self.manifest.id).update(start_canvas=self)
-
     def save(self, *args, **kwargs): # pylint: disable = signature-differs
         """
         Override save to call the before_save function.
diff --git a/apps/iiif/manifests/models.py b/apps/iiif/manifests/models.py
index 077a2df2..2123ce8b 100644
--- a/apps/iiif/manifests/models.py
+++ b/apps/iiif/manifests/models.py
@@ -342,25 +342,20 @@ def save(self, *args, **kwargs): # pylint: disable = arguments-differ
         if not self._state.adding and 'pid' in self.get_dirty_fields() and self.image_server and self.image_server.storage_service == 's3':
             self.__rename_s3_objects()
 
-        super().save(*args, **kwargs)
-
-        for collection in self.collections.all():
-            collection.modified_at = self.modified_at
-            collection.save()
-
-        Canvas = apps.get_model('canvases.canvas')
         try:
+            Canvas = apps.get_model('canvases.canvas')
             if self.start_canvas is None and hasattr(self, 'canvas_set') and self.canvas_set.exists():
                 self.start_canvas = self.canvas_set.all().order_by('position').first()
-                self.save()
+                Canvas.objects.filter(manifest=self).update(is_starting_page=False)
+                Canvas.objects.filter(pk=self.start_canvas.id).update(is_starting_page=True)
         except Canvas.DoesNotExist:
             self.start_canvas = None
 
-        # if 'update_fields' not in kwargs or 'search_vector' not in kwargs['update_fields']:
-        #     instance = self._meta.default_manager.with_documents().get(pk=self.pk)
-        #     instance.search_vector = instance.document
-        #     instance.save(update_fields=['search_vector'])
+        super().save(*args, **kwargs)
 
+        for collection in self.collections.all():
+            collection.modified_at = self.modified_at
+            collection.save()
 
     def delete(self, *args, **kwargs):
         """
diff --git a/apps/iiif/manifests/tests/test_views.py b/apps/iiif/manifests/tests/test_views.py
index f6ea8826..1717acb3 100644
--- a/apps/iiif/manifests/tests/test_views.py
+++ b/apps/iiif/manifests/tests/test_views.py
@@ -104,6 +104,7 @@ def test_multiple_starting_canvases(self):
         assert volume.canvas_set.exists() is False
         for index, _ in enumerate(range(4)):
             CanvasFactory.create(manifest=volume, is_starting_page=True, position=index+1)
+        volume.save()
         volume.refresh_from_db()
         manifest = json.loads(
             serialize(
diff --git a/apps/iiif/manifests/tests/tests.py b/apps/iiif/manifests/tests/tests.py
index f43615c6..fa6f4dc0 100644
--- a/apps/iiif/manifests/tests/tests.py
+++ b/apps/iiif/manifests/tests/tests.py
@@ -97,6 +97,7 @@ def test_default_start_canvas(self):
         assert manifest.start_canvas is None
         canvas = CanvasFactory.create(manifest=manifest)
         canvas.save()
+        manifest.save()
         manifest.refresh_from_db()
         assert manifest.start_canvas == canvas