FIX sale_planner It is possible that the 'fall back' core geo_localize can raise an index error

This commit is contained in:
Jared Kipe
2019-08-06 16:11:36 -07:00
parent c54a601d21
commit 0d7bfda5df
2 changed files with 37 additions and 32 deletions

View File

@@ -13,17 +13,20 @@ class Partner(models.Model):
def geo_localize(self): def geo_localize(self):
# We need country names in English below # We need country names in English below
for partner in self.with_context(lang='en_US'): for partner in self.with_context(lang='en_US'):
if SearchEngine and partner.zip: try:
with SearchEngine() as search: if SearchEngine and partner.zip:
zipcode = search.by_zipcode(str(self.zip).split('-')[0]) with SearchEngine() as search:
if zipcode and zipcode.lat: zipcode = search.by_zipcode(str(self.zip).split('-')[0])
partner.write({ if zipcode and zipcode.lat:
'partner_latitude': zipcode.lat, partner.write({
'partner_longitude': zipcode.lng, 'partner_latitude': zipcode.lat,
'date_localization': fields.Date.context_today(partner), 'partner_longitude': zipcode.lng,
}) 'date_localization': fields.Date.context_today(partner),
else: })
super(Partner, partner).geo_localize() else:
else: super(Partner, partner).geo_localize()
super(Partner, partner).geo_localize() else:
super(Partner, partner).geo_localize()
except:
pass
return True return True

View File

@@ -59,26 +59,28 @@ class FakePartner():
@property @property
def date_localization(self): def date_localization(self):
if not self._date_localization: if not self._date_localization:
self._date_localization = 'TODAY!' try:
# The fast way. self._date_localization = 'TODAY!'
if SearchEngine and self.zip: # The fast way.
with SearchEngine() as search: if SearchEngine and self.zip:
zipcode = search.by_zipcode(str(self.zip).split('-')[0]) with SearchEngine() as search:
if zipcode and zipcode.lat: zipcode = search.by_zipcode(str(self.zip).split('-')[0])
self.partner_latitude = zipcode.lat if zipcode and zipcode.lat:
self.partner_longitude = zipcode.lng self.partner_latitude = zipcode.lat
return self._date_localization self.partner_longitude = zipcode.lng
return self._date_localization
# The slow way.
result = geo_find(geo_query_address(
city=self.city,
state=self.state_id.name,
country=self.country_id.name,
))
if result:
self.partner_latitude = result[0]
self.partner_longitude = result[1]
# The slow way.
result = geo_find(geo_query_address(
city=self.city,
state=self.state_id.name,
country=self.country_id.name,
))
if result:
self.partner_latitude = result[0]
self.partner_longitude = result[1]
except:
self._date_localization = 'ERROR'
return self._date_localization return self._date_localization
def __getattr__(self, item): def __getattr__(self, item):