Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New: Add views for institutions #1586

Merged
merged 2 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions django/cantusdb_project/main_app/identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class ExternalIdentifiers:
GND = 4
BNF = 5
LC = 6
DIAMM = 7


IDENTIFIER_TYPES = (
Expand All @@ -14,6 +15,7 @@ class ExternalIdentifiers:
(ExternalIdentifiers.GND, "GND (Gemeinsame Normdatei)"),
(ExternalIdentifiers.BNF, "Bibliothèque national de France"),
(ExternalIdentifiers.LC, "Library of Congress"),
(ExternalIdentifiers.DIAMM, "Digital Image Archive of Medieval Music"),
)

TYPE_PREFIX = {
Expand All @@ -23,4 +25,5 @@ class ExternalIdentifiers:
ExternalIdentifiers.GND: ("dnb", "https://d-nb.info/gnd/"),
ExternalIdentifiers.BNF: ("bnf", "https://catalogue.bnf.fr/ark:/12148/cb"),
ExternalIdentifiers.LC: ("lc", "https://id.loc.gov/authorities/"),
ExternalIdentifiers.DIAMM: ("diamm", "https://www.diamm.ac.uk/"),
}
1 change: 1 addition & 0 deletions django/cantusdb_project/main_app/models/institution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class Institution(BaseModel):
class Meta:
ordering = ["country", "city", "name"]
constraints = [
CheckConstraint(
check=~(Q(is_private_collector=True) & Q(siglum__isnull=False)),
Expand Down
71 changes: 71 additions & 0 deletions django/cantusdb_project/main_app/templates/institution_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{% extends "base.html" %}

{% block title %}

{% endblock %}

{% block content %}
<div class="mr-3 p-3 col-md-12 mx-auto bg-white rounded">
<object align="right" class="search-bar">
{% include "global_search_bar.html" %}
</object>
<h3>{{ institution.name }} {% if institution.siglum %}({{ institution.siglum }}){% endif %}</h3>
<h4>{{ institution.city }}, {{ institution.country }}</h4>
{% if institution_authorities %}
<hr />
<div class="row">
<div class="col">
{% for authority in institution_authorities %}
View this institution in <a href="{{ authority.1 }}">{{ authority.0 }}</a>
{% endfor %}
</div>
</div>
{% endif %}
<hr />
<div class="row">
{% if num_cantus_sources > 0 %}
<div class="col">
<h5>Cantus Database</h5>
<table class="table table-bordered table-sm small">
<thead>
<tr>
<th>Shelfmark</th>
</tr>
</thead>
<tbody>
{% for source in cantus_sources %}
<tr>
<td>
<a href="{% url "source-detail" source.id %}"><b>{{ source.shelfmark }}</b></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}

{% if num_bower_sources > 0 %}
<div class="col">
<h5>Bower Sequence Database</h5>
<table class="table table-bordered table-sm small">
<thead>
<tr>
<th>Shelfmark</th>
</tr>
</thead>
<tbody>
{% for source in bower_sources %}
<tr>
<td>
<a href="{% url "source-detail" source.id %}"><b>{{ source.shelfmark }}</b></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% endif %}
</div>
</div>
{% endblock %}
44 changes: 44 additions & 0 deletions django/cantusdb_project/main_app/templates/institution_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{% extends "base.html" %}

{% block title %}

{% endblock %}

