"""Change Detection logic for physical pricing.""" from pricing.constants import pricing_family from pricing.logic import product_store_pricing from pricing.models import ows_product_physical from pricing.models import product_store_effective_price_code STORES = [ { 'store_id': 738, 'write_to_ows_pricing': True, 'write_to_effective_price_code': True }, { 'store_id': 696, 'write_to_ows_pricing': True, 'write_to_effective_price_code': True }, { 'store_id': 739, 'write_to_ows_pricing': True, 'write_to_effective_price_code': True }, { 'store_id': 737, 'write_to_ows_pricing': True, 'write_to_effective_price_code': True }, { 'store_id': 740, 'write_to_ows_pricing': True, 'write_to_effective_price_code': True }, { 'store_id': 1705, 'write_to_ows_pricing': True, 'write_to_effective_price_code': True }, { 'store_id': 1901, 'write_to_ows_pricing': True, 'write_to_effective_price_code': True } ] def write_pricing_changes(product_id, pricing_family_id): """Write physical pricing changes to ows-product-physical. Args: product_id (int): the ID of the product. """ if pricing_family_id != pricing_family.PHYSICAL_PRICING_FAMILY_ID: return for store in STORES: store_id = store['store_id'] item = get_physical_price_item(product_id, store_id) if item: price_code = item['price_code'] else: price_code = 'No Price Assigned' if store['write_to_ows_pricing']: ows_product_physical.add_change_history( product_id, store_id, price_code) if store['write_to_effective_price_code']: data = { 'price_code': price_code } if item and 'store_pricing_tier_name' in item: data['store_pricing_tier_name'] = item[ 'store_pricing_tier_name'] product_store_effective_price_code.\ update_product_store_effective_price_code( product_id, store_id, data) def get_physical_price_item(product_id, store_id): """Get the effective physical price item for a product. Args: product_id (int): the ID of the product. store_id (int): the ID of the store. Returns: dict: the price code item. """ pricing_response = product_store_pricing.get_product_store_pricing( pricing_family.PHYSICAL_PRICING_FAMILY_ID, product_id, store_id) if not pricing_response or not pricing_response.message: return if 'items' not in pricing_response.message: return if len(pricing_response.message['items']) != 1: return return pricing_response.message['items'][0]