Repository: DDMAL/CantusDB

Issue:
Chant-create page - chant suggestion buttons - genre selector not filled
On the chant create page (see for example http://127.0.0.1:8080/chant-create/699578), there are buttons along the right side of the page that are supposed to autofill many of the fields. There is code to autofill the Genre selector, but the code doesn't work - clicking on any of the buttons has no effect on the value of the Genre selector.

The issue seems to be with the line
```javascript
const genreId = Array.prototype.find.call(document.getElementById('id_genre').children, ({ textContent }) => textContent === "{{ chant.genre }}");
```

I'm having difficulty parsing it (I gather it's supposed to search through the `id_genre`, pulling out the proper genre id if the genre's ID matches the current genre name (i.e. "A", "V", "R", etc.)). It would be possible to store the proper genre ID associated with each Cantus ID in `next_chants` and not worry about calculating `genreId` on the fly, but I'm worried this approach could get a bit messy.


Discussion:
I am so confused by this line of javascript... some refactoring is definitely in order. There should be a more intuitive way to populate the genre selector, just like how we populate the feast selector, which I believe has already been implemented somewhere.

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
@@ -723,6 +723,10 @@ def make_suggested_chant_dict(suggested_chant):
             chant_dict = json.loads(response.text[2:])[0]
             # add number of occurence to the dict, so that we can display it easily
             chant_dict["count"] = sugg_chant_count
+            # figure out the id of the genre of the chant, to easily populate the Genre selector
+            genre_name = chant_dict["genre"]
+            genre_id = Genre.objects.get(name=genre_name).id
+            chant_dict["genre_id"] = genre_id
             return chant_dict
 
         suggested_chants_dicts = [


Test patch:
diff --git a/django/cantusdb_project/main_app/tests/make_fakes.py b/django/cantusdb_project/main_app/tests/make_fakes.py
--- a/django/cantusdb_project/main_app/tests/make_fakes.py
+++ b/django/cantusdb_project/main_app/tests/make_fakes.py
@@ -158,10 +158,12 @@ def make_fake_feast() -> Feast:
     return feast
 
 
-def make_fake_genre() -> Genre:
+def make_fake_genre(name=None) -> Genre:
     """Generates a fake Genre object."""
+    if name is None:
+        name = faker.lexify("???")
     genre = Genre.objects.create(
-        name=faker.lexify("???"),
+        name=name,
         description=make_fake_text(
             max_size=MAX_NUMBER_TEXT_CHARS, min_size=MIN_NUMBER_TEXT_CHARS
         ),
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
@@ -930,6 +930,8 @@ def test_post_error(self):
 
     def test_suggest_one_folio(self):
         fake_source = make_fake_source()
+        # create fake genre to match fake_chant_2
+        fake_R_genre = make_fake_genre(name="R")
         fake_chant_3 = make_fake_chant(
             source=fake_source,
             cantus_id="333333",
@@ -968,6 +970,9 @@ def test_suggest_one_folio(self):
         self.assertEqual(
             "007450", response.context["suggested_chants"][0]["cid"]
         )
+        self.assertEqual(
+            fake_R_genre.id, response.context["suggested_chants"][0]["genre_id"]
+        )
 
     def test_fake_source(self):
         """cannot go to input form with a fake source