Better manage price object juggling in Item.price property. The previous way was creating new price objects when an object already existed in the database which could lead to Integrity issues.

This commit is contained in:
blitzmann
2017-04-10 20:48:58 -04:00
parent 1260900668
commit ce01c66b3e
3 changed files with 23 additions and 30 deletions

View File

@@ -254,6 +254,7 @@ class Item(EqBase):
self.__offensive = None
self.__assistive = None
self.__overrides = None
self.__price = None
@property
def attributes(self):
@@ -441,34 +442,22 @@ class Item(EqBase):
@property
def price(self):
try:
if not hasattr(self, "__price"):
# todo: use `from sqlalchemy import inspect` instead (need to verify it works in old and new OS X builds)
if self.__price is not None and getattr(self.__price, '_sa_instance_state', None):
pyfalog.debug("Price data for {} was deleted, resetting object".format(self.ID))
self.__price = None
if self.__price is None:
db_price = eos.db.getPrice(self.ID)
# do not yet have a price in the database for this item, create one
if db_price is None:
print "Creating a price for {}".format(self.ID)
self.__price = types_Price(self.ID)
# Get the price from the DB
price = eos.db.getPrice(self.ID)
if price:
if self.__price.time > price.time:
# The object is newer than the DB, update the DB.
eos.db.add(self.__price)
eos.db.commit()
if self.__price.time < price.time:
# DB object is newer than local object, update the local object
self.__price = price
eos.db.add(self.__price)
eos.db.commit()
else:
pyfalog.debug("Unable to fetch item price from database.")
return self.__price
except Exception as e:
# We want to catch our failure and log it, but don't bail out for a single missing price tag.
pyfalog.error("Failed to get price for typeID: {0}", self.ID)
pyfalog.error(e)
if not self.__price.price:
self.__price.price = 0
self.__price.failed = True
self.__price = db_price
return self.__price