Improve logic some more, for importing ProductCost.preference

This commit is contained in:
Lance Edgar 2016-12-15 18:42:40 -06:00
parent b1d761c404
commit 6fc055609d
2 changed files with 22 additions and 18 deletions

View file

@ -31,7 +31,7 @@ import logging
from rattail.db import cache from rattail.db import cache
from rattail.db.util import QuerySequence from rattail.db.util import QuerySequence
from rattail.time import make_utc from rattail.time import make_utc
from rattail.util import progress_loop from rattail.util import progress_loop, OrderedDict
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -145,16 +145,14 @@ class Importer(object):
# Prune duplicate keys from host/source data. This is for the sake of # Prune duplicate keys from host/source data. This is for the sake of
# sanity since duplicates typically lead to a ping-pong effect, where a # sanity since duplicates typically lead to a ping-pong effect, where a
# "clean" (change-less) import is impossible. # "clean" (change-less) import is impossible.
unique = {} unique = OrderedDict()
for data in host_data: for data in host_data:
key = self.get_key(data) key = self.get_key(data)
if key in unique: if key in unique:
log.warning("duplicate records detected from {} for key: {}".format( log.warning("duplicate records detected from {} for key: {}".format(
self.host_system_title, key)) self.host_system_title, key))
unique[key] = data unique[key] = data
host_data = [] host_data = list(unique.itervalues())
for key in sorted(unique):
host_data.append(unique[key])
# Cache local data if appropriate. # Cache local data if appropriate.
if self.caches_local_data: if self.caches_local_data:

View file

@ -1760,24 +1760,30 @@ class ProductCostImporter(ToRattail):
log.warning("duplicate products detected for UPC {}".format(upc.pretty())) log.warning("duplicate products detected for UPC {}".format(upc.pretty()))
if 'preferred' in self.fields: if 'preferred' in self.fields:
product = cost.product or self.session.query(model.Product).get(cost.product_uuid)
if data['preferred']: if data['preferred']:
if cost.preference is None: if cost in product.costs:
cost.preference = 1 if cost.preference != 1:
elif cost.preference != 1: product.costs.remove(cost)
product = cost.product product.costs.insert(0, cost)
product.costs.remove(cost) elif product.costs:
product.costs.insert(0, cost) product.costs.insert(0, cost)
else:
product.costs.append(cost)
else: # not preferred else: # not preferred
if cost.preference is None: if cost in product.costs:
product = cost.product or self.session.query(model.Product).get(cost.product_uuid) if cost.preference == 1:
if len(product.costs) > 1:
product.costs.remove(cost)
product.costs.append(cost)
product.costs.reorder()
else:
log.warning("cannot un-prefer cost, as product has only the one: {}".format(cost))
else:
if not product.costs:
log.warning("new cost will be preferred, as product has only the one: {}".format(self.get_key(data)))
product.costs.append(cost) product.costs.append(cost)
product.costs.reorder() product.costs.reorder()
elif cost.preference == 1:
product = cost.product
if len(product.costs) > 1:
product.costs.remove(cost)
product.costs.append(cost)
product.costs.reorder()
return cost return cost