postcodes: strip leading country codes

This commit is contained in:
Sarah Hoffmann
2022-05-23 11:01:57 +02:00
parent 28ab2f6048
commit baee6f3de0
2 changed files with 11 additions and 6 deletions

View File

@@ -29,8 +29,9 @@ class _PostcodeMatcher:
raise UsageError("Field 'pattern' required for 'postcode' "
f"for country '{country_code}'")
self.pattern = re.compile(config['pattern'].replace('d', '[0-9]')
.replace('l', '[A-Z]'))
pc_pattern = config['pattern'].replace('d', '[0-9]').replace('l', '[A-Z]')
self.pattern = re.compile(f'(?:{country_code.upper()}[ -]?)?({pc_pattern})')
def normalize(self, postcode):
@@ -39,7 +40,9 @@ class _PostcodeMatcher:
"""
normalized = postcode.strip().upper()
return normalized if self.pattern.fullmatch(normalized) else None
match = self.pattern.fullmatch(normalized)
return match.group(1) if match else None
class _PostcodeSanitizer: