Skip to content

Commit

Permalink
feat(source list): add sorting by country and source columns
Browse files Browse the repository at this point in the history
- add functionality to sort by a source's holding institution
country (the "Country" column) and city/name/siglum (the "Source"
column) in the Source List view
- use the sortable_header helper tag for sortable column headers
in the source_list.html template
- fix the value used in the source column to the source's heading
property
  • Loading branch information
dchiller committed Aug 9, 2024
1 parent 09c5749 commit c36febf
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
10 changes: 6 additions & 4 deletions django/cantusdb_project/main_app/templates/source_list.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{% extends "base.html" %}
{% load helper_tags %}


{% block title %}
<title>Browse Sources | Cantus Database</title>
Expand Down Expand Up @@ -76,8 +78,8 @@ <h3>Browse Sources</h3>
<table class="table table-sm small table-bordered table-responsive">
<thead>
<tr>
<th scope="col" class="text-wrap" style="text-align:center">Country</th>
<th scope="col" class="text-wrap" style="text-align:center">Source</th>
{% sortable_header request "country" %}
{% sortable_header request "heading" "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 @@ -90,9 +92,9 @@ <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.title }}">
<td class="text-wrap" style="text-align:center" title="{{ source.heading }}">
<a href="{% url 'source-detail' source.id %}">
<b>{{ source.title|truncatechars_html:100 }}</b>
<b>{{ source.heading|truncatechars_html:100 }}</b>
</a>
</td>
<td class="text-wrap" style="text-align:center" title="{{ source.summary|default:""|truncatechars_html:500 }}">
Expand Down
23 changes: 20 additions & 3 deletions django/cantusdb_project/main_app/views/source.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.mixins import UserPassesTestMixin
Expand Down Expand Up @@ -214,7 +216,7 @@ class SourceListView(ListView):
context_object_name = "sources"
template_name = "source_list.html"

def get_context_data(self, **kwargs):
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
context = super().get_context_data(**kwargs)
context["provenances"] = (
Provenance.objects.all().order_by("name").values("id", "name")
Expand All @@ -224,11 +226,11 @@ def get_context_data(self, **kwargs):
)
return context

def get_queryset(self):
def get_queryset(self) -> QuerySet[Source]:
# use select_related() for foreign keys to reduce DB queries
queryset = Source.objects.select_related(
"segment", "provenance", "holding_institution"
).order_by("siglum")
)

display_unpublished: bool = self.request.user.is_authenticated
if display_unpublished:
Expand Down Expand Up @@ -338,8 +340,23 @@ def get_queryset(self):
)
q_obj_filter &= indexing_search_q

order_param = self.request.GET.get("order")
order_fields = ["siglum"]
if order_param == "country":
order_fields.insert(0, "holding_institution__country")
if order_param == "heading":
order_fields.insert(0, "holding_institution__city")
order_fields.insert(1, "holding_institution__name")
if self.request.GET.get("sort") == "desc":
sort_prefix = "-"
else:
sort_prefix = ""

order_by_args = [f"{sort_prefix}{field}" for field in order_fields]

return (
queryset.filter(q_obj_filter)
.order_by(*order_by_args)
.distinct()
.prefetch_related(
Prefetch("century", queryset=Century.objects.all().order_by("id"))
Expand Down

0 comments on commit c36febf

Please sign in to comment.