Add support for importing Product.case_size from CORE API

This commit is contained in:
Lance Edgar 2021-01-27 09:21:02 -06:00
parent 59abc4995e
commit da55a07982

View file

@ -454,6 +454,7 @@ class ProductImporter(FromCOREPOSAPI, corepos_importing.model.ProductImporter):
'food_stampable', 'food_stampable',
# 'tax1', # 'tax1',
# 'tax2', # 'tax2',
'case_size',
] ]
def setup(self): def setup(self):
@ -467,6 +468,14 @@ class ProductImporter(FromCOREPOSAPI, corepos_importing.model.ProductImporter):
self.core_existing = self.cache_model(model.Product, key='corepos_id', self.core_existing = self.cache_model(model.Product, key='corepos_id',
query=query) query=query)
self.vendor_items_by_upc = {}
def cache(item, i):
self.vendor_items_by_upc.setdefault(item['upc'], []).append(item)
self.progress_loop(cache, self.api.get_vendor_items(),
message="Caching CORE Vendor Items")
def get_host_objects(self): def get_host_objects(self):
return self.api.get_products() return self.api.get_products()
@ -501,6 +510,12 @@ class ProductImporter(FromCOREPOSAPI, corepos_importing.model.ProductImporter):
return product.uuid return product.uuid
return get_uuid() return get_uuid()
def get_vendor_items(self, api_product):
if hasattr(self, 'vendor_items_by_upc'):
return self.vendor_items_by_upc.get(api_product['upc'])
return self.api.get_vendor_items(upc=api_product['upc'])
def normalize_host_object(self, product): def normalize_host_object(self, product):
try: try:
upc = GPC(product['upc'], calc_check_digit='upc') upc = GPC(product['upc'], calc_check_digit='upc')
@ -552,6 +567,23 @@ class ProductImporter(FromCOREPOSAPI, corepos_importing.model.ProductImporter):
'uom_abbreviation': size_info['uom_abbrev'], 'uom_abbreviation': size_info['uom_abbrev'],
}) })
if 'case_size' in self.fields:
case_size = None
items = self.get_vendor_items(product)
if items:
# here we sort by `modified DESC` to get the "deterministic
# pseudo-default" vendorItems record
def sortkey(item):
dt = datetime.datetime.strptime(item['modified'], '%Y-%m-%d %H:%M:%S')
return dt
items = sorted(items, key=sortkey, reverse=True)
item = items[0]
case_size = decimal.Decimal(item['units'])
data['case_size'] = case_size
return data return data