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

Refactor: simplify Eligibility Start templates #2726

Merged
merged 8 commits into from
Mar 18, 2025
1 change: 0 additions & 1 deletion benefits/core/admin/enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ def get_readonly_fields(self, request, obj=None):
"eligibility_api_url",
"eligibility_form_class",
"selection_label_template_override",
"eligibility_start_template_override",
"eligibility_unverified_template_override",
"help_template",
"enrollment_index_template_override",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 5.1.7 on 2025-03-13 18:22

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("core", "0037_remove_transitagency_eligibility_index_template_override"),
]

operations = [
migrations.RemoveField(
model_name="enrollmentflow",
name="eligibility_start_template_override",
),
]
15 changes: 3 additions & 12 deletions benefits/core/models/enrollment.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from .claims import ClaimsProvider
from .transit import TransitAgency
from benefits.core import context as core_context
from benefits.eligibility import context as eligibility_context
from benefits.in_person import context as in_person_context

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -119,11 +120,6 @@ class EnrollmentFlow(models.Model):
default="",
help_text="Override the default template that defines the end-user UI for selecting this flow among other options.",
)
eligibility_start_template_override = models.TextField(
blank=True,
default="",
help_text="Override the default template for the informational page of this flow.",
)
eligibility_unverified_template_override = models.TextField(
blank=True,
default="",
Expand Down Expand Up @@ -195,12 +191,8 @@ def selection_label_template(self):
return self.selection_label_template_override or f"{prefix}--{self.system_name}.html"

@property
def eligibility_start_template(self):
prefix = "eligibility/start"
if self.uses_api_verification:
return self.eligibility_start_template_override or f"{prefix}--{self.agency_card_name}.html"
else:
return self.eligibility_start_template_override or f"{prefix}--{self.system_name}.html"
def eligibility_start_context(self):
return eligibility_context.eligibility_start[self.system_name].dict()

@property
def eligibility_unverified_template(self):
Expand Down Expand Up @@ -268,7 +260,6 @@ def clean(self):
if self.transit_agency:
templates = [
self.selection_label_template,
self.eligibility_start_template,
self.eligibility_unverified_template,
self.enrollment_index_template,
self.enrollment_success_template,
Expand Down
3 changes: 2 additions & 1 deletion benefits/eligibility/context/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .agency import eligibility_index
from .flow import eligibility_start

__all__ = ["eligibility_index"]
__all__ = ["eligibility_index", "eligibility_start"]
80 changes: 80 additions & 0 deletions benefits/eligibility/context/flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from dataclasses import dataclass, asdict
from typing import Optional

from django.utils.translation import gettext_lazy as _

from benefits.core.context import SystemName
from benefits.routes import routes


@dataclass
class CTAButton:
text: str
route: str
fallback_text: Optional[str] = None
extra_classes: Optional[str] = None


@dataclass
class EligibilityStart:
page_title: str
headline_text: str
eligibility_item_template: str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can take a follow-up item to refactor this field away from using these templates, and to encode the item strings directly in context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, great idea! Created #2759 for it

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put it in the sprint, but feel free to change that if you disagree

call_to_action_button: CTAButton

def dict(self):
return asdict(self)


class LoginGovEligibilityStart(EligibilityStart):
def __init__(self, page_title, headline_text):
super().__init__(
page_title=page_title,
headline_text=headline_text,
eligibility_item_template="eligibility/includes/eligibility-item--identification--start--login-gov.html",
call_to_action_button=CTAButton(
text=_("Get started with"), fallback_text="Login.gov", route=routes.OAUTH_LOGIN, extra_classes="login"
),
)


class AgencyCardEligibilityStart(EligibilityStart):
def __init__(self, headline_text, eligibility_item_template):
super().__init__(
page_title=_("Agency card overview"),
headline_text=headline_text,
eligibility_item_template=eligibility_item_template,
call_to_action_button=CTAButton(text=_("Continue"), route=routes.ELIGIBILITY_CONFIRM),
)


eligibility_start = {
SystemName.AGENCY_CARD.value: AgencyCardEligibilityStart(
headline_text=_("You selected an Agency Card transit benefit."),
eligibility_item_template="eligibility/includes/eligibility-item--identification--start--cst-agency-card.html",
),
SystemName.CALFRESH.value: LoginGovEligibilityStart(
page_title=_("CalFresh benefit overview"), headline_text=_("You selected a CalFresh Cardholder transit benefit.")
),
SystemName.COURTESY_CARD.value: AgencyCardEligibilityStart(
headline_text=_("You selected a Courtesy Card transit benefit."),
eligibility_item_template="eligibility/includes/eligibility-item--identification--start--mst-agency-card.html",
),
SystemName.MEDICARE.value: EligibilityStart(
page_title=_("Medicare benefit overview"),
headline_text=_("You selected a Medicare Cardholder transit benefit."),
eligibility_item_template="eligibility/includes/eligibility-item--identification--start--medicare.html",
call_to_action_button=CTAButton(text=_("Continue to Medicare.gov"), route=routes.OAUTH_LOGIN),
),
SystemName.OLDER_ADULT.value: LoginGovEligibilityStart(
page_title=_("Older Adult benefit overview"),
headline_text=_("You selected an Older Adult transit benefit."),
),
SystemName.REDUCED_FARE_MOBILITY_ID.value: AgencyCardEligibilityStart(
headline_text=_("You selected a Reduced Fare Mobility ID transit benefit."),
eligibility_item_template="eligibility/includes/eligibility-item--identification--start--sbmtd-agency-card.html",
),
SystemName.VETERAN.value: LoginGovEligibilityStart(
page_title=_("Veterans benefit overview"), headline_text=_("You selected a Veteran transit benefit.")
),
}
21 changes: 0 additions & 21 deletions benefits/eligibility/templates/eligibility/start--calfresh.html

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions benefits/eligibility/templates/eligibility/start--medicare.html

This file was deleted.

This file was deleted.

This file was deleted.

21 changes: 0 additions & 21 deletions benefits/eligibility/templates/eligibility/start--senior.html

This file was deleted.

21 changes: 0 additions & 21 deletions benefits/eligibility/templates/eligibility/start--veteran.html

This file was deleted.

20 changes: 14 additions & 6 deletions benefits/eligibility/templates/eligibility/start.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,34 @@
{{ block.super | add:" eligibility-start" }}
{% endblock classes %}

{% block page-title %}
{% translate page_title %}
{% endblock page-title %}

{% block nav-buttons %}
{% url routes.ELIGIBILITY_INDEX as url %}
{% include "core/includes/button--previous-page.html" with url=url %}
{% endblock nav-buttons %}

{% block headline %}
<h1>
{% block headline-text %}
{% endblock headline-text %}
</h1>
<h1>{{ headline_text }}</h1>
{% endblock headline %}

{% block inner-content %}
<p class="py-4">{% translate "You will need a few items to continue:" %}</p>
<ul class="d-flex flex-column gap-4 list-unstyled ps-0 mb-0">
{% block eligibility-item %}
{% endblock eligibility-item %}
{% include eligibility_item_template %}
{% include "eligibility/includes/eligibility-item--contactless-card--start.html" %}
</ul>
{% endblock inner-content %}

{% block call-to-action-button %}
{% translate call_to_action_button.text as button_text %}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is something I was wondering about when working on #2753:

Since we already use the gettext() helper (aliased as _()) setting up the context, to mark the string as translatable -- do we also need to translate the string in the template with this {% translate %} (or {% blocktranslate %} tag?

I think the answer, from #2753 anyway, seems to be: no. You can just render the variable directly in the template and it will be translated as necessary. E.g. this template change: https://github.com/cal-itp/benefits/pull/2753/files#diff-70ed6fd5fa2321e83db0d63df8312f680cd40428de4ada50dab22e93f55db7c7R109

Translation still works image

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that we can remove translate from the templates 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking to merge this PR as-is, and a follow-up PR can remove the unnecessary translates in the templates

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Created #2760 as that follow-up ticket

<a href="{% url call_to_action_button.route %}"
class="btn btn-lg btn-primary{% if call_to_action_button.extra_classes %} {{ call_to_action_button.extra_classes }}{% endif %}">
{{ button_text }}
{% if call_to_action_button.fallback_text %}
<span class="fallback-text white-logo">{{ call_to_action_button.fallback_text }}</span>
{% endif %}
</a>
{% endblock call-to-action-button %}
2 changes: 1 addition & 1 deletion benefits/eligibility/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def start(request):

flow = session.flow(request)

return TemplateResponse(request, flow.eligibility_start_template)
return TemplateResponse(request, "eligibility/start.html", flow.eligibility_start_context)


@decorator_from_middleware(AgencySessionRequired)
Expand Down
Loading
Loading