[addresses] adding new config for postal codes around the world. Allows appending the ISO alpha-2 country code to the beginning of the postcode as in e.g. SI-1000 (only used if the postcode begins with a digit). This system was used for postal codes in continental Europe as a recommendation from the CEPT. Now 7 member states still use it, so in those countries add the country-code with higher probability. The config also contains the license plate codes for countries where e.g. L-1234 might be used instead of LU-1234. Allows configuring in which countries postcodes should be validated using Google's per-country validation regexes (and the ability to override with a custom regex), and in which countries other admin component names should be stripped.
This commit is contained in:
33
scripts/geodata/postal_codes/validation.py
Normal file
33
scripts/geodata/postal_codes/validation.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import re
|
||||
from geodata.i18n.google import google_i18n
|
||||
from geodata.postal_codes.config import postal_codes_config
|
||||
|
||||
|
||||
class PostcodeRegexes(object):
|
||||
def __init__(self):
|
||||
self.responses = {}
|
||||
self.postcode_regexes = {}
|
||||
|
||||
def get(self, country_code):
|
||||
ret = self.postcode_regexes.get(country_code.lower())
|
||||
if ret is None:
|
||||
|
||||
override_regex = postal_codes_config.get_property('override_regex', country=country_code)
|
||||
if override_regex:
|
||||
ret = re.compile(override_regex, re.I)
|
||||
self.postcode_regexes[country_code.lower()] = ret
|
||||
return ret
|
||||
|
||||
response = google_i18n.get(country_code)
|
||||
if response:
|
||||
postcode_expression = response.get('zip')
|
||||
if not postcode_expression:
|
||||
self.postcode_regexes[country_code.lower()] = None
|
||||
return None
|
||||
ret = re.compile(postcode_expression, re.I)
|
||||
self.postcode_regexes[country_code.lower()] = ret
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
postcode_regexes = PostcodeRegexes()
|
||||
Reference in New Issue
Block a user