-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add view, template, unit test, and styling for subjects page
- Loading branch information
“OMosimege”
committed
Jul 25, 2024
1 parent
cb1a749
commit 0e22a57
Showing
5 changed files
with
164 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,42 @@ | ||
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 subjectsViewTest(TestCase): | ||
def setUp(self): | ||
self.subject1 = Subject.objects.create(name="Mathematics") | ||
self.subject2 = Subject.objects.create(name="Science") | ||
|
||
def test_subject_creation(self): | ||
self.assertEqual(str(self.subject1), "Mathematics") | ||
self.assertEqual(str(self.subject2), "Science") | ||
|
||
def test_subject_name_uniqueness(self): | ||
with self.assertRaises(Exception): | ||
Subject.objects.create(name="Mathematics") | ||
|
||
def test_history_records_creation(self): | ||
self.assertEqual(self.subject1.history.count(), 1) | ||
self.assertEqual(self.subject1.history.first().name, "Mathematics") | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() | ||
self.institution = Institution.objects.create( | ||
name="Test Institution", | ||
abbreviation="TI", | ||
url="http://testinstitution.org", | ||
email="[email protected]", | ||
) | ||
|
||
self.subject1 = Subject.objects.create(name="English") | ||
self.subject2 = Subject.objects.create(name="Spanish") | ||
|
||
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_subjects_view_num_queries(self): | ||
with self.assertNumQueries(3): | ||
response = self.client.get(reverse("subjects")) | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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">« {% trans "First" %}</a></li> | ||
<li class="page-item"><a class="page-link" href="?page={{ page_obj.previous_page_number }}">‹ {% trans "Previous" %}</a></li> | ||
{% else %} | ||
<li class="page-item disabled"><a class="page-link" href="#">« {% trans "First" %}</a></li> | ||
<li class="page-item disabled"><a class="page-link" href="#">‹ {% 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" %} ›</a></li> | ||
<li class="page-item"><a class="page-link" href="?page={{ page_obj.paginator.num_pages }}">{% trans "Last" %} »</a></li> | ||
{% else %} | ||
<li class="page-item disabled"><a class="page-link" href="#">{% trans "Next" %} ›</a></li> | ||
<li class="page-item disabled"><a class="page-link" href="#">{% trans "Last" %} »</a></li> | ||
{% endif %} | ||
</ul> | ||
</div> | ||
{% endif %} | ||
</div> | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters