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

individual document page #91

Merged
merged 1 commit into from
Jul 18, 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
1 change: 1 addition & 0 deletions app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
path("_health/", views.health, name="health"),
path("", views.home, name="home"),
path("institutions/", views.institutions, name="institutions"),
path("documents/", views.documents, name="documents"),
path("projects/", views.projects, name="projects"),
path("projects/<int:project_id>/", views.project_detail, name="project_detail"),
path("institution/<int:institution_id>/", views.institution_detail, name="institution_detail"),
Expand Down
22 changes: 19 additions & 3 deletions app/app/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from django.db.models import Count
from django.db.models import Count, Prefetch
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, render
from django.utils.http import urlencode
Expand Down Expand Up @@ -158,13 +158,29 @@ def institution_detail(request, institution_id):
return render(request, template_name=template, context=context)


def document_detail(request, project_id):
def documents(request):
template = "app/documents.html"

context = {
"current_page": "documents",
}
return render(request, template_name=template, context=context)


def document_detail(request, document_id):
template = "app/document_detail.html"

document = DocumentFile.objects.get(id=document_id)
document = get_object_or_404(
DocumentFile.objects.select_related("institution").prefetch_related(
Prefetch("subjects", queryset=Subject.objects.order_by("name")),
Prefetch("languages", queryset=Language.objects.order_by("name")),
),
id=document_id,
)

context = {
"current_page": "document_detail",
"document": document,
}
return render(request, template_name=template, context=context)

Expand Down
41 changes: 41 additions & 0 deletions app/general/tests/test_document_detail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from django.test import TestCase
from django.urls import reverse

from general.models import DocumentFile, Institution, Language, Project, Subject


class DocumentDetailViewTest(TestCase):
def setUp(self):
self.institution = Institution.objects.create(name="University of Cape Town")

self.subject1 = Subject.objects.create(name="Anatomy")
self.subject2 = Subject.objects.create(name="Biology")

self.language1 = Language.objects.create(name="Afrikaans", iso_code="af")
self.language2 = Language.objects.create(name="English", iso_code="en")

self.document = DocumentFile.objects.create(
title="Afrikaans_HL_P1_Feb-March_2011",
description="This is a description of the document.",
url="https://externaldocumentrepository.com/document1",
uploaded_file="path/to/document.pdf",
available=True,
license="(c)",
document_type="Glossary",
institution=self.institution,
mime_type="application/pdf",
document_data="",
search_vector=None,
)
self.document.subjects.add(self.subject1, self.subject2)
self.document.languages.add(self.language1, self.language2)

def test_document_detail_num_queries(self):
url = reverse("document_detail", args=[self.document.id])
with self.assertNumQueries(3):
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
self.assertContains(response, self.document.title)
self.assertContains(response, self.institution.name)
self.assertContains(response, self.subject1.name)
self.assertContains(response, self.language1.name)
71 changes: 71 additions & 0 deletions app/templates/app/document_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
{% extends "base.html" %}
{% load static %}
{% load i18n %}

{% block content %}
<br/>
<div class="detail">
<p><a href="{% url 'documents' %}">{% trans "Documents" %}</a> > {{ document.title }}</p>

<div class="detail-row row">
<h1>{{ document.title }}</h1>

<a href="{{ document.url }}" target="_blank" class="document-url text-decoration-underline float-left" title="External link">
{{ document.url }}
</a>

<div class="mb-3">
<a href="{{ document.uploaded_file.url }}" download>
<button class="btn btn-primary btn-sm" type="submit">
<i class="detail-icon bi-file-earmark-arrow-down-fill"></i> {% trans "Download File" %}
</button>
</a>
</div>

<div class="project-body">
<p>{{ document.description }}</p>
</div>

<p>
<a href="{% url 'institution_detail' document.institution.id %}" class="text-decoration-underline">
{{ document.institution.name }}
</a>
</p>

<p><strong>{% trans "License:" %}</strong> {{ document.license }}</p>
<p><strong>{% trans "Category:" %}</strong> {{ document.document_type }}</p>
</div>


<div class="detail-row row">
<div class="col-md-6 col-12 detail-col">
<div>
<h2>{% trans "Subjects" %}</h2>
<ul>
{% for subject in document.subjects.all %}
<li>
<span class="icon-text"><i class="detail-icon bi-book"></i></span>
<span class="icon-text"><a href="{% url 'subject_detail' subject.id %}">{{ subject.name }}</a></span>
</li>
{% endfor %}
</ul>
</div>
</div>

<div class="col-md-6 col-12 detail-col">
<div>
<h2>{% trans "Languages" %}</h2>
<ul>
{% for language in document.languages.all %}
<li>
<span class="icon-text"><i class="detail-icon bi-vector-pen"></i></span>
<span class="icon-text"><a href="{% url 'language_detail' language.id %}">{{ language.name }}</a></span>
</li>
{% endfor %}
</ul>
</div>
</div>
</div>
</div>

{% endblock content %}
7 changes: 7 additions & 0 deletions app/templates/app/documents.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{% extends "base.html" %}
{% load static %}
{% load i18n %}

{% block content %}

{% endblock content%}
Loading