Skip to content

Commit

Permalink
Merge pull request #41 from SADiLaR/feature/number_of_projects
Browse files Browse the repository at this point in the history
Add dynamic project count to institutions
  • Loading branch information
OnaMosimege authored May 7, 2024
2 parents c68c4fb + 74760e4 commit a9c7b8b
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
9 changes: 8 additions & 1 deletion app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/


if DEBUG:
STATICFILES_STORAGE = "django.contrib.staticfiles.storage.StaticFilesStorage"
else:
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"


STATIC_URL = "/static/"
STATICFILES_DIRS = [
BASE_DIR / "static",
Expand All @@ -146,7 +153,7 @@
"BACKEND": "django.core.files.storage.FileSystemStorage",
},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
"BACKEND": STATICFILES_STORAGE,
},
}

Expand Down
9 changes: 7 additions & 2 deletions app/app/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db.models import Count
from django.http import HttpResponse
from django.shortcuts import render

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

def institutions(request):
template = "app/institutions.html"
context = {}

institutions = Institution.objects.all().order_by("name").values()
context = {"current_page": "institutions", "institutions": institutions}
institutions = Institution.objects.annotate(project_count=Count("project")).order_by("name")
context = {
"current_page": "institutions",
"institutions": institutions,
}

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

Expand Down
45 changes: 45 additions & 0 deletions app/general/tests/test_views_institution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from django.test import Client, TestCase
from django.urls import reverse

from general.models import Institution, Project


class InstitutionsViewTestCase(TestCase):
def setUp(self):
Institution.objects.create(name="Institution Banana")
Institution.objects.create(name="Institution Apple")

inst1 = Institution.objects.get(name="Institution Banana")
Project.objects.create(name="Test Project 1", institution=inst1)
Project.objects.create(name="Test Project 2", institution=inst1)

self.url = reverse("institutions")

def test_institutions_view_correct_template_used(self):
response = self.client.get(self.url)

self.assertTemplateUsed(response, "app/institutions.html")

def test_institutions_view_correct_context_returned(self):
response = self.client.get(self.url)

self.assertIn("current_page", response.context)
self.assertIn("institutions", response.context)
self.assertEqual(response.context["current_page"], "institutions")

def test_institutions_view_correct_projects_returned(self):
response = self.client.get(self.url)

institutions = response.context["institutions"]
self.assertEqual(len(institutions), 2)
self.assertTrue(all(hasattr(inst, "project_count") for inst in institutions))
self.assertEqual(institutions[0].project_count, 0)
self.assertEqual(institutions[1].project_count, 2)

def test_institutions_view_queries(self):
response = self.client.get(self.url)

with self.assertNumQueries(1):
response = self.client.get(self.url)

self.assertEqual(response.status_code, 200)
6 changes: 5 additions & 1 deletion app/templates/app/institutions.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ <h5 class="card-title">{{ institution.name }}</h5>
{{ institution.abbreviation }}
</p>
<p class="card-text">
4 applicable projects
{% if institution.project_count == 0 %}
no applicable project
{% else %}
{{institution.project_count}} applicable projects
{% endif %}
</p>
<br>
</div>
Expand Down

0 comments on commit a9c7b8b

Please sign in to comment.