Skip to content

Commit

Permalink
Accept commas on num_votes, but clean them before save
Browse files Browse the repository at this point in the history
  • Loading branch information
VirginiaDooley committed May 3, 2024
1 parent 4c8cda8 commit bf921d0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
8 changes: 6 additions & 2 deletions ynr/apps/uk_results/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __init__(self, ballot, *args, **kwargs):
except CandidateResult.DoesNotExist:
initial = {}

fields[name] = forms.IntegerField(
fields[name] = forms.CharField(
label=membership.name_and_party,
initial=initial.get("num_ballots"),
required=True,
Expand Down Expand Up @@ -117,9 +117,13 @@ def clean(self):
"""
cleaned_data = super().clean()

for field_name, value in cleaned_data.items():
if field_name.startswith("memberships_"):
cleaned_data[field_name] = int(value.replace(",", ""))

if len(self._tied_vote_winners) > self.ballot.winner_count:
raise forms.ValidationError(
"Cant have more coin toss winners than seats up!"
"Can't have more coin toss winners than seats up!"
)

return cleaned_data
Expand Down
14 changes: 13 additions & 1 deletion ynr/apps/uk_results/tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def build_fieldname(self, obj):
def test_clean(self):
"""
Test that you cant have more tied vote winners than number of winners
for the ballot
for the ballot and commas are accepted but removed in num_votes
"""
form = ResultSetForm(ballot=self.ballot)
self.assertEqual(self.ballot.winner_count, 1)
Expand All @@ -57,6 +57,18 @@ def test_clean(self):
str(e), "Cant have more coin toss winners than seats up!"
)

def test_commas_are_cleaned(self):
form = ResultSetForm(ballot=self.ballot)
cleaned_data = {}
cleaned_data[f"memberships_{self.candidacies[0].pk}"] = "1,000"

form.cleaned_data = cleaned_data
form.clean()

self.assertEqual(
form.cleaned_data, {f"memberships_{self.candidacies[0].pk}": 1000}
)

def test_tied_vote_winners(self):
form = ResultSetForm(ballot=self.ballot)
cleaned_data = {}
Expand Down
2 changes: 1 addition & 1 deletion ynr/apps/uk_results/tests/test_smoke_test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_form_view_creates_result(self):
resp = self.app.get(url, user=self.user_who_can_record_results)
self.assertEqual(resp.status_code, 200)
self.assertContains(resp, 'inputmode="numeric"')
self.assertContains(resp, r'pattern="[0-9\s\.]*"')
self.assertContains(resp, r'pattern="[0-9\s\.,]*"')
form = resp.forms[1]
form["memberships_13"] = 1000
form["memberships_14"] = 2000
Expand Down
2 changes: 1 addition & 1 deletion ynr/apps/utils/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def build_attrs(self, base_attrs, extra_attrs=None):
attrs.update(
{
"inputmode": "numeric",
"pattern": r"[0-9\s\.]*",
"pattern": r"[0-9\s\.,]*",
"oninvalid": "this.setCustomValidity('Enter a number')",
}
)
Expand Down

0 comments on commit bf921d0

Please sign in to comment.