Changeset - da55a07982cb
[Not reviewed]
0 1 0
Lance Edgar - 4 years ago 2021-01-27 09:21:02
lance@edbob.org
Add support for importing `Product.case_size` from CORE API
1 file changed with 32 insertions and 0 deletions:
0 comments (0 inline, 0 general) First comment
rattail_corepos/importing/corepos/api.py
Show inline comments
 
@@ -454,6 +454,7 @@ class ProductImporter(FromCOREPOSAPI, corepos_importing.model.ProductImporter):
 
        'food_stampable',
 
        # 'tax1',
 
        # 'tax2',
 
        'case_size',
 
    ]
 

	
 
    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',
 
                                              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):
 
        return self.api.get_products()
 

	
 
@@ -501,6 +510,12 @@ class ProductImporter(FromCOREPOSAPI, corepos_importing.model.ProductImporter):
 
            return product.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):
 
        try:
 
            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'],
 
            })
 

	
 
        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
 

	
 

	
0 comments (0 inline, 0 general) First comment
You need to be logged in to comment. Login now