From ba0ff806da970d6970b0261474b9809f9c20d765 Mon Sep 17 00:00:00 2001 From: Dylan Hillerbrand Date: Mon, 26 Aug 2024 13:06:20 -0400 Subject: [PATCH 1/2] fix(source list template): adjust source id columns - Add sortable "city + institution" column - Make source column = siglum + shelfmark - Make default order siglum + shelfmark --- .../main_app/templates/source_list.html | 12 +++++++++--- django/cantusdb_project/main_app/views/source.py | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/django/cantusdb_project/main_app/templates/source_list.html b/django/cantusdb_project/main_app/templates/source_list.html index 7988b08bd..a65800af0 100644 --- a/django/cantusdb_project/main_app/templates/source_list.html +++ b/django/cantusdb_project/main_app/templates/source_list.html @@ -88,7 +88,8 @@

Browse Sources

{% sortable_header request "country" %} - {% sortable_header request "heading" "Source" %} + {% sortable_header request "city_institution" "City + Institution" %} + {% sortable_header request "source" "Source" %} Summary Date/Origin Image Link @@ -101,9 +102,14 @@

Browse Sources

{{ source.holding_institution.country }} - + + + {{ source.holding_institution.city }}, {{ source.holding_institution.name }} + + + - {{ source.heading|truncatechars_html:100 }} + {{ source.short_heading|truncatechars_html:100 }} diff --git a/django/cantusdb_project/main_app/views/source.py b/django/cantusdb_project/main_app/views/source.py index a61aca119..9942fd760 100644 --- a/django/cantusdb_project/main_app/views/source.py +++ b/django/cantusdb_project/main_app/views/source.py @@ -348,10 +348,10 @@ def get_queryset(self) -> QuerySet[Source]: q_obj_filter &= indexing_search_q order_param = self.request.GET.get("order") - order_fields = ["siglum"] + order_fields = ["holding_institution__siglum", "shelfmark"] if order_param == "country": order_fields.insert(0, "holding_institution__country") - if order_param == "heading": + elif order_param == "city_institution": order_fields.insert(0, "holding_institution__city") order_fields.insert(1, "holding_institution__name") if self.request.GET.get("sort") == "desc": From 5776ecfa83520052676f656f8c698bd34489a763 Mon Sep 17 00:00:00 2001 From: Dylan Hillerbrand Date: Mon, 26 Aug 2024 13:27:03 -0400 Subject: [PATCH 2/2] test(source list): add tests for source list ordering --- .../main_app/tests/test_views.py | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/django/cantusdb_project/main_app/tests/test_views.py b/django/cantusdb_project/main_app/tests/test_views.py index 30c794673..f7146eef8 100644 --- a/django/cantusdb_project/main_app/tests/test_views.py +++ b/django/cantusdb_project/main_app/tests/test_views.py @@ -4694,6 +4694,79 @@ def test_search_by_indexing_notes(self): response = self.client.get(reverse("source-list"), {"general": search_term}) self.assertIn(source, response.context["sources"]) + def test_ordering(self) -> None: + """ + Order is currently available by country, city + institution name (parameter: + "city_institution"), and siglum + shelfmark. Siglum + shelfmark is the default. + """ + # Create a bunch of sources + sources = [] + for _ in range(10): + sources.append(make_fake_source()) + # Default ordering is by siglum and shelfmark, ascending + with self.subTest("Default ordering"): + response = self.client.get(reverse("source-list")) + response_sources = response.context["sources"] + expected_source_order = sorted( + sources, + key=lambda source: ( + source.holding_institution.siglum, + source.shelfmark, + ), + ) + self.assertEqual( + list(expected_source_order), + list(response_sources), + ) + response_reverse = self.client.get(reverse("source-list"), {"sort": "desc"}) + response_sources_reverse = response_reverse.context["sources"] + self.assertEqual( + list(reversed(expected_source_order)), + list(response_sources_reverse), + ) + with self.subTest("Order by country, ascending"): + response = self.client.get(reverse("source-list"), {"order": "country"}) + response_sources = response.context["sources"] + expected_source_order = sorted( + sources, key=lambda source: source.holding_institution.country + ) + self.assertEqual( + list(expected_source_order), + list(response_sources), + ) + response_reverse = self.client.get( + reverse("source-list"), {"order": "country", "sort": "desc"} + ) + response_sources_reverse = response_reverse.context["sources"] + self.assertEqual( + list(reversed(expected_source_order)), + list(response_sources_reverse), + ) + with self.subTest("Order by city and institution name, ascending"): + response = self.client.get( + reverse("source-list"), {"order": "city_institution"} + ) + response_sources = response.context["sources"] + expected_source_order = sorted( + sources, + key=lambda source: ( + source.holding_institution.city, + source.holding_institution.name, + ), + ) + self.assertEqual( + list(expected_source_order), + list(response_sources), + ) + response_reverse = self.client.get( + reverse("source-list"), {"order": "city_institution", "sort": "desc"} + ) + response_sources_reverse = response_reverse.context["sources"] + self.assertEqual( + list(reversed(expected_source_order)), + list(response_sources_reverse), + ) + class SourceCreateViewTest(TestCase): @classmethod