Skip to content

Commit a9c7b8b

Browse files
authored
Merge pull request #41 from SADiLaR/feature/number_of_projects
Add dynamic project count to institutions
2 parents c68c4fb + 74760e4 commit a9c7b8b

File tree

4 files changed

+65
-4
lines changed

4 files changed

+65
-4
lines changed

app/app/settings.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@
135135
# Static files (CSS, JavaScript, Images)
136136
# https://docs.djangoproject.com/en/5.0/howto/static-files/
137137

138+
139+
if DEBUG:
140+
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
141+
else:
142+
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
143+
144+
138145
STATIC_URL = "/static/"
139146
STATICFILES_DIRS = [
140147
BASE_DIR / "static",
@@ -146,7 +153,7 @@
146153
"BACKEND": "django.core.files.storage.FileSystemStorage",
147154
},
148155
"staticfiles": {
149-
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
156+
"BACKEND": STATICFILES_STORAGE,
150157
},
151158
}
152159

app/app/views.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.db.models import Count
12
from django.http import HttpResponse
23
from django.shortcuts import render
34

@@ -20,9 +21,13 @@ def home(request):
2021

2122
def institutions(request):
2223
template = "app/institutions.html"
24+
context = {}
2325

24-
institutions = Institution.objects.all().order_by("name").values()
25-
context = {"current_page": "institutions", "institutions": institutions}
26+
institutions = Institution.objects.annotate(project_count=Count("project")).order_by("name")
27+
context = {
28+
"current_page": "institutions",
29+
"institutions": institutions,
30+
}
2631

2732
return render(request, template_name=template, context=context)
2833

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from django.test import Client, TestCase
2+
from django.urls import reverse
3+
4+
from general.models import Institution, Project
5+
6+
7+
class InstitutionsViewTestCase(TestCase):
8+
def setUp(self):
9+
Institution.objects.create(name="Institution Banana")
10+
Institution.objects.create(name="Institution Apple")
11+
12+
inst1 = Institution.objects.get(name="Institution Banana")
13+
Project.objects.create(name="Test Project 1", institution=inst1)
14+
Project.objects.create(name="Test Project 2", institution=inst1)
15+
16+
self.url = reverse("institutions")
17+
18+
def test_institutions_view_correct_template_used(self):
19+
response = self.client.get(self.url)
20+
21+
self.assertTemplateUsed(response, "app/institutions.html")
22+
23+
def test_institutions_view_correct_context_returned(self):
24+
response = self.client.get(self.url)
25+
26+
self.assertIn("current_page", response.context)
27+
self.assertIn("institutions", response.context)
28+
self.assertEqual(response.context["current_page"], "institutions")
29+
30+
def test_institutions_view_correct_projects_returned(self):
31+
response = self.client.get(self.url)
32+
33+
institutions = response.context["institutions"]
34+
self.assertEqual(len(institutions), 2)
35+
self.assertTrue(all(hasattr(inst, "project_count") for inst in institutions))
36+
self.assertEqual(institutions[0].project_count, 0)
37+
self.assertEqual(institutions[1].project_count, 2)
38+
39+
def test_institutions_view_queries(self):
40+
response = self.client.get(self.url)
41+
42+
with self.assertNumQueries(1):
43+
response = self.client.get(self.url)
44+
45+
self.assertEqual(response.status_code, 200)

app/templates/app/institutions.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ <h5 class="card-title">{{ institution.name }}</h5>
1414
{{ institution.abbreviation }}
1515
</p>
1616
<p class="card-text">
17-
4 applicable projects
17+
{% if institution.project_count == 0 %}
18+
no applicable project
19+
{% else %}
20+
{{institution.project_count}} applicable projects
21+
{% endif %}
1822
</p>
1923
<br>
2024
</div>

0 commit comments

Comments
 (0)