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: verifier radio template #1489

Merged
merged 7 commits into from
Jul 7, 2023
5 changes: 2 additions & 3 deletions benefits/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2 on 2023-06-29 18:48
# Generated by Django 4.2 on 2023-07-06 23:16

from django.db import migrations, models
import django.db.models.deletion
Expand Down Expand Up @@ -45,8 +45,7 @@ class Migration(migrations.Migration):
("jwe_cek_enc", models.TextField(null=True)),
("jwe_encryption_alg", models.TextField(null=True)),
("jws_signing_alg", models.TextField(null=True)),
("selection_label", models.TextField()),
("selection_label_description", models.TextField(null=True)),
("selection_label_template", models.TextField()),
("start_title", models.TextField()),
("start_headline", models.TextField()),
("start_item_heading", models.TextField()),
Expand Down
12 changes: 4 additions & 8 deletions benefits/core/migrations/0002_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ def load_data(app, *args, **kwargs):
],
eligibility_type=mst_senior_type,
auth_provider=senior_auth_provider,
selection_label=_("eligibility.pages.index.login_gov.label"),
selection_label_description=_("eligibility.pages.index.login_gov.description"),
selection_label_template="core/includes/selection_label__login_gov.html",
start_title=_("eligibility.pages.start.login_gov.title"),
start_headline=_("eligibility.pages.start.login_gov.headline"),
start_item_heading=_("eligibility.pages.start.login_gov.start_item.heading"),
Expand All @@ -187,8 +186,7 @@ def load_data(app, *args, **kwargs):
],
eligibility_type=mst_veteran_type,
auth_provider=veteran_auth_provider,
selection_label=_("eligibility.pages.index.veteran.label"),
selection_label_description=_("eligibility.pages.index.veteran.description"),
selection_label_template="core/includes/selection_label__mst_veteran.html",
start_title=_("eligibility.pages.start.veteran.title"),
start_headline=_("eligibility.pages.start.veteran.headline"),
start_item_heading=_("eligibility.pages.start.veteran.start_item.heading"),
Expand All @@ -212,8 +210,7 @@ def load_data(app, *args, **kwargs):
jwe_encryption_alg=os.environ.get("COURTESY_CARD_VERIFIER_JWE_ENCRYPTION_ALG", "RSA-OAEP"),
jws_signing_alg=os.environ.get("COURTESY_CARD_VERIFIER_JWS_SIGNING_ALG", "RS256"),
auth_provider=None,
selection_label=_("eligibility.pages.index.mst_cc.label"),
selection_label_description=_("eligibility.pages.index.mst_cc.description"),
selection_label_template="core/includes/selection_label__mst_courtesy_card.html",
start_title=_("eligibility.pages.start.mst_cc.title"),
start_headline=_("eligibility.pages.start.mst_cc.headline"),
start_item_heading=_("eligibility.pages.start.mst_cc.start_item.heading"),
Expand Down Expand Up @@ -250,8 +247,7 @@ def load_data(app, *args, **kwargs):
],
eligibility_type=sacrt_senior_type,
auth_provider=senior_auth_provider,
selection_label=_("eligibility.pages.index.login_gov.label"),
selection_label_description=_("eligibility.pages.index.login_gov.description"),
selection_label_template="core/includes/selection_label__login_gov.html",
start_title=_("eligibility.pages.start.login_gov.title"),
start_headline=_("eligibility.pages.start.login_gov.headline"),
start_item_heading=_("eligibility.pages.start.login_gov.start_item.heading"),
Expand Down
3 changes: 1 addition & 2 deletions benefits/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ class EligibilityVerifier(models.Model):
# The JWS-compatible signing algorithm
jws_signing_alg = models.TextField(null=True)
auth_provider = models.ForeignKey(AuthProvider, on_delete=models.PROTECT, null=True)
selection_label = models.TextField()
selection_label_description = models.TextField(null=True)
selection_label_template = models.TextField()
start_title = models.TextField()
start_headline = models.TextField()
start_item_heading = models.TextField()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "core/includes/verifier_radio_select_label.html" %}
{% load i18n %}

