From 10cf53b65bc84a965e5a41353a8beabb0daddf6a Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Tue, 22 Apr 2025 19:13:57 +0000 Subject: [PATCH 1/6] feat: add District context for templates --- pems/districts/views.py | 11 ++++++++++- tests/pytest/pems/districts/test_views.py | 24 ++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/pems/districts/views.py b/pems/districts/views.py index 86fae4e..e41c2d4 100644 --- a/pems/districts/views.py +++ b/pems/districts/views.py @@ -1,9 +1,16 @@ from django.views.generic import TemplateView +from .models import District + class IndexView(TemplateView): template_name = "districts/index.html" + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + context["district_queryset"] = District.objects.all() + return context + class DistrictView(TemplateView): template_name = "districts/district.html" @@ -11,5 +18,7 @@ class DistrictView(TemplateView): def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) district = self.kwargs.get("district") - context["district"] = district + context["url_district"] = district + context["district_queryset"] = District.objects.all() + context["district_query"] = District.objects.all().get(number=district) return context diff --git a/tests/pytest/pems/districts/test_views.py b/tests/pytest/pems/districts/test_views.py index d5451f1..3ce9218 100644 --- a/tests/pytest/pems/districts/test_views.py +++ b/tests/pytest/pems/districts/test_views.py @@ -1,6 +1,26 @@ import pytest from pems.districts import views +from pems.districts.models import District + + +class TestIndexViewView: + @pytest.fixture + def view(app_request): + v = views.IndexView() + v.setup(app_request) + + return v + + @pytest.mark.django_db + def test_get_context_data(self, view): + + context = view.get_context_data() + + assert set(context["district_queryset"]) == set(District.objects.all()) + + def test_template_name(self, view): + assert view.template_name == "districts/index.html" class TestDistrictView: @@ -11,11 +31,13 @@ def view(app_request): return v + @pytest.mark.django_db + @pytest.mark.usefixtures("model_District") def test_get_context_data(self, view): context = view.get_context_data() - assert context["district"] == 1 + assert context["url_district"] == 1 def test_template_name(self, view): assert view.template_name == "districts/district.html" From 46f09695e74d209731b589f35e98a54243542bb4 Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Tue, 22 Apr 2025 19:23:26 +0000 Subject: [PATCH 2/6] feat: add ui placeholders for the districts pages --- .../templates/districts/district.html | 26 +++++++++++- pems/districts/templates/districts/index.html | 41 ++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/pems/districts/templates/districts/district.html b/pems/districts/templates/districts/district.html index 9c77165..9253519 100644 --- a/pems/districts/templates/districts/district.html +++ b/pems/districts/templates/districts/district.html @@ -1,2 +1,24 @@ -

District {{ district }}

-

URL of route: {% url "districts:district" district %}

+{% extends "districts/index.html" %} + +{% block headline %} + District {{ url_district }} - {{ district_query.name }} +{% endblock headline %} + +{% block district-content %} +
+
+

Form

+
+
+

Chart

+
+
+
+
+

Details for {{ district_query.name }}

+
+
+

Map

+
+
+{% endblock district-content %} diff --git a/pems/districts/templates/districts/index.html b/pems/districts/templates/districts/index.html index b199e5a..33a97b1 100644 --- a/pems/districts/templates/districts/index.html +++ b/pems/districts/templates/districts/index.html @@ -1,7 +1,46 @@ {% extends "core/base.html" %} + {% block headline %} Districts {% endblock headline %} + {% block inner-content %} -

Districts

+
+
+ +
+ + {% block district-content %} +
+
+

Form

+
+
+

Chart

+
+
+
+
+

Details

+
+
+

Map

+
+
+ {% endblock district-content %} + +
+
+
{% endblock inner-content %} From 7b93b1f2f262d321337bebb523958c3b0f37ea23 Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Wed, 23 Apr 2025 18:24:34 +0000 Subject: [PATCH 3/6] feat: re-organize context for templates using a mixin context that will be shared across views of the districts app is moved into the DistrictContextMixin. This reduces context duplication, both when different views need the same context and also when the view may not need the context, but the template it extends does need it. Note that because of how the MRO and mixins work, DistrictContextMixin inherits from TemplateView. --- .../templates/districts/district.html | 4 ++-- pems/districts/templates/districts/index.html | 2 +- pems/districts/views.py | 20 +++++++++++-------- tests/pytest/pems/districts/test_views.py | 6 +++--- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/pems/districts/templates/districts/district.html b/pems/districts/templates/districts/district.html index 9253519..4697160 100644 --- a/pems/districts/templates/districts/district.html +++ b/pems/districts/templates/districts/district.html @@ -1,7 +1,7 @@ {% extends "districts/index.html" %} {% block headline %} - District {{ url_district }} - {{ district_query.name }} + District {{ district_number }} - {{ district.name }} {% endblock headline %} {% block district-content %} @@ -15,7 +15,7 @@

Chart

-

Details for {{ district_query.name }}

+

Details for {{ district.name }}

Map

diff --git a/pems/districts/templates/districts/index.html b/pems/districts/templates/districts/index.html index 33a97b1..47d8c18 100644 --- a/pems/districts/templates/districts/index.html +++ b/pems/districts/templates/districts/index.html @@ -11,7 +11,7 @@
- {% block district-content %} + {% block districts-content %}

Form

@@ -38,7 +38,7 @@

Details

Map

