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

Validate 8-digit Australian tax file numbers properly #284

Merged
merged 1 commit into from
Mar 10, 2017
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
1 change: 1 addition & 0 deletions docs/authors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Authors
* Alex Gaynor
* Alex Hill
* Alex Zhang
* Alexey Kotlyarov
* Alix Martineau
* Alonisser
* Andreas Pelme
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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 @@ -56,6 +56,7 @@ class AUTaxFileNumberFieldValidator(RegexValidator):
Validation for Australian Tax File Numbers.

.. versionadded:: 1.4
.. versionchanged:: 1.5
"""

error_message = _('Enter a valid TFN.')
Expand All @@ -75,7 +76,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 @@ -233,6 +233,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