From fb16516ddb5dc373224e60adc3e357aeb9bce9e6 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 24 Jul 2024 15:51:23 +0200 Subject: [PATCH] created function for creating shopify discount codes --- .../groups/shx-order-voucher-codes/script.py | 53 ++-------------- .../utils/__pycache__/utils.cpython-39.pyc | Bin 4036 -> 5456 bytes groupTasks/libs/utils/utils.py | 57 +++++++++++++++++- 3 files changed, 61 insertions(+), 49 deletions(-) diff --git a/groupTasks/groups/shx-order-voucher-codes/script.py b/groupTasks/groups/shx-order-voucher-codes/script.py index 3d0c8a0..c9b9499 100644 --- a/groupTasks/groups/shx-order-voucher-codes/script.py +++ b/groupTasks/groups/shx-order-voucher-codes/script.py @@ -45,7 +45,7 @@ orderId = orderId["value"] if orderId.startswith("#"): orderId = orderId[1:] -discountCode = None +generated_discount_code = None def CreateCrmCustomer(): @@ -117,49 +117,6 @@ def CheckIfCrmCustomerExists(): GetCustomerActivityLinks(customerId=customerId, thirdVoucher=thirdVoucher) -# create shopify discount code -# this function will retry 3 times to create a new discount code if the random generate code already exists -def createShopifyDiscountCode(retries): - # generate random code - characters = string.ascii_uppercase + string.digits - discount_code = "".join(random.choice(characters) for _ in range(12)) - - # load data from secrets - shopify_secrets = secrets["shopify"] - - shopify_url = shopify_secrets["store_url"] - x_shopify_app_access_token = shopify_secrets["x_shopify_app_access_token"] - shopify_discount_code_price_rule = shopify_secrets["discount_price_rules"]["order_voucher_10_percent"] - - response = requests.post( - url=f"{shopify_url}/admin/api/2024-01/price_rules/{shopify_discount_code_price_rule}/discount_codes.json", - headers={ - "X-Shopify-Access-Token": x_shopify_app_access_token, - "Content-Type": "application/json" - }, - json={ - "discount_code": {"code": discount_code} - }) - - retries += 1 - - if retries >= 3: - sys.exit("Error, tried 3 times to create discount code") - - if response.status_code == 422: - print("Failed to create code, response status code 422. Trying to generate new discount code") - createShopifyDiscountCode(retries) - - if response.status_code != 201: - sys.exit(f"Failed to create discount code, status code: {response.status_code}") - - print(f"Created discount code: {discount_code}") - - global discountCode - - discountCode = discount_code - - def CreateCrmActivityLink(customerId, thirdVoucher): print(f"Creating CRM activity link for customer {customerId}") @@ -179,9 +136,11 @@ def CreateCrmActivityLink(customerId, thirdVoucher): print(f"CreateCrmActivityLink req error {response.status_code}") sys.exit(1) - createShopifyDiscountCode(0) + global generated_discount_code - req("10 % Gutschein", f"{secrets['shopify']['public_url']}/discount/{discountCode}?utm_source=order&utm_medium=qrcode&utm_campaign=ordervouchercodes&utm_content=reedemvouchercode") + generated_discount_code = utils.create_shopify_discount_code("order_voucher_10_percent") + + req("10 % Gutschein", f"{secrets['shopify']['public_url']}/discount/{generated_discount_code}?utm_source=order&utm_medium=qrcode&utm_campaign=ordervouchercodes&utm_content=reedemvouchercode") req("5 € Gutschein", f"{productUrl}?utm_source=order&utm_medium=qrcode&utm_campaign=ordervouchercodes&utm_content=qualifyforvouchercode") if thirdVoucher: @@ -276,7 +235,7 @@ def ReplaceHtmlVariables(): file = open("backPage.html", "r") html = file.read() - html = html.replace("{{DISCOUNT_CODE}}", discountCode) + html = html.replace("{{DISCOUNT_CODE}}", generated_discount_code) file.close() file = open("backPage.html", "w") diff --git a/groupTasks/libs/utils/__pycache__/utils.cpython-39.pyc b/groupTasks/libs/utils/__pycache__/utils.cpython-39.pyc index c2bcdfc822976bd06cf543989f701193e40c0761..4d42fd358dffd536a9ea2810ec4514cd2432c0c8 100644 GIT binary patch delta 1700 zcmaJ>PiP!f7=Q2W?9A-{O`89PW_mEVnq6jfGp$?i;#x`VhM^+LM%fWh7gBf7>*$h!w8Hb9)mF$M;w6(m_!@}dWBS`4#~Ga zouD+|{gpnZqX)mRAx5XWkF>}1m4n~X2Bm8Uzw2)?dg%W9>sxhMw!2>MH&>K_!WZ^lq1ul9)+RLQ|(NY*)m~x zn~D2j29E7fN)pY;c1rvZX7A--RK4FJciFx=Gut{AFm{Htjn2(gbQzBfOfV7YXrd+V zz54!0oF|nNT1}~Qcuv_KS?M^H>C3SpH-m<^eyf&|VIa8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 b/groupTasks/libs/utils/utils.py @@ -5,7 +5,9 @@ import shutil import sys import qrcode import json - +import string +import random +import requests def clear_workspace(files): if not files or not isinstance(files, list) or len(files) == 0: @@ -147,4 +149,55 @@ def create_qrcode(qr_code_url, save_path, back_color): def get_secrets(): with open('../../secrets/secrets.json') as content: - return json.load(content) \ No newline at end of file + return json.load(content) + + +# create shopify discount code +# this function will retry 3 times to create a new discount code if the random generate code already exists +def create_shopify_discount_code(shopify_discount_code_price_rule_name): + # load data from secrets + secrets = get_secrets() + shopify_secrets = secrets["shopify"] + shopify_url = shopify_secrets["store_url"] + x_shopify_app_access_token = shopify_secrets["x_shopify_app_access_token"] + shopify_discount_code_price_rule = shopify_secrets["discount_price_rules"][shopify_discount_code_price_rule_name] + + characters = string.ascii_uppercase + string.digits + + # _createShopifyDiscountCode(secrets, characters, shopify_discount_code_price_rule, 0) + + generated_discount_code = None + retries = 0 + + while (generated_discount_code is None or retries >= 3): + # generate random code + discount_code = "".join(random.choice(characters) for _ in range(12)) + + # shopify_discount_code_price_rule = shopify_secrets["discount_price_rules"]["order_voucher_10_percent"] + + response = requests.post( + url=f"{shopify_url}/admin/api/2024-01/price_rules/{shopify_discount_code_price_rule}/discount_codes.json", + headers={ + "X-Shopify-Access-Token": x_shopify_app_access_token, + "Content-Type": "application/json" + }, + json={ + "discount_code": {"code": discount_code} + }) + + if response.status_code == 422: + print("Failed to create code, response status code 422. Trying to generate new discount code") + + if response.status_code != 201: + sys.exit(f"Failed to create discount code, status code: {response.status_code}") + + generated_discount_code = discount_code + + retries += 1 + + if retries >= 3: + sys.exit("Error, tried 3 times to create discount code") + + print(f"Created discount code: {generated_discount_code}. Retries: {retries}") + + return generated_discount_code