Skip to content

Commit 0e1d3bc

Browse files
committed
Fixes #113, support 'A1B 2C3' formatted postal codes to FSA
1 parent 15b0de5 commit 0e1d3bc

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

backend/api-lambda/index.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
from geolocator import Geolocator
23
from params_manager import *
34
from model_manager import *
@@ -52,7 +53,15 @@ def handler(event, context):
5253
response = {"statusCode": 200, "body": '{"message_en": "Mandatory \'/?q= or table=\' parameter not provided", "message_fr": "Paramètre obligatoire \'/?q= or table=\' non fourni"}'}
5354
return response
5455

55-
q = params_full_list.get("q")
56+
postal_code = extract_postal_prefix(params_full_list.get("q"))
57+
58+
if(postal_code and key_in_params("locate", params_full_list)):
59+
#print("Postal code detected, using forward sortation area instead")
60+
q = extract_postal_prefix(params_full_list.get("q"))
61+
params_full_list.update({"q": q}) #need to update this variable as only q and lang are used for caching
62+
else:
63+
q = params_full_list.get("q")
64+
5665
keys = params_full_list.pop("keys")
5766
lang = params_full_list.get("lang")
5867
q_lang = q + lang # compound key for cache results
@@ -107,7 +116,7 @@ def handler(event, context):
107116
if any(table_update[table_name]):
108117
print(table_name, ' table updates:', table_update[table_name])
109118
geolocator.write_table(table_name, tables)
110-
119+
111120
response = {
112121
"statusCode": 200,
113122
"headers": {
@@ -169,3 +178,23 @@ def q_alphanumeric(q):
169178
return True
170179

171180
return False
181+
182+
def extract_postal_prefix(postal_code):
183+
"""
184+
Regular expression to match postal code with a space in between
185+
"""
186+
if re.fullmatch(r'[A-Za-z]\d[A-Za-z][\s\+]?(\d[A-Za-z]\d)', postal_code):
187+
return postal_code[:3]
188+
return None
189+
190+
def key_in_params(key, params_full_list):
191+
"""
192+
Check if key is in keys parameter
193+
"""
194+
keys = params_full_list.get("keys", [])
195+
if isinstance(keys, str):
196+
keys = keys.split(",")
197+
if key in keys:
198+
return True
199+
else:
200+
return False

0 commit comments

Comments
 (0)