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

WIP: Refactor means test to always use boolean values #181

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions app/means_test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,4 @@

bp = Blueprint("means_test", __name__)

YES = "1"
NO = "0"


def is_yes(value):
return YES == value


from app.means_test import urls # noqa: E402,F401
28 changes: 27 additions & 1 deletion app/means_test/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from decimal import Decimal, InvalidOperation

from flask import render_template
from flask_babel import lazy_gettext as _
from flask_babel import lazy_gettext as _, LazyString
from wtforms import RadioField
from wtforms.widgets import TextInput
from markupsafe import Markup
from wtforms import Field, IntegerField as BaseIntegerField
Expand All @@ -11,6 +12,31 @@
from app.means_test.validators import CurrencyValidator


class YesNoField(RadioField):
def __init__(self, label: LazyString = None, validators=None, **kwargs):
if "coerce" in kwargs:
raise ValueError("The 'coerce' parameter is not allowed in a YesNoField")
if "choices" in kwargs:
raise ValueError(
"Choices in a YesNoField are fixed and cannot be overridden"
)
choices = [(True, _("Yes")), (False, _("No"))]
coerce = self._coerce_to_boolean
super(YesNoField, self).__init__(
label=label, validators=validators, choices=choices, coerce=coerce, **kwargs
) # noqa

@staticmethod
def _coerce_to_boolean(value):
# Converts the string value from the HTML request to a boolean
# "Yes" being True, "No" being False
if isinstance(value, bool):
return value
if isinstance(value, str):
return value.lower() in ("true", "yes", "y", "1", "t")
return bool(value)


class MoneyIntervalWidget(TextInput):
def __call__(self, field, *args, **kwargs):
# Pass the current values to the template
Expand Down
42 changes: 14 additions & 28 deletions app/means_test/forms/about_you.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,32 @@
from wtforms.fields import RadioField
from govuk_frontend_wtf.wtforms_widgets import GovTextInput
from wtforms.validators import InputRequired, NumberRange
from app.means_test.validators import ValidateIf
from app.means_test.widgets import MeansTestRadioInput
from flask_babel import lazy_gettext as _
from app.means_test import YES, NO
from app.means_test.forms import BaseMeansTestForm
from app.means_test.fields import IntegerField
from app.means_test.fields import IntegerField, YesNoField


class AboutYouForm(BaseMeansTestForm):
title = _("About you")

template = "means_test/about-you.html"

