Repository: DDMAL/CantusDB

Issue:
Chant Create page: we could auto-populate the Office selector
In an email forwarded to us by Debra, a CantusDB contributor commented:

> Office/Mass box. When adding chants, the selection made here does not carry over from the previous chant. My preference is that it pre-populate from the previous chant in the source. (But I don't remember whether this used to pre-populate from the previous or not.)




Discussion:
I haven't checked yet whether this is what OldCantus did, so I'm marking this as an "enhancement" for the time being.
Just checked--OldCantus did prepopulate this field, as well as the "Feast", "Folio", and "Sequence" fields 
![image](https://github.com/DDMAL/CantusDB/assets/67451875/14a089c9-e428-4c65-bce7-1bbf4f952cf8)

Why is this labelled as an enhancement again? OldCantus did it; we should too.

I'm going to get to work on this one, there's a good chance it's a simple fix.

Resolving patch:
diff --git a/django/cantusdb_project/main_app/views/chant.py b/django/cantusdb_project/main_app/views/chant.py
--- a/django/cantusdb_project/main_app/views/chant.py
+++ b/django/cantusdb_project/main_app/views/chant.py
@@ -805,6 +805,7 @@ def get_initial(self):
             }
         latest_folio = latest_chant.folio if latest_chant.folio else "001r"
         latest_feast = latest_chant.feast.id if latest_chant.feast else ""
+        latest_office = latest_chant.office.id if latest_chant.office else ""
         latest_seq = (
             latest_chant.c_sequence if latest_chant.c_sequence is not None else 0
         )
@@ -812,6 +813,7 @@ def get_initial(self):
         return {
             "folio": latest_folio,
             "feast": latest_feast,
+            "office": latest_office,
             "c_sequence": latest_seq + 1,
             "image_link": latest_image,
         }


Test patch:
diff --git a/django/cantusdb_project/main_app/tests/test_views.py b/django/cantusdb_project/main_app/tests/test_views.py
--- a/django/cantusdb_project/main_app/tests/test_views.py
+++ b/django/cantusdb_project/main_app/tests/test_views.py
@@ -2909,6 +2909,52 @@ def test_volpiano_signal(self):
         self.assertEqual(chant_2.volpiano, "abacadaeafagahaja")
         self.assertEqual(chant_2.volpiano_intervals, "1-12-23-34-45-56-67-78-8")
 
+    def test_initial_values(self):
+        # create a chant with a known folio, feast, office, c_sequence and image_link
+        source: Source = make_fake_source()
+        folio: str = "001r"
+        sequence: int = 1
+        feast: Feast = make_fake_feast()
+        office: Office = make_fake_office()
+        image_link: str = "https://www.youtube.com/watch?v=9bZkp7q19f0"
+        self.client.post(
+            reverse("chant-create", args=[source.id]),
+            {
+                "manuscript_full_text_std_spelling": "this is a bog standard manuscript textful spelling",
+                "folio": folio,
+                "c_sequence": str(sequence),
+                "feast": feast.id,
+                "office": office.id,
+                "image_link": image_link,
+            },
+        )
+
+        # when we request the Chant Create page, the same folio, feast, office and image_link should
+        # be preselected, and c_sequence should be incremented by 1.
+        response = self.client.get(
+            reverse("chant-create", args=[source.id]),
+        )
+
+        observed_initial_folio: int = response.context["form"].initial["folio"]
+        with self.subTest(subtest="test initial value of folio field"):
+            self.assertEqual(observed_initial_folio, folio)
+
+        observed_initial_feast: int = response.context["form"].initial["feast"]
+        with self.subTest(subtest="test initial value of feast feild"):
+            self.assertEqual(observed_initial_feast, feast.id)
+
+        observed_initial_office: int = response.context["form"].initial["office"]
+        with self.subTest(subtest="test initial value of office field"):
+            self.assertEqual(observed_initial_office, office.id)
+
+        observed_initial_sequence: int = response.context["form"].initial["c_sequence"]
+        with self.subTest(subtest="test initial value of c_sequence field"):
+            self.assertEqual(observed_initial_sequence, sequence + 1)
+
+        observed_initial_image: int = response.context["form"].initial["image_link"]
+        with self.subTest(subtest="test initial value of image_link field"):
+            self.assertEqual(observed_initial_image, image_link)
+
 
 class ChantDeleteViewTest(TestCase):
     @classmethod