Skip to content

Commit

Permalink
fix(GDPR): fix Picard import script after testing in prod. ref #430
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Sep 12, 2024
1 parent 955cecc commit 38e50c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
13 changes: 10 additions & 3 deletions scripts/gdpr/create_prices_from_gdpr_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def gdpr_source_field_cleanup_rules(gdpr_source, op_field, gdpr_field_value):
if op_field == "product_code":
if len(gdpr_field_value) == 4:
gdpr_field_value = f"0{gdpr_field_value}"
elif op_field == "date":
gdpr_field_value = datetime.datetime.strptime(
gdpr_field_value, "%d/%m/%Y"
).strftime("%Y-%m-%d")

return gdpr_field_value

Expand Down Expand Up @@ -226,9 +230,12 @@ def map_gdpr_price_list_to_open_prices(gdpr_price_list, gdpr_source="", extra_da

def create_price(price):
headers = {"Authorization": f"Bearer {OPEN_PRICES_TOKEN}"}
requests.post(OPEN_PRICES_CREATE_PRICE_ENDPOINT, json=price, headers=headers)
# if response.status_code == 201:
# print("Price created !")
response = requests.post(
OPEN_PRICES_CREATE_PRICE_ENDPOINT, json=price, headers=headers
)
if not response.status_code == 201:
print(response.json())
print(price)


if __name__ == "__main__":
Expand Down
35 changes: 23 additions & 12 deletions scripts/gdpr/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def get_picard_product_from_subcode(op_price_dict):
# the Picard product_code is incomplete
# use Search-a-licious API to get the full product code
# if needed, prompt the user to select the correct one
passes_test = True
full_product_code = None

print(
Expand All @@ -17,14 +16,14 @@ def get_picard_product_from_subcode(op_price_dict):
op_price_dict["product_name"],
op_price_dict["price"],
)
for q_index, q_params in enumerate(
[
f"code:{PICARD_GS1_PREFIX}?{op_price_dict['product_code']}? brands:picard",
f"code:{PICARD_GS1_PREFIX}?{op_price_dict['product_code']}?",
f"code:*{op_price_dict['product_code']}? brands:picard",
f"code:*{op_price_dict['product_code']}?&page_size=50",
]
):

STEPS = [
f"code:{PICARD_GS1_PREFIX}?{op_price_dict['product_code']}? brands:picard",
f"code:{PICARD_GS1_PREFIX}?{op_price_dict['product_code']}?",
f"code:*{op_price_dict['product_code']}? brands:picard",
f"code:*{op_price_dict['product_code']}?&page_size=50",
]
for q_index, q_params in enumerate(STEPS):
response = requests.get(
OFF_SEARCHLICIOUS_API_ENDPOINT,
params={"q": q_params},
Expand All @@ -33,10 +32,12 @@ def get_picard_product_from_subcode(op_price_dict):
if response.status_code == 200:
response_product_count = response.json()["count"]
print("Products found:", response_product_count)
# loop until at least 1 product is returned
if response_product_count:
# confidence strong enough: take the first product
if (q_index < 2) and (response_product_count == 1):
full_product_code = response.json()["hits"][0]["code"]
print("Chosen product code:", full_product_code)
else:
# multiple results: prompt the user to select
response_product_list = response.json()["hits"]
Expand All @@ -50,7 +51,7 @@ def get_picard_product_from_subcode(op_price_dict):
response_product.get("stores", ""),
)
user_choice_number_str = input(
"Which product ? Type 0 to skip. Or provide the correct code. "
"Which product ? Type 0 to skip. Or provide the correct code: "
)
if len(user_choice_number_str) == 1:
full_product_code = response_product_list[
Expand All @@ -62,7 +63,17 @@ def get_picard_product_from_subcode(op_price_dict):
print("Chosen product code:", full_product_code)
else:
print("Product not found...")
passes_test = False
break
else:
if q_index == len(STEPS) - 1:
# Last chance: prompt the user to type a EAN
user_choice_number_str = input(
"Which product ? Type 0 to skip. Or provide the correct code: "
)
if len(user_choice_number_str) == 13:
full_product_code = user_choice_number_str
print("Chosen product code:", full_product_code)
else:
print("Product not found...")

return passes_test, full_product_code
return full_product_code

0 comments on commit 38e50c9

Please sign in to comment.