{% block content %}
<div class="mr-3 p-3 col-md-12 mx-auto bg-white rounded">
<object align="right" class="search-bar">
{% include "global_search_bar.html" %}
</object>
<h3>Institutions</h3>
<small>Displaying {{ page_obj.start_index }}-{{ page_obj.end_index }} of {{ page_obj.paginator.count }}</small>
<table class="table table-bordered table-sm small">
<thead>
<tr>
<th scope="col" class="text-wrap" title="Country">Country</th>
<th scope="col" class="text-wrap" title="City">City</th>
<th scope="col" class="text-wrap" title="Name">Name</th>
<th scope="col" class="text-wrap" title="Sources">Sources</th>
</tr>
</thead>
<tbody>
{% for institution in institutions %}
<tr>
<td class="text-wrap">
{{ institution.country }}
</td>
<td>
{{ institution.city }}
</td>
<td class="text-wrap">
<a href="{% url "institution-detail" institution.id %}">
<b>{{ institution.name }} {% if institution.siglum %}({{ institution.siglum }}){% endif %}</b>
</a>
</td>
<td>{{ institution.num_sources }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% include "pagination.html" %}
</div>
{% endblock %}
4 changes: 4 additions & 0 deletions django/cantusdb_project/main_app/templates/source_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ <h3>{{ source.heading }}</h3>
{% if source.holding_institution %}
<dt>Siglum</dt>
<dd>{{ source.short_heading }}</dd>
<dt>Holding Institution</dt>
<dd>
<a href="{% url "institution-detail" source.holding_institution_id %}">{{ source.holding_institution }}</a>
</dd>
{% endif %}

{% if source.summary %}
Expand Down
12 changes: 12 additions & 0 deletions django/cantusdb_project/main_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
articles_list_export,
flatpages_list_export,
)
from main_app.views.institution import InstitutionListView, InstitutionDetailView
from main_app.views.redirect import (
redirect_chants,
redirect_genre,
Expand Down Expand Up @@ -256,6 +257,17 @@
IndexerListView.as_view(),
name="indexer-list",
),
# institution
path(
"institutions/",
InstitutionListView.as_view(),
name="institution-list",
),
path(
"institution/<int:pk>",
InstitutionDetailView.as_view(),
name="institution-detail",
),
# notation
path(
"notation/<int:pk>",
Expand Down
77 changes: 77 additions & 0 deletions django/cantusdb_project/main_app/views/institution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from django.db.models import Count, Subquery, OuterRef, Aggregate, F, Q, Func
from django.views.generic import DetailView, ListView

from main_app.identifiers import IDENTIFIER_TYPES, TYPE_PREFIX
from main_app.models import Institution, Source, Segment, InstitutionIdentifier


class InstitutionListView(ListView):
model = Institution
context_object_name = "institutions"
paginate_by = 100
template_name = "institution_list.html"

def get_queryset(self):
display_unpublished: bool = self.request.user.is_authenticated

# uses a subquery to get a count of the sources, filtering by published
# sources only it the user is not logged in.
qargs = {"holding_institution": OuterRef("pk")}
if display_unpublished is False:
qargs["published"] = True

sources = (
Source.objects.filter(**qargs)
.annotate(c=Func(F("id"), function="COUNT"))
.values("c")
)

# Only display institution records if they have sources in them that the user
# can access.
qset = Institution.objects.annotate(num_sources=Subquery(sources)).filter(
num_sources__gt=0
)
return qset


class InstitutionDetailView(DetailView):
model = Institution
context_object_name = "institution"
template_name = "institution_detail.html"

def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
institution = self.get_object()

# Show the Cantus and Bower sources in separate tables, and pre-format
# the external authority links.
cantus_segment = Segment.objects.get(id=4063)
bower_segment = Segment.objects.get(id=4064)
cantus_sources = Source.objects.filter(
holding_institution=institution, segment=cantus_segment
).select_related("holding_institution")
bower_sources = Source.objects.filter(
holding_institution=institution, segment=bower_segment
).select_related("holding_institution")
institution_authorities = InstitutionIdentifier.objects.filter(
institution=institution
)

display_unpublished = self.request.user.is_authenticated
if display_unpublished is False:
cantus_sources = cantus_sources.filter(published=True)
bower_sources = bower_sources.filter(published=True)

formatted_authorities = []
for authority in institution_authorities:
formatted_authorities.append(
(authority.identifier_label, authority.identifier_url)
)

context["cantus_sources"] = cantus_sources
context["num_cantus_sources"] = cantus_sources.count()
context["bower_sources"] = bower_sources
context["num_bower_sources"] = bower_sources.count()
context["institution_authorities"] = formatted_authorities

return context
Loading