Skip to content

Commit

Permalink
Merge pull request #1618 from dchiller/i1616-source-list-table
Browse files Browse the repository at this point in the history
Adjust sortable columns for source list page (based on email feedback)
  • Loading branch information
dchiller authored Aug 30, 2024
2 parents 32874b5 + 5776ecf commit 22a048d
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
12 changes: 9 additions & 3 deletions django/cantusdb_project/main_app/templates/source_list.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ <h3>Browse Sources</h3>
<thead>
<tr>
{% sortable_header request "country" %}
{% sortable_header request "heading" "Source" %}
{% sortable_header request "city_institution" "City + Institution" %}
{% sortable_header request "source" "Source" %}
<th scope="col" class="text-wrap" style="text-align:center">Summary</th>
<th scope="col" class="text-wrap" style="text-align:center">Date/Origin</th>
<th scope="col" class="text-wrap" style="text-align:center">Image Link</th>
Expand All @@ -101,9 +102,14 @@ <h3>Browse Sources</h3>
<td class="text-wrap" style="text-align:center">
<b>{{ source.holding_institution.country }}</b>
</td>
<td class="text-wrap" style="text-align:center" title="{{ source.heading }}">
<td class="text-wrap" style="text-align:center">
<a href="{% url 'source-detail' source.id %}">
<b>{{ source.holding_institution.city }}, {{ source.holding_institution.name }}</b>
</a>
</td>
<td class="text-wrap" style="text-align:center">
<a href="{% url 'source-detail' source.id %}">
<b>{{ source.heading|truncatechars_html:100 }}</b>
<b>{{ source.short_heading|truncatechars_html:100 }}</b>
</a>
</td>
<td class="text-wrap" style="text-align:center" title="{{ source.summary|default:""|truncatechars_html:500 }}">
Expand Down
73 changes: 73 additions & 0 deletions django/cantusdb_project/main_app/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4730,6 +4730,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
Expand Down
4 changes: 2 additions & 2 deletions django/cantusdb_project/main_app/views/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 22a048d

Please sign in to comment.