From 5126c40c8ac6fae77b8f04806d59ec5be078836e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9COMosimege=E2=80=9D?= <“onalerona.mosimege@gmail.com”> Date: Thu, 18 Jul 2024 12:14:35 +0200 Subject: [PATCH] Add document detail view, url, and template --- app/app/urls.py | 1 + app/app/views.py | 22 ++++++- app/general/tests/test_document_detail.py | 41 +++++++++++++ app/templates/app/document_detail.html | 71 +++++++++++++++++++++++ app/templates/app/documents.html | 7 +++ 5 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 app/general/tests/test_document_detail.py create mode 100644 app/templates/app/document_detail.html create mode 100644 app/templates/app/documents.html diff --git a/app/app/urls.py b/app/app/urls.py index 649d0d9b..a9986360 100644 --- a/app/app/urls.py +++ b/app/app/urls.py @@ -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//", views.project_detail, name="project_detail"), path("institution//", views.institution_detail, name="institution_detail"), diff --git a/app/app/views.py b/app/app/views.py index 9e0eb798..7d4538e9 100644 --- a/app/app/views.py +++ b/app/app/views.py @@ -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 @@ -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) diff --git a/app/general/tests/test_document_detail.py b/app/general/tests/test_document_detail.py new file mode 100644 index 00000000..f02687fa --- /dev/null +++ b/app/general/tests/test_document_detail.py @@ -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) diff --git a/app/templates/app/document_detail.html b/app/templates/app/document_detail.html new file mode 100644 index 00000000..1bab19ca --- /dev/null +++ b/app/templates/app/document_detail.html @@ -0,0 +1,71 @@ +{% extends "base.html" %} +{% load static %} +{% load i18n %} + +{% block content %} +
+
+

{% trans "Documents" %} > {{ document.title }}

+ +
+

{{ document.title }}

+ + + {{ document.url }} + + + + +
+

{{ document.description }}

+
+ +

+ + {{ document.institution.name }} + +

+ +

{% trans "License:" %} {{ document.license }}

+

{% trans "Category:" %} {{ document.document_type }}

+
+ + +
+
+
+

{% trans "Subjects" %}

+
    + {% for subject in document.subjects.all %} +
  • + + {{ subject.name }} +
  • + {% endfor %} +
+
+
+ +
+
+

{% trans "Languages" %}

+ +
+
+
+
+ +{% endblock content %} diff --git a/app/templates/app/documents.html b/app/templates/app/documents.html new file mode 100644 index 00000000..84d76d25 --- /dev/null +++ b/app/templates/app/documents.html @@ -0,0 +1,7 @@ +{% extends "base.html" %} +{% load static %} +{% load i18n %} + +{% block content %} + +{% endblock content%}