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.util import QuerySequence
from rattail.time import make_utc
from rattail.util import progress_loop
from rattail.util import progress_loop, OrderedDict
log = logging.getLogger(__name__)
@ -145,16 +145,14 @@ class Importer(object):
# 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
# "clean" (change-less) import is impossible.
unique = {}
unique = OrderedDict()
for data in host_data:
key = self.get_key(data)
if key in unique:
log.warning("duplicate records detected from {} for key: {}".format(
self.host_system_title, key))
unique[key] = data
host_data = []
for key in sorted(unique):
host_data.append(unique[key])
host_data = list(unique.itervalues())
# Cache local data if appropriate.
if self.caches_local_data:

View file

@ -1760,24 +1760,30 @@ class ProductCostImporter(ToRattail):
log.warning("duplicate products detected for UPC {}".format(upc.pretty()))
if 'preferred' in self.fields:
product = cost.product or self.session.query(model.Product).get(cost.product_uuid)
if data['preferred']:
if cost.preference is None:
cost.preference = 1
elif cost.preference != 1:
product = cost.product
product.costs.remove(cost)
if cost in product.costs:
if cost.preference != 1:
product.costs.remove(cost)
product.costs.insert(0, cost)
elif product.costs:
product.costs.insert(0, cost)
else:
product.costs.append(cost)
else: # not preferred
if cost.preference is None:
product = cost.product or self.session.query(model.Product).get(cost.product_uuid)
if cost in product.costs:
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.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