diff --git a/app/app/views.py b/app/app/views.py index 356dc858..3f24687f 100644 --- a/app/app/views.py +++ b/app/app/views.py @@ -291,11 +291,44 @@ def languages(request): def subjects(request): template = "app/subjects.html" - project = Project.objects.get(id=project_id) + subjects = ( + Subject.objects.all() + .prefetch_related( + Prefetch( + "documentfile_set", + queryset=DocumentFile.objects.only("id", "title", "subjects").order_by("title"), + ), + Prefetch( + "project_set", + queryset=Project.objects.only("id", "name", "subjects").order_by("name"), + ), + ) + .order_by("name") + ) + + subject_data = [] + + for subject in subjects: + documents = subject.documentfile_set.all() + projects = subject.project_set.all() + if documents or projects: + subject_data.append( + { + "subject": subject, + "documents": documents, + "projects": projects, + } + ) + + paginator = Paginator(subject_data, 10) + + page_number = request.GET.get("page") + page_obj = paginator.get_page(page_number) context = { - "current_page": "subject_detail", - "project": project, + "current_page": "subjects", + "subject_data": page_obj.object_list, + "page_obj": page_obj, } return render(request, template_name=template, context=context) diff --git a/app/general/tests/tests_subject.py b/app/general/tests/tests_subject.py index 53cc60c0..4e7666f2 100644 --- a/app/general/tests/tests_subject.py +++ b/app/general/tests/tests_subject.py @@ -1,15 +1,37 @@ -import unittest - from django.test import TestCase +from django.urls import reverse -from general.models import Subject +from general.models import DocumentFile, Institution, Project, Subject -class TestSubject(TestCase): +class TestSubjects(TestCase): def setUp(self): + self.institution = Institution.objects.create( + name="Test Institution", + abbreviation="TI", + url="http://testinstitution.org", + email="info@testinstitution.org", + ) + self.subject1 = Subject.objects.create(name="Mathematics") self.subject2 = Subject.objects.create(name="Science") + self.document1 = DocumentFile.objects.create( + title="Document 1", institution=self.institution, document_type="report" + ) + self.document1.subjects.add(self.subject1) + + self.document2 = DocumentFile.objects.create( + title="Document 2", institution=self.institution, document_type="report" + ) + self.document2.subjects.add(self.subject2) + + self.project1 = Project.objects.create(name="Project 1", institution=self.institution) + self.project1.subjects.add(self.subject1) + + self.project2 = Project.objects.create(name="Project 2", institution=self.institution) + self.project2.subjects.add(self.subject2) + def test_subject_creation(self): self.assertEqual(str(self.subject1), "Mathematics") self.assertEqual(str(self.subject2), "Science") @@ -22,6 +44,11 @@ def test_history_records_creation(self): self.assertEqual(self.subject1.history.count(), 1) self.assertEqual(self.subject1.history.first().name, "Mathematics") + def test_subjects_view_num_queries(self): + with self.assertNumQueries(3): + response = self.client.get(reverse("subjects")) -if __name__ == "__main__": - unittest.main() + self.assertEqual(response.status_code, 200) + self.assertTemplateUsed(response, "app/subjects.html") + self.assertTrue("subject_data" in response.context) + self.assertTrue("page_obj" in response.context) diff --git a/app/templates/app/languages.html b/app/templates/app/languages.html index cdb95cea..39f1fd69 100644 --- a/app/templates/app/languages.html +++ b/app/templates/app/languages.html @@ -4,10 +4,10 @@ {% block content %}
-
+

{% trans "Languages" %}


-
+
{% for item in language_data %}
{{ item.language.name }}
diff --git a/app/templates/app/subjects.html b/app/templates/app/subjects.html index e69de29b..f2b25e77 100644 --- a/app/templates/app/subjects.html +++ b/app/templates/app/subjects.html @@ -0,0 +1,83 @@ +{% extends "base.html" %} +{% load static %} +{% load i18n %} + +{% block content %} +
+
+

{% trans "Subjects" %}

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

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

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

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

+ {% endif %} +
+
+
+
+ {% endfor %} +
+ + {% if page_obj.has_other_pages %} +
+ +
+ {% endif %} +
+{% endblock content %} diff --git a/app/templates/base.html b/app/templates/base.html index 052c5865..f8dce1e9 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -63,6 +63,12 @@ class="nav-link" href="{% url 'languages' %}">{% trans "Languages" %} +