From c7f90bff8d92581aed379bb3d6a2619adcb6bc0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9COMosimege=E2=80=9D?= <“onalerona.mosimege@gmail.com”> Date: Tue, 23 Jul 2024 09:16:02 +0200 Subject: [PATCH] add language page template, view, and styling --- app/app/urls.py | 4 +- app/app/views.py | 39 +++++++++++--- app/general/tests/test_languages.py | 38 +++++++++++++ app/templates/app/languages.html | 54 +++++++++++++++++++ app/templates/app/subject_detail.html | 0 .../{language_detail.html => subjects.html} | 0 app/templates/base.html | 6 +++ 7 files changed, 133 insertions(+), 8 deletions(-) create mode 100644 app/general/tests/test_languages.py create mode 100644 app/templates/app/languages.html delete mode 100644 app/templates/app/subject_detail.html rename app/templates/app/{language_detail.html => subjects.html} (100%) diff --git a/app/app/urls.py b/app/app/urls.py index a9986360..7ba0c260 100644 --- a/app/app/urls.py +++ b/app/app/urls.py @@ -37,8 +37,8 @@ path("projects//", views.project_detail, name="project_detail"), path("institution//", views.institution_detail, name="institution_detail"), path("documents//", views.document_detail, name="document_detail"), - path("language//", views.language_detail, name="language_detail"), - path("subject//", views.subject_detail, name="subject_detail"), + path("languages/", views.languages, name="languages"), + path("subjects/", views.subjects, name="subjects"), path("search/", views.search, name="search"), path("i18n/", include("django.conf.urls.i18n")), path("accounts/", include("accounts.urls")), diff --git a/app/app/views.py b/app/app/views.py index 9747ad8a..356dc858 100644 --- a/app/app/views.py +++ b/app/app/views.py @@ -250,19 +250,46 @@ def document_detail(request, document_id): return render(request, template_name=template, context=context) -def language_detail(request, project_id): - template = "app/language_detail.html" +def languages(request): + template = "app/languages.html" + + languages = ( + Language.objects.all() + .prefetch_related( + Prefetch( + "documentfile_set", + queryset=DocumentFile.objects.only("id", "title", "languages").order_by("title"), + ), + Prefetch( + "project_set", + queryset=Project.objects.only("id", "name", "languages").order_by("name"), + ), + ) + .order_by("name") + ) - project = Project.objects.get(id=project_id) + language_data = [] + + for language in languages: + documents = language.documentfile_set.all() + projects = language.project_set.all() + language_data.append( + { + "language": language, + "documents": documents, + "projects": projects, + } + ) context = { - "current_page": "language_detail", + "current_page": "languages", + "language_data": language_data, } return render(request, template_name=template, context=context) -def subject_detail(request, project_id): - template = "app/subject_detail.html" +def subjects(request): + template = "app/subjects.html" project = Project.objects.get(id=project_id) diff --git a/app/general/tests/test_languages.py b/app/general/tests/test_languages.py new file mode 100644 index 00000000..8029af05 --- /dev/null +++ b/app/general/tests/test_languages.py @@ -0,0 +1,38 @@ +from django.test import TestCase +from django.urls import reverse + +from general.models import DocumentFile, Institution, Language, Project + + +class LanguagesViewTest(TestCase): + def setUp(self): + self.institution = Institution.objects.create( + name="Test Institution", + abbreviation="TI", + url="http://testinstitution.org", + email="info@testinstitution.org", + ) + + self.language1 = Language.objects.create(name="English", iso_code="lang1") + self.language2 = Language.objects.create(name="Spanish", iso_code="lang2") + + self.document1 = DocumentFile.objects.create( + title="Document 1", institution=self.institution, document_type="report" + ) + self.document1.languages.add(self.language1) + + self.document2 = DocumentFile.objects.create( + title="Document 2", institution=self.institution, document_type="report" + ) + self.document2.languages.add(self.language2) + + self.project1 = Project.objects.create(name="Project 1", institution=self.institution) + self.project1.languages.add(self.language1) + + self.project2 = Project.objects.create(name="Project 2", institution=self.institution) + self.project2.languages.add(self.language2) + + def test_languages_view_num_queries(self): + with self.assertNumQueries(3): + response = self.client.get(reverse("languages")) + self.assertEqual(response.status_code, 200) diff --git a/app/templates/app/languages.html b/app/templates/app/languages.html new file mode 100644 index 00000000..cdb95cea --- /dev/null +++ b/app/templates/app/languages.html @@ -0,0 +1,54 @@ +{% extends "base.html" %} +{% load static %} +{% load i18n %} + +{% block content %} +
+
+

{% trans "Languages" %}

+
+
+ {% for item in language_data %} +
{{ item.language.name }}
+
+
{% trans "Documents" %}
+ {% if item.documents.exists %} +
+ +
+ {% else %} +

{% trans "No documents available for this language." %}

+ {% endif %} +
+
+
{% trans "Projects" %}
+ {% if item.projects.exists %} +
+ +
+ {% else %} +

{% trans "No projects available for this language." %}

+ {% endif %} +
+
+
+
+ {% endfor %} +
+
+ +{% endblock content %} diff --git a/app/templates/app/subject_detail.html b/app/templates/app/subject_detail.html deleted file mode 100644 index e69de29b..00000000 diff --git a/app/templates/app/language_detail.html b/app/templates/app/subjects.html similarity index 100% rename from app/templates/app/language_detail.html rename to app/templates/app/subjects.html diff --git a/app/templates/base.html b/app/templates/base.html index d5dc7da3..052c5865 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -57,6 +57,12 @@ class="nav-link" href="{% url 'documents' %}">{% trans "Documents" %} +