- {% endblock district-content %} + {% endblock districts-content %}
From 5cb85e25665f2ab20c6cee69255f4d879c784315 Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Wed, 23 Apr 2025 20:18:50 +0000 Subject: [PATCH 5/6] refactor(views): DistrictView inherits from DetailView using DetailView for the DistrictView class simplifies the code and improves readability in the districts/district.html template. --- .../templates/districts/district.html | 2 +- pems/districts/views.py | 14 +++++------ tests/pytest/pems/districts/test_views.py | 24 ++++++------------- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/pems/districts/templates/districts/district.html b/pems/districts/templates/districts/district.html index 301b96e..fe6ba37 100644 --- a/pems/districts/templates/districts/district.html +++ b/pems/districts/templates/districts/district.html @@ -1,7 +1,7 @@ {% extends "districts/index.html" %} {% block headline %} - District {{ district_number }} - {{ district.name }} + District {{ district.number }} - {{ district.name }} {% endblock headline %} {% block districts-content %} diff --git a/pems/districts/views.py b/pems/districts/views.py index 98faf18..bd991b0 100644 --- a/pems/districts/views.py +++ b/pems/districts/views.py @@ -1,4 +1,4 @@ -from django.views.generic import TemplateView +from django.views.generic import TemplateView, DetailView from .models import District @@ -17,12 +17,10 @@ class IndexView(DistrictContextMixin, TemplateView): template_name = "districts/index.html" -class DistrictView(DistrictContextMixin, TemplateView): +class DistrictView(DistrictContextMixin, DetailView): + model = District + context_object_name = "district" template_name = "districts/district.html" - def get_context_data(self, **kwargs): - context = super().get_context_data(**kwargs) - district_number = self.kwargs.get("district") - context["district_number"] = district_number - context["district"] = context.get("districts").get("all").get(number=district_number) - return context + def get_object(self): + return District.objects.get(number__iexact=self.kwargs["district"]) diff --git a/tests/pytest/pems/districts/test_views.py b/tests/pytest/pems/districts/test_views.py index 2ea71ad..6e00d57 100644 --- a/tests/pytest/pems/districts/test_views.py +++ b/tests/pytest/pems/districts/test_views.py @@ -1,5 +1,6 @@ import pytest +from django.urls import reverse from pems.districts import views from pems.districts.models import District @@ -23,21 +24,10 @@ def test_template_name(self, view): assert view.template_name == "districts/index.html" -class TestDistrictView: - @pytest.fixture - def view(app_request): - v = views.DistrictView() - v.setup(app_request, district=1) - - return v +@pytest.mark.django_db +def test_district_view(client, model_District): + url = reverse("districts:district", kwargs={"district": 1}) + response = client.get(url) - @pytest.mark.django_db - @pytest.mark.usefixtures("model_District") - def test_get_context_data(self, view): - - context = view.get_context_data() - - assert context["district_number"] == 1 - - def test_template_name(self, view): - assert view.template_name == "districts/district.html" + assert response.status_code == 200 + assert response.context["district"] == model_District From 6f2679caf23d30d109b4ccd4cef74e55e7c5d492 Mon Sep 17 00:00:00 2001 From: Luis Alvergue Date: Thu, 24 Apr 2025 20:17:26 +0000 Subject: [PATCH 6/6] refactor: rename URL parameter and template context for clarity now the URL parameter is more descriptive, it is the district number, and the template context refers to the currently selected district --- pems/districts/templates/districts/district.html | 4 ++-- pems/districts/urls.py | 2 +- pems/districts/views.py | 4 ++-- tests/pytest/pems/districts/test_views.py | 4 ++-- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/pems/districts/templates/districts/district.html b/pems/districts/templates/districts/district.html index fe6ba37..0211a3e 100644 --- a/pems/districts/templates/districts/district.html +++ b/pems/districts/templates/districts/district.html @@ -1,7 +1,7 @@ {% extends "districts/index.html" %} {% block headline %} - District {{ district.number }} - {{ district.name }} + District {{ current_district.number }} - {{ current_district.name }} {% endblock headline %} {% block districts-content %} @@ -15,7 +15,7 @@

Chart

-

Details for {{ district.name }}

+

Details for {{ current_district.name }}

Map

diff --git a/pems/districts/urls.py b/pems/districts/urls.py index acdacda..a56d963 100644 --- a/pems/districts/urls.py +++ b/pems/districts/urls.py @@ -10,5 +10,5 @@ urlpatterns = [ # /districts path("", views.IndexView.as_view(), name="index"), - path("", views.DistrictView.as_view(), name="district"), + path("", views.DistrictView.as_view(), name="district"), ] diff --git a/pems/districts/views.py b/pems/districts/views.py index bd991b0..978c693 100644 --- a/pems/districts/views.py +++ b/pems/districts/views.py @@ -19,8 +19,8 @@ class IndexView(DistrictContextMixin, TemplateView): class DistrictView(DistrictContextMixin, DetailView): model = District - context_object_name = "district" + context_object_name = "current_district" template_name = "districts/district.html" def get_object(self): - return District.objects.get(number__iexact=self.kwargs["district"]) + return District.objects.get(number__iexact=self.kwargs["district_number"]) diff --git a/tests/pytest/pems/districts/test_views.py b/tests/pytest/pems/districts/test_views.py index 6e00d57..5776e00 100644 --- a/tests/pytest/pems/districts/test_views.py +++ b/tests/pytest/pems/districts/test_views.py @@ -26,8 +26,8 @@ def test_template_name(self, view): @pytest.mark.django_db def test_district_view(client, model_District): - url = reverse("districts:district", kwargs={"district": 1}) + url = reverse("districts:district", kwargs={"district_number": model_District.number}) response = client.get(url) assert response.status_code == 200 - assert response.context["district"] == model_District + assert response.context["current_district"] == model_District