import csv import os import sys import requests FILENAME = os.environ.get('FILENAME', 'physical_pricing.csv') OWS_PRICING_URL = os.environ.get('OWS_PRICING_URL', 'https://qa-ows-pricing.theorchard.io') def get_pricing_data(): path = os.path.dirname(os.path.realpath(__file__)) + \ '/data/' + FILENAME pricing_rows = [] with open(path, 'rt') as csvfile: reader = csv.reader(csvfile, delimiter=',', quotechar='|') for pricing_row in reader: pricing_rows.append(pricing_row) return pricing_rows[1:] def get_orchard_pricing_tiers(): url = '{0}/pricing-family/4/orchard-pricing-tier'.format(OWS_PRICING_URL) pricing_response = requests.get(url) pricing_result = pricing_response.json() return pricing_result['items'] def ingest_pricing_row(pricing_row, pricing_tiers): product_id = pricing_row[0] orchard_pricing_tier_name = pricing_row[1] stores = [ { 'row_number': 2, 'store_id': 738 }, { 'row_number': 3, 'store_id': 696 }, { 'row_number': 4, 'store_id': 739 }, { 'row_number': 5, 'store_id': 737 }, { 'row_number': 6, 'store_id': 740 }, ] print('About to ingest pricing_row for product ID {0}'.format(product_id)) print('Orchard Pricing Tier: {0}'.format(orchard_pricing_tier_name)) print('Going to look for tier with name: ' + orchard_pricing_tier_name) try: pricing_tier = next(pt for pt in pricing_tiers if pt['name'].lower() == orchard_pricing_tier_name.lower()) except StopIteration: print('Price tier not found') return delete_url = '{0}/legacy/product/{1}/unmigrate'.format(OWS_PRICING_URL, product_id) print('Going to delete pricing information using URL: ' + delete_url) try: delete_response = requests.delete(delete_url) except Exception: print("Error calling ows-pricing to delete pricing for product ID: {0}, error: {1}".format(product_id, sys.exc_info()[0])) print('response from delete pricing: ') print(delete_response) set_tier_url = '{0}/product/{1}/pricing-family/4/orchard_pricing_tier'.format(OWS_PRICING_URL, product_id) print('Going to set orchard tier on URL: ' + set_tier_url) print(pricing_tier) data = {'orchard_pricing_tier_id': pricing_tier['orchard_pricing_tier_id']} print('about to send data:') print(data) try: set_tier_response = requests.put(set_tier_url, json = data) except Exception: print("Error calling ows-pricing to set orchard pricing tier error:", sys.exc_info()[0]) print('response from set tier: ') print(set_tier_response) sort_order = 1 overrides_to_create = [] for store in stores: if pricing_row[store['row_number']] and pricing_row[store['row_number']] != '': store_id = store['store_id'] print('going to set up an override for store_id {0} to have price_code: {1}'.format(store_id, pricing_row[store['row_number']])) override = { 'sort_order': sort_order, 'price_code': pricing_row[store['row_number']], 'product_id': product_id, 'applies_worldwide': True, 'activated': True, 'stores': [store_id], 'pricing_family_id': 4 } sort_order = sort_order + 1 overrides_to_create.append(override) if overrides_to_create: create_override_url = '{0}/product/{1}/override/bulk'.format(OWS_PRICING_URL, product_id) data = { 'items': overrides_to_create } try: create_override_response = requests.post(create_override_url, json = data) except Exception: print("Error calling ows-pricing to create product pricing override, error:", sys.exc_info()[0]) print('response from create override: ') print(create_override_response) try: print('About to read pricing data from {0}'.format(FILENAME)) pricing_rows = get_pricing_data() print('Fetched {0} rows of pricing data to migrate'.format(len(pricing_rows))) pricing_tiers = get_orchard_pricing_tiers() print('Fetched {0} orchard pricing tiers to use'.format(len(pricing_tiers))) for pricing_row in pricing_rows: ingest_pricing_row(pricing_row, pricing_tiers) print('Done ingesting {0} pricing rows'.format(len(pricing_rows))) sys.exit() except Exception as e: sys.exit(e)