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

language page #99

Merged
merged 1 commit into from
Jul 24, 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
4 changes: 2 additions & 2 deletions app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
path("projects/<int:project_id>/", views.project_detail, name="project_detail"),
path("institution/<int:institution_id>/", views.institution_detail, name="institution_detail"),
path("documents/<int:document_id>/", views.document_detail, name="document_detail"),
path("language/<int:pk>/", views.language_detail, name="language_detail"),
path("subject/<int:pk>/", 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")),
Expand Down
39 changes: 33 additions & 6 deletions app/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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

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)
54 changes: 54 additions & 0 deletions app/templates/app/languages.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{% extends "base.html" %}
{% load static %}
{% load i18n %}

{% block content %}
<br/>
<div class="detail">
<h2>{% trans "Languages" %}</h2>
<br>
<div class="detail-row row">
{% for item in language_data %}
<h5>{{ item.language.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 language." %}</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 language." %}</p>
{% endif %}
</div>
<br>
<hr>
<br>
{% endfor %}
</div>
</div>

{% endblock content %}
Empty file.
6 changes: 6 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@
class="nav-link" href="{% url 'documents' %}">{% trans "Documents" %}
</a>
</li>
<li class="nav-item">
<a
{% if current_page == 'languages' %} class="nav-link active" aria-current="page" {% endif %}
class="nav-link" href="{% url 'languages' %}">{% trans "Languages" %}
</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{% url 'admin:index' %}">{% trans "Admin" %}</a>
</li>
Expand Down
Loading