Browse Source

[IMP] base_location_geonames_import: Try/except is added to remove cities and postal codes

TT33464
14.0
Víctor Martínez 3 years ago
committed by Pedro M. Baeza
parent
commit
1709a66c9e
  1. 2
      base_location_geonames_import/__manifest__.py
  2. 32
      base_location_geonames_import/wizard/geonames_import.py

2
base_location_geonames_import/__manifest__.py

@ -7,7 +7,7 @@
{
"name": "Base Location Geonames Import",
"version": "14.0.1.0.1",
"version": "14.0.1.0.2",
"category": "Partner Management",
"license": "AGPL-3",
"summary": "Import zip entries from Geonames",

32
base_location_geonames_import/wizard/geonames_import.py

@ -188,6 +188,22 @@ class CityZipGeonamesImport(models.TransientModel):
self._process_csv(parsed_csv, country)
return True
def _action_remove_old_records(self, model_name, old_records, country):
model = self.env[model_name]
items = model.browse(list(old_records))
try:
logger.info("removing %s entries" % model._name)
items.unlink()
logger.info(
"%d entries deleted for country %s" % (len(old_records), country.name)
)
except Exception:
for item in items:
try:
item.unlink()
except Exception:
logger.info(_("%d could not be deleted %") % item.name)
def _process_csv(self, parsed_csv, country):
state_model = self.env["res.country.state"]
zip_model = self.env["res.city.zip"]
@ -225,23 +241,13 @@ class CityZipGeonamesImport(models.TransientModel):
zip_vals_list.append(zip_vals)
else:
old_zips.discard(zip_code.id)
self.env["res.city.zip"].create(zip_vals_list)
zip_model.create(zip_vals_list)
if not max_import:
if old_zips:
logger.info("removing city zip entries")
self.env["res.city.zip"].browse(list(old_zips)).unlink()
logger.info(
"%d city zip entries deleted for country %s"
% (len(old_zips), country.name)
)
self._action_remove_old_records("res.city.zip", old_zips, country)
old_cities -= set(city_dict.values())
if old_cities:
logger.info("removing city entries")
self.env["res.city"].browse(list(old_cities)).unlink()
logger.info(
"%d res.city entries deleted for country %s"
% (len(old_cities), country.name)
)
self._action_remove_old_records("res.city", old_cities, country)
logger.info(
"The wizard to create cities and/or city zip entries from "
"geonames has been successfully completed."

Loading…
Cancel
Save