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

Subjects list page #102

Merged
merged 1 commit into from
Jul 25, 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
39 changes: 36 additions & 3 deletions app/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
39 changes: 33 additions & 6 deletions app/general/tests/tests_subject.py
Original file line number Diff line number Diff line change
@@ -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="[email protected]",
)

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")
Expand All @@ -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)
4 changes: 2 additions & 2 deletions app/templates/app/languages.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

{% block content %}
<br/>
<div class="detail">
<div class="project-list">
<h2>{% trans "Languages" %}</h2>
<br>
<div class="detail-row row">
<div class="row">
{% for item in language_data %}
<h5>{{ item.language.name }}</h5>
<div class="col-md-6 col-12 detail-col">
Expand Down
83 changes: 83 additions & 0 deletions app/templates/app/subjects.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{% extends "base.html" %}
{% load static %}
{% load i18n %}

{% block content %}
<br/>
<div class="project-list">
<h2>{% trans "Subjects" %}</h2>
<br>
<div class="row">
{% for item in subject_data %}
<h5>{{ item.subject.name }}</h5>
<div class="col-md-6 col-12 detail-col">
<h6>{% trans "Documents" %}</h6>
{% if item.documents.exists %}
<div class="documents">
<ul>
{% for document in item.documents %}
<li>
<span class="icon-text"><i class="detail-icon bi-file-earmark"></i></span>
<span class="icon-text"><a href="{% url 'document_detail' document.id %}">{{ document.title }}</a></span>
</li>
{% endfor %}
</ul>
</div>
{% else %}
<p>{% trans "No documents available for this subject." %}</p>
{% endif %}
</div>
<div class="col-md-6 col-12 detail-col">
<h6>{% trans "Projects" %}</h6>
{% if item.projects.exists %}
<div class="projects">
<ul>
{% for project in item.projects %}
<li>
<span class="icon-text"><i class="detail-icon bi-clipboard2"></i></span>
<span class="icon-text"><a href="{% url 'project_detail' project.id %}">{{ project.name }}</a></span>
</li>
{% endfor %}
</ul>
</div>
{% else %}
<p>{% trans "No projects available for this subject." %}</p>
{% endif %}
</div>
<br>
<hr>
<br>
{% endfor %}
</div>

{% if page_obj.has_other_pages %}
<div class="pagination-wrapper d-flex justify-content-center align-items-center">
<ul class="pagination">
{% if page_obj.has_previous %}
<li class="page-item"><a class="page-link" href="?page=1">&laquo; {% trans "First" %}</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">&lsaquo; {% trans "Previous" %}</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">&laquo; {% trans "First" %}</a></li>
<li class="page-item disabled"><a class="page-link" href="#">&lsaquo; {% trans "Previous" %}</a></li>
{% endif %}

{% for num in page_obj.paginator.page_range %}
{% if page_obj.number == num %}
<li class="page-item active"><a class="page-link" href="#">{{ num }}</a></li>
{% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
<li class="page-item"><a class="page-link" href="?page={{ num }}">{{ num }}</a></li>
{% endif %}
{% endfor %}

{% if page_obj.has_next %}
<li class="page-item"><a class="page-link" href="?page={{ page_obj.next_page_number }}">{% trans "Next" %} &rsaquo;</a></li>
<li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">{% trans "Last" %} &raquo;</a></li>
{% else %}
<li class="page-item disabled"><a class="page-link" href="#">{% trans "Next" %} &rsaquo;</a></li>
<li class="page-item disabled"><a class="page-link" href="#">{% trans "Last" %} &raquo;</a></li>
{% endif %}
</ul>
</div>
{% endif %}
</div>
{% endblock content %}
6 changes: 6 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@
class="nav-link" href="{% url 'languages' %}">{% trans "Languages" %}
</a>
</li>
<li class="nav-item">
<a
{% if current_page == 'subjects' %} class="nav-link active" aria-current="page" {% endif %}
class="nav-link" href="{% url 'subjects' %}">{% trans "Subjects" %}
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'admin:index' %}">{% trans "Admin" %}</a>
</li>
Expand Down
Loading