Skip to content

Commit

Permalink
Use postcode utils module from PyPI.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Jun 29, 2016
1 parent b20ee9f commit 5b909cf
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 36 deletions.
41 changes: 5 additions & 36 deletions mapit_gb/countries.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from functools import partial
import re

from django.core.urlresolvers import reverse, NoReverseMatch
from django.http import HttpResponseRedirect
from ukpostcodeutils import validation

from mapit.shortcuts import get_object_or_404

Expand Down Expand Up @@ -65,43 +65,12 @@ def is_special_postcode(pc):
return pc in SPECIAL_POSTCODES


PARTS = {
'fst': 'ABCDEFGHIJKLMNOPRSTUWYZ',
'sec': 'ABCDEFGHKLMNOPQRSTUVWXY',
'thd': 'ABCDEFGHJKPSTUW',
'fth': 'ABEHMNPRVWXY',
'inward': 'ABDEFGHJLNPQRSTUWXYZ',
}
def is_valid_postcode(pc):
return validation.is_valid_postcode(pc, SPECIAL_POSTCODES + ('ZZ99ZZ', 'ZZ99ZY'))

FULL_MATCH_REGEX = re.compile('|'.join([r.format(**PARTS) for r in (
'^[{fst}][1-9]\d[{inward}][{inward}]$',
'^[{fst}][1-9]\d\d[{inward}][{inward}]$',
'^[{fst}][{sec}]\d\d[{inward}][{inward}]$',
'^[{fst}][{sec}][1-9]\d\d[{inward}][{inward}]$',
'^[{fst}][1-9][{thd}]\d[{inward}][{inward}]$',
'^[{fst}][{sec}][1-9][{fth}]\d[{inward}][{inward}]$',
)]))

PARTIAL_MATCH_REGEX = re.compile('|'.join([r.format(**PARTS) for r in (
'^[{fst}][1-9]$',
'^[{fst}][1-9]\d$',
'^[{fst}][{sec}]\d$',
'^[{fst}][{sec}][1-9]\d$',
'^[{fst}][1-9][{thd}]$',
'^[{fst}][{sec}][1-9][{fth}]$',
)]))

del PARTS


def _match_postcode(regex, extra_postcodes, pc):
if pc in extra_postcodes:
return True
return regex.match(pc) is not None


is_valid_postcode = partial(_match_postcode, FULL_MATCH_REGEX, SPECIAL_POSTCODES + ('ZZ99ZZ', 'ZZ99ZY'))
is_valid_partial_postcode = partial(_match_postcode, PARTIAL_MATCH_REGEX, ('ZZ9',))
def is_valid_partial_postcode(pc):
return validation.is_valid_partial_postcode(pc, ('ZZ9',))


def get_postcode_display(pc):
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def read_file(filename):
'psycopg2',
'PyYAML',
'Shapely',
'uk-postcode-utils',
],
classifiers=[
'Development Status :: 5 - Production/Stable',
Expand Down

0 comments on commit 5b909cf

Please sign in to comment.