Skip to content

Commit d93767a

Browse files
committed
refactor: replace template name property with context property
use a single template that will be given each agency's specific context. no need for the template override field on `TransitAgency` or the individual templates anymore.
1 parent 29b66df commit d93767a

13 files changed

+31
-95
lines changed

benefits/core/admin/transit.py

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ def get_readonly_fields(self, request, obj=None):
3030
if not request.user.is_superuser:
3131
fields.extend(
3232
[
33-
"eligibility_index_template_override",
3433
"eligibility_api_id",
3534
"transit_processor",
3635
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 5.1.5 on 2025-02-27 02:32
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
("core", "0036_in_person_enrollmentflows"),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name="transitagency",
15+
name="eligibility_index_template_override",
16+
),
17+
]

benefits/core/models/transit.py

+3-18
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from benefits.core import context as core_context
1010
from benefits.routes import routes
11-
from .common import PemData, SecretNameField, template_path
11+
from .common import PemData, SecretNameField
1212

1313
logger = logging.getLogger(__name__)
1414

@@ -72,11 +72,6 @@ class TransitAgency(models.Model):
7272
help_text="URL of a website/page with more information about the agency's discounts",
7373
)
7474
phone = models.TextField(default="", blank=True, help_text="Agency customer support phone number")
75-
eligibility_index_template_override = models.TextField(
76-
help_text="Override the default template used for this agency's eligibility landing page",
77-
blank=True,
78-
default="",
79-
)
8075
eligibility_api_id = models.TextField(
8176
help_text="The identifier for this agency used in Eligibility API calls.",
8277
blank=True,
@@ -168,8 +163,8 @@ def index_url(self):
168163
return reverse(routes.AGENCY_INDEX, args=[self.slug])
169164

170165
@property
171-
def eligibility_index_template(self):
172-
return self.eligibility_index_template_override or f"eligibility/index--{self.slug}.html"
166+
def eligibility_index_context(self):
167+
return core_context.eligibility_index[self.slug].dict()
173168

174169
@property
175170
def eligibility_index_url(self):
@@ -197,7 +192,6 @@ def enrollment_flows(self):
197192

198193
def clean(self):
199194
field_errors = {}
200-
template_errors = []
201195

202196
if self.active:
203197
message = "This field is required for active transit agencies."
@@ -219,17 +213,8 @@ def clean(self):
219213
)
220214
field_errors.update({k: ValidationError(message) for k, v in needed.items() if not v})
221215

222-
# since templates are calculated from the pattern or the override field
223-
# we can't add a field-level validation error
224-
# so just create directly for a missing template
225-
for t in [self.eligibility_index_template]:
226-
if not template_path(t):
227-
template_errors.append(ValidationError(f"Template not found: {t}"))
228-
229216
if field_errors:
230217
raise ValidationError(field_errors)
231-
if template_errors:
232-
raise ValidationError(template_errors)
233218

234219
@staticmethod
235220
def by_id(id):

benefits/eligibility/templates/eligibility/index--cst.html

-9
This file was deleted.

benefits/eligibility/templates/eligibility/index--mst.html

-9
This file was deleted.

benefits/eligibility/templates/eligibility/index--nevco.html

-9
This file was deleted.

benefits/eligibility/templates/eligibility/index--sacrt.html

-12
This file was deleted.

benefits/eligibility/templates/eligibility/index--sbmtd.html

-9
This file was deleted.

benefits/eligibility/templates/eligibility/index.html

+8-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@ <h1>{% translate "Choose the transit benefit you would like to enroll in" %}</h1
1919
{% endblock headline %}
2020

2121
{% block inner-content %}
22-
{% block form-text %}
23-
{% endblock form-text %}
22+
{% for text in form_text %}
23+
{% if not forloop.last %}
24+
<p class="pt-4">{% translate text %}</p>
25+
{% else %}
26+
<p class="pt-4 pb-4">{% translate text %}</p>
27+
{% endif %}
28+
{% endfor %}
29+
2430
{% include "core/includes/form.html" with form=form %}
2531
<script nonce="{{ request.csp_nonce }}">
2632
document.querySelectorAll(".modal").forEach((modal) => {

benefits/eligibility/views.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ def index(request):
4949
context["form"] = form
5050
response = TemplateResponse(request, agency.eligibility_index_template, context)
5151
else:
52-
response = TemplateResponse(request, agency.eligibility_index_template, context)
52+
context.update(agency.eligibility_index_context)
53+
response = TemplateResponse(request, "eligibility/index.html", context)
5354

5455
return response
5556

tests/pytest/conftest.py

-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ def model_TransitAgency(model_PemData, model_TransitProcessor):
176176
eligibility_api_id="test123",
177177
eligibility_api_private_key=model_PemData,
178178
eligibility_api_public_key=model_PemData,
179-
eligibility_index_template_override="eligibility/index.html",
180179
logo_large="agencies/cst-lg.png",
181180
logo_small="agencies/cst-sm.png",
182181
)

tests/pytest/core/admin/test_transit.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def test_get_exclude(self, admin_user_request, agency_admin_model, user_type, ex
5252
[
5353
(
5454
"staff",
55-
["eligibility_index_template_override", "eligibility_api_id", "transit_processor"],
55+
["eligibility_api_id", "transit_processor"],
5656
),
5757
("super", ()),
5858
],

tests/pytest/core/models/test_transit.py

-23
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,6 @@ def test_TransitAgency_str(model_TransitAgency):
3232
assert str(model_TransitAgency) == model_TransitAgency.long_name
3333

3434

35-
@pytest.mark.django_db
36-
def test_TransitAgency_template_overrides(model_TransitAgency):
37-
assert model_TransitAgency.eligibility_index_template == model_TransitAgency.eligibility_index_template_override
38-
39-
model_TransitAgency.eligibility_index_template_override = ""
40-
model_TransitAgency.save()
41-
42-
assert model_TransitAgency.eligibility_index_template == f"eligibility/index--{model_TransitAgency.slug}.html"
43-
44-
4535
@pytest.mark.django_db
4636
def test_TransitAgency_index_url(model_TransitAgency):
4737
result = model_TransitAgency.index_url
@@ -176,16 +166,3 @@ def test_TransitAgency_clean(model_TransitAgency_inactive, model_TransitProcesso
176166
assert "transit_processor_audience" in errors
177167
assert "transit_processor_client_id" in errors
178168
assert "transit_processor_client_secret_name" in errors
179-
180-
181-
@pytest.mark.django_db
182-
@pytest.mark.parametrize("template_attribute", ["eligibility_index_template_override"])
183-
def test_TransitAgency_clean_templates(model_TransitAgency_inactive, template_attribute):
184-
setattr(model_TransitAgency_inactive, template_attribute, "does/not/exist.html")
185-
# agency is inactive, OK to have missing template
186-
model_TransitAgency_inactive.clean()
187-
188-
# now mark it active and expect failure on clean()
189-
model_TransitAgency_inactive.active = True
190-
with pytest.raises(ValidationError, match="Template not found: does/not/exist.html"):
191-
model_TransitAgency_inactive.clean()

0 commit comments

Comments
 (0)