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

Update MoneyField handling of 0 #112

Merged
merged 5 commits into from
Feb 4, 2025
Merged
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
3 changes: 1 addition & 2 deletions app/means_test/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from app.means_test.forms.benefits import BenefitsForm, AdditionalBenefitsForm
from app.means_test.forms.property import MultiplePropertiesForm
from app.means_test.money_interval import MoneyInterval
from tests.unit_tests.means_test.payload.test_cases import EligibilityData


def update_means_test(payload):
Expand All @@ -29,7 +28,7 @@ def is_eligible(reference):
return response["is_eligible"]


def get_means_test_payload(eligibility_data: EligibilityData) -> dict:
def get_means_test_payload(eligibility_data) -> dict:
about = eligibility_data.forms.get("about-you", {})
savings_form = eligibility_data.forms.get("savings", {})

Expand Down
4 changes: 4 additions & 0 deletions app/means_test/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from wtforms import Field, IntegerField as BaseIntegerField
from app.means_test.money_interval import MoneyInterval
import re
from app.means_test.validators import CurrencyValidator


class MoneyIntervalWidget(TextInput):
Expand Down Expand Up @@ -95,6 +96,7 @@ def process_formdata(self, valuelist):
"""Process the form data from both inputs"""
if valuelist and len(valuelist) == 2:
# Handle the data coming from the form fields named field.id[value] and field.id[interval]
valuelist[0] = CurrencyValidator.clean_input(valuelist[0])
self.data = valuelist

if (
Expand All @@ -108,13 +110,15 @@ def process_formdata(self, valuelist):
self.min_val is not None
and self.data["per_interval_value"] < self.min_val
):
self.field_with_error.add("value")
raise ValueError(
f"Enter a value of more than £{self.min_val / 100:,.2f}"
)
if (
self.max_val is not None
and self.data["per_interval_value"] > self.max_val
):
self.field_with_error.add("value")
raise ValueError(
f"Enter a value of less than £{self.max_val / 100:,.2f}"
)
Expand Down
5 changes: 5 additions & 0 deletions app/means_test/money_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ def amount(self, value):
Decimal first.
"""

# If you have an amount of 0 the interval is irrelevant, therefore default to per_month
# This allows the user to leave frequency as "--Please select--" and enter 0 for the amount
if self.interval is None and value == "0":
self.interval = "per_month"

try:
self["per_interval_value"] = to_amount(value)

Expand Down
3 changes: 3 additions & 0 deletions app/means_test/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def __call__(self, form, field):
field.errors.append(str(e))
field.field_with_error.add("value")

if (amount == "0") and not interval:
return

if (not amount) and (not interval):
message = messages["message"]
field.errors.append(message)
Expand Down
Loading