Refactor the Rattail <-> CORE product importers

this should allow for more seamless "dual authority" mode
This commit is contained in:
Lance Edgar 2020-08-20 20:00:13 -05:00
parent de628b387a
commit d3e2619944
2 changed files with 83 additions and 12 deletions
rattail_corepos/corepos/importing

View file

@ -286,13 +286,18 @@ class ProductImporter(FromRattail, corepos_importing.model.ProductImporter):
'scale',
]
def query(self):
query = super(ProductImporter, self).query()
return query.filter(model.Product.item_id != None)
def normalize_host_object(self, product):
upc = product.item_id
if not upc and product.upc:
upc = str(product.upc)[:-1]
if not upc:
log.warning("skipping product %s with unknown upc: %s",
product.uuid, product)
return
return {
'upc': product.item_id,
'_product': product,
'upc': upc,
'brand': product.brand.name if product.brand else '',
'description': product.description or '',
'size': product.size or '',
@ -301,3 +306,25 @@ class ProductImporter(FromRattail, corepos_importing.model.ProductImporter):
'foodstamp': '1' if product.food_stampable else '0',
'scale': '1' if product.weighed else '0',
}
def create_object(self, key, data):
# must be sure not to pass the original Product instance, or else the
# API call will try to serialize and submit it
product = data.pop('_product')
corepos_product = super(ProductImporter, self).create_object(key, data)
if corepos_product:
# update our Rattail Product with the CORE ID
product.corepos_id = int(corepos_product['id'])
return corepos_product
def update_object(self, corepos_product, data, local_data=None):
# must be sure not to pass the original Product instance, or else the
# API call will try to serialize and submit it
product = data.pop('_product', None)
corepos_product = super(ProductImporter, self).update_object(corepos_product, data, local_data)
return corepos_product