Skip to content

Commit

Permalink
Validate 8-digit Australian tax file numbers properly (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
koterpillar authored and benkonrath committed Mar 10, 2017
1 parent 76c1a0e commit 2a8a506
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Modifications to existing flavors:
(`gh-277 <https://github.com/django/django-localflavor/pull/277>`_)
- Fixed Dutch NLZipCodeField field not to store empty value as a single space.
(`gh-280 <https://github.com/django/django-localflavor/pull/280>`_)
- Fixed validation for old Australian tax file numbers.
(`gh-284 <https://github.com/django/django-localflavor/pull/284>`_)

Other changes:

Expand Down
8 changes: 7 additions & 1 deletion localflavor/au/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ class AUTaxFileNumberFieldValidator(RegexValidator):
Validation for Australian Tax File Numbers.
.. versionadded:: 1.4
.. versionchanged:: 1.5
"""

error_message = _('Enter a valid TFN.')
Expand All @@ -118,7 +119,12 @@ def _is_valid(self, value):
"""
# 1. Multiply each digit by its weighting factor.
digits = [int(i) for i in list(value)]
weighting_factors = [1, 4, 3, 7, 5, 8, 6, 9, 10]

if len(digits) == 8:
weighting_factors = [10, 7, 8, 4, 6, 3, 5, 1]
else:
weighting_factors = [1, 4, 3, 7, 5, 8, 6, 9, 10]

weighted = [digit * weight for digit, weight in zip(digits, weighting_factors)]

# 2. Sum the resulting values.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_au/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,14 @@ def test_raises_error_for_invalid_tfn(self):
validator = AUTaxFileNumberFieldValidator()
self.assertRaises(ValidationError, lambda: validator(invalid_tfn))

def test_old_tfn(self):
"""Test old, 8-digit TFNs."""
validator = AUTaxFileNumberFieldValidator()
old_valid_tfn = '38593474'
validator(old_valid_tfn)
old_invalid_tfn = '38593475'
self.assertRaises(ValidationError, lambda: validator(old_invalid_tfn))


class AULocalFlavorAUBusinessNumberModelTests(TestCase):

Expand Down

0 comments on commit 2a8a506

Please sign in to comment.