From 38e50c907f12aed3bbf54b52669adb824b5ba862 Mon Sep 17 00:00:00 2001 From: Raphael Odini Date: Thu, 12 Sep 2024 15:28:08 +0200 Subject: [PATCH] fix(GDPR): fix Picard import script after testing in prod. ref #430 --- scripts/gdpr/create_prices_from_gdpr_csv.py | 13 ++++++-- scripts/gdpr/utils.py | 35 ++++++++++++++------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/scripts/gdpr/create_prices_from_gdpr_csv.py b/scripts/gdpr/create_prices_from_gdpr_csv.py index 5a63abd6..aa7ccf30 100644 --- a/scripts/gdpr/create_prices_from_gdpr_csv.py +++ b/scripts/gdpr/create_prices_from_gdpr_csv.py @@ -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 @@ -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__": diff --git a/scripts/gdpr/utils.py b/scripts/gdpr/utils.py index 9c9a08d9..ffcce0fa 100644 --- a/scripts/gdpr/utils.py +++ b/scripts/gdpr/utils.py @@ -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( @@ -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}, @@ -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"] @@ -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[ @@ -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