has_partner = RadioField(
has_partner = YesNoField(
_("Do you have a partner?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
description=_(
"Your husband, wife, civil partner (unless you have permanently separated) or someone you live with as if you're married"
),
validators=[InputRequired(message=_("Tell us whether you have a partner"))],
)

are_you_in_a_dispute = RadioField(
are_you_in_a_dispute = YesNoField(
_("Are you in a dispute with your partner?"),
description=_(
"This means your partner is the opponent in the dispute you need help with, for example a dispute over money or property"
),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
validators=[
ValidateIf("has_partner", YES),
Expand All @@ -39,17 +36,15 @@ class AboutYouForm(BaseMeansTestForm):
],
)

on_benefits = RadioField(
on_benefits = YesNoField(
_("Do you receive any benefits (including Child Benefit)?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
description=_("Being on some benefits can help you qualify for legal aid"),
validators=[InputRequired(message=_("Tell us whether you receive benefits"))],
)

have_children = RadioField(
have_children = YesNoField(
_("Do you have any children aged 15 or under?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
description=_("Don't include any children who don't live with you"),
validators=[
Expand All @@ -71,9 +66,8 @@ class AboutYouForm(BaseMeansTestForm):
],
)

have_dependents = RadioField(
have_dependents = YesNoField(
_("Do you have any dependants aged 16 or over?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
description=_(
"People who you live with and support financially. This could be a young person for whom you get Child Benefit"
Expand All @@ -95,63 +89,57 @@ class AboutYouForm(BaseMeansTestForm):
],
)

own_property = RadioField(
own_property = YesNoField(
_("Do you own any property?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
description=_("For example, a house, static caravan or flat"),
validators=[InputRequired(message=_("Tell us if you own any properties"))],
)

is_employed = RadioField(
is_employed = YesNoField(
_("Are you employed?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
description=_(
"This means working as an employee - you may be both employed and self-employed"
),
validators=[InputRequired(message=_("Tell us if you are employed"))],
)

partner_is_employed = RadioField(
partner_is_employed = YesNoField(
_("Is your partner employed?"),
description=_(
"This means working as an employee - your partner may be both employed and self-employed"
),
choices=[(YES, _("Yes")), (NO, _("No"))],
validators=[
ValidateIf("are_you_in_a_dispute", NO),
InputRequired(message=_("Tell us whether your partner is employed")),
],
widget=MeansTestRadioInput(),
)

is_self_employed = RadioField(
is_self_employed = YesNoField(
_("Are you self-employed?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
description=_(
"This means working for yourself - you may be both employed and self-employed"
),
validators=[InputRequired(message=_("Tell us if you are self-employed"))],
)

partner_is_self_employed = RadioField(
partner_is_self_employed = YesNoField(
_("Is your partner self-employed?"),
description=_(
"This means working for yourself - your partner may be both employed and self-employed"
),
choices=[(YES, _("Yes")), (NO, _("No"))],
validators=[
ValidateIf("are_you_in_a_dispute", NO),
InputRequired(message=_("Tell us whether your partner is self-employed")),
],
widget=MeansTestRadioInput(),
)

aged_60_or_over = RadioField(
aged_60_or_over = YesNoField(
_("Are you or your partner (if you have one) aged 60 or over?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
validators=[
InputRequired(
Expand All @@ -160,18 +148,16 @@ class AboutYouForm(BaseMeansTestForm):
],
)

have_savings = RadioField(
have_savings = YesNoField(
_("Do you have any savings or investments?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
validators=[
InputRequired(message=_("Tell us whether you have savings or investments"))
],
)

have_valuables = RadioField(
have_valuables = YesNoField(
_("Do you have any valuable items worth over £500 each?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
validators=[
InputRequired(
Expand Down
3 changes: 1 addition & 2 deletions app/means_test/forms/benefits.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
ValidateIf,
ValidateIfType,
)
from app.means_test import YES, NO
from app.means_test import YES

from dataclasses import dataclass, field

Expand Down Expand Up @@ -194,7 +194,6 @@ class AdditionalBenefitsForm(BaseMeansTestForm):
"Allowance"
),
widget=MeansTestRadioInput(),
choices=[(YES, _("Yes")), (NO, _("No"))],
validators=[
InputRequired(message=_("Tell us whether you receive any other benefits"))
],
Expand Down
37 changes: 17 additions & 20 deletions app/means_test/forms/property.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from flask import session
from wtforms import ValidationError
from wtforms.fields import RadioField, FieldList, FormField
from wtforms.fields import FieldList, FormField
from wtforms.fields.core import Label
from govuk_frontend_wtf.wtforms_widgets import GovTextInput
from wtforms.validators import InputRequired, NumberRange
from app.means_test.widgets import MeansTestRadioInput
from flask_babel import lazy_gettext as _
from app.means_test import YES, NO
from app.means_test.forms import BaseMeansTestForm
from app.means_test.fields import MoneyIntervalField, MoneyIntervalWidget, MoneyField
from app.means_test.fields import (
MoneyIntervalField,
MoneyIntervalWidget,
MoneyField,
YesNoField,
)
from app.means_test.validators import MoneyIntervalAmountRequired
from app.means_test.validators import ValidateIf
from app.means_test.money_interval import MoneyInterval, to_amount
Expand All @@ -22,10 +26,10 @@ def val(field):
return form_data.get(field)

def yes(field):
return form_data.get(field) == YES
return form_data.get(field)

def no(field):
return form_data.get(field) == NO
return not form_data.get(field)

self.update(
{
Expand Down Expand Up @@ -84,14 +88,14 @@ def validate_single_main_home(form, field):
properties = form.properties.data
main_home_count = 0
for property_data in properties:
if property_data.get("is_main_home") == "True":
if property_data.get("is_main_home"):
main_home_count += 1

if main_home_count > 1:
raise ValidationError(_("You can only have 1 main property"))


class PartnerRadioField(RadioField):
class PartnerYesNoFieldField(YesNoField):
def __init__(self, label, partner_label, *args, **kwargs):
super().__init__(*args, **kwargs)
self._label = label
Expand All @@ -116,22 +120,20 @@ class PropertyForm(BaseMeansTestForm):
def has_partner(self):
return session.get_eligibility().has_partner

is_main_home = RadioField(
is_main_home = YesNoField(
_("Is this property your main home?"),
choices=[(True, _("Yes")), (False, _("No"))],
widget=MeansTestRadioInput(),
description=_(
"If you’re temporarily living away from the property, select ‘Yes’"
),
validators=[InputRequired(message=_("Tell us whether this is your main home"))],
)

other_shareholders = PartnerRadioField(
other_shareholders = PartnerYesNoFieldField(
partner_label=_(
"Does anyone else (other than you or your partner) own a share of the property?"
),
label=_("Does anyone else own a share of the property?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
description=_(
"Select ‘Yes’ if you share ownership with a friend, relative or ex-partner"
Expand Down Expand Up @@ -182,9 +184,8 @@ def has_partner(self):
],
)

is_rented = RadioField(
is_rented = YesNoField(
_("Do you rent out any part of this property?"),
choices=[(YES, _("Yes")), (NO, _("No"))],
widget=MeansTestRadioInput(),
validators=[
InputRequired(
Expand All @@ -199,7 +200,7 @@ def has_partner(self):
exclude_intervals=["per_4week"],
widget=MoneyIntervalWidget(),
validators=[
ValidateIf("is_rented", YES),
ValidateIf("is_rented", True),
MoneyIntervalAmountRequired(
message=_("Tell us how much rent you receive from this property"),
freq_message=_("Tell us how often you receive this rent"),
Expand All @@ -208,9 +209,8 @@ def has_partner(self):
],
)

in_dispute = RadioField(
in_dispute = YesNoField(
_("Is your share of the property in dispute?"),
choices=[(True, _("Yes")), (False, _("No"))],
widget=MeansTestRadioInput(),
description=_("For example, as part of the financial settlement of a divorce"),
validators=[
Expand All @@ -225,10 +225,7 @@ class MultiplePropertiesForm(BaseMeansTestForm):

@classmethod
def should_show(cls) -> bool:
return (
session.get_eligibility().forms.get("about-you", {}).get("own_property")
== YES
)
return session.get_eligibility().forms.get("about-you", {}).get("own_property")

properties = FieldList(
FormField(PropertyForm), # Each entry is an instance of PropertyForm
Expand Down
6 changes: 4 additions & 2 deletions app/means_test/partner_fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from flask import session
from wtforms.fields import SelectMultipleField, RadioField
from wtforms.fields import SelectMultipleField

from app.means_test.fields import YesNoField


class PartnerMixin(object):
Expand All @@ -18,5 +20,5 @@ class PartnerMultiCheckboxField(PartnerMixin, SelectMultipleField):
pass


class PartnerYesNoField(PartnerMixin, RadioField):
class PartnerYesNoField(PartnerMixin, YesNoField):
pass
Loading
Loading