{% block label %}
{% translate "eligibility.pages.index.login_gov.label" %}
{% endblock label %}

{% block description %}
{% translate "eligibility.pages.index.login_gov.description" %}
{% endblock description %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "core/includes/verifier_radio_select_label.html" %}
{% load i18n %}

{% block label %}
{% translate "eligibility.pages.index.mst_cc.label" %}
{% endblock label %}

{% block description %}
{% translate "eligibility.pages.index.mst_cc.description" %}
{% endblock description %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "core/includes/verifier_radio_select_label.html" %}
{% load i18n %}

{% block label %}
{% translate "eligibility.pages.index.veteran.label" %}
{% endblock label %}

{% block description %}
{% translate "eligibility.pages.index.veteran.description" %}
{% endblock description %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<span class="d-block h3">
{% block label %}
{% endblock label %}
</span>
<span class="d-block pt-1">
{% block description %}
{% endblock description %}
</span>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

{% if widget.wrap_label %}
<label{% if widget.attrs.id %} for="{{ widget.attrs.id }}" class="radio-label"{% endif %}>
<span class="d-block h3">{{ widget.label }}</span>
{% if widget.description %}<span class="d-block pt-1">{{ widget.description }}</span>{% endif %}
{% include widget.selection_label_template %}
</label>
{% endif %}
10 changes: 5 additions & 5 deletions benefits/core/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ class VerifierRadioSelect(widgets.RadioSelect):
template_name = "core/widgets/verifier_radio_select.html"
option_template_name = "core/widgets/verifier_radio_select_option.html"

def __init__(self, choice_descriptions=(), *args, **kwargs):
def __init__(self, selection_label_templates=(), *args, **kwargs):
super().__init__(*args, **kwargs)
self.choice_descriptions = list(choice_descriptions)
self.selection_label_templates = list(selection_label_templates)

def __deepcopy__(self, memo):
obj = super().__deepcopy__(memo)
obj.choice_descriptions = copy.copy(self.choice_descriptions)
obj.selection_label_templates = copy.copy(self.selection_label_templates)
return obj

def create_option(self, name, value, label, selected, index, subindex, attrs):
option = super().create_option(name, value, label, selected, index, subindex, attrs)
# this implementation does not support groups from ChoiceWidget.optgroups
if value in self.choice_descriptions:
option.update({"description": self.choice_descriptions[value]})
if value in self.selection_label_templates:
option.update({"selection_label_template": self.selection_label_templates[value]})

return option
9 changes: 4 additions & 5 deletions benefits/eligibility/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import logging

from django import forms
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _

from benefits.core import models, recaptcha, widgets
Expand All @@ -29,10 +28,10 @@ def __init__(self, agency: models.TransitAgency, *args, **kwargs):
verifiers = agency.eligibility_verifiers.all()

self.classes = "offset-lg-1 col-lg-9"
self.fields["verifier"].choices = [(v.id, _(v.selection_label)) for v in verifiers]
self.fields["verifier"].widget.choice_descriptions = {
v.id: format_html(_(v.selection_label_description)) for v in verifiers if v.selection_label_description
}
# second element is not used since we render the whole label using selection_label_template,
# therefore set to None
self.fields["verifier"].choices = [(v.id, None) for v in verifiers]
self.fields["verifier"].widget.selection_label_templates = {v.id: v.selection_label_template for v in verifiers}

def clean(self):
if not recaptcha.verify(self.data):
Expand Down
2 changes: 1 addition & 1 deletion tests/pytest/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def model_EligibilityVerifier(model_PemData, model_EligibilityType):
api_auth_key="secret-key",
eligibility_type=model_EligibilityType,
public_key=model_PemData,
selection_label="Select",
selection_label_template="core/includes/verifier_radio_select_label.html",
start_title="Information",
start_headline="Start",
start_item_heading="Start Item",
Expand Down