From 097b1a638c638e6950887b1207bc75ecd7ab8838 Mon Sep 17 00:00:00 2001 From: sygel Date: Thu, 5 Sep 2024 13:27:04 +0200 Subject: [PATCH] [IMP] base_location_geonames_import: manually add zip files to folder --- .../data/zips/KY.txt | 15 ++++++ .../wizard/geonames_import.py | 50 +++++++++++++++---- 2 files changed, 54 insertions(+), 11 deletions(-) create mode 100644 base_location_geonames_import/data/zips/KY.txt diff --git a/base_location_geonames_import/data/zips/KY.txt b/base_location_geonames_import/data/zips/KY.txt new file mode 100644 index 00000000000..b9d59c75400 --- /dev/null +++ b/base_location_geonames_import/data/zips/KY.txt @@ -0,0 +1,15 @@ +CY KY1-1000 Airport Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-1600 Bodden Town Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-1800 East End Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-1100 Geoge Town Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-0002 Gun Bay Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-1400 Hell Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-1700 North Side Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-9007 Ogier Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-0001 Old Man Bay Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-1500 Sabannah Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-1200 Seven Mile Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-9000 Unique Post Code Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY1-1300 West Bay Grand Cayman KY1 Grand Cayman KY1 Grand Cayman KY1 0 0 0 +CY KY2-2000 Cayman Brac Cayman Brac KY2 Cayman Brac KY2 Cayman Brac KY2 0 0 0 +CY KY3-2500 Little Cayman Little Cayman KY3 Little Cayman KY3 Little Cayman KY3 0 0 0 diff --git a/base_location_geonames_import/wizard/geonames_import.py b/base_location_geonames_import/wizard/geonames_import.py index db5d61b89be..fb9b097373a 100644 --- a/base_location_geonames_import/wizard/geonames_import.py +++ b/base_location_geonames_import/wizard/geonames_import.py @@ -17,6 +17,7 @@ from odoo import _, api, fields, models from odoo.exceptions import UserError +from odoo.modules.module import get_module_path logger = logging.getLogger(__name__) @@ -103,25 +104,52 @@ def get_and_parse_csv(self, country): url = config_url % country_code logger.info("Starting to download %s" % url) res_request = requests.get(url, timeout=15) - if res_request.status_code != requests.codes.ok: - # pylint: disable=translation-positional-used - Don't want to re-translate - raise UserError( - _("Got an error %d when trying to download the file %s.") + + # logger.warning( + # _("Got an error %d when trying to download the file %s." + # " Searching it locally") + # % (res_request.status_code, url) + # ) + # addon_dir = get_module_path("base_location_geonames_import") + # route = os.path.join(addon_dir, f"data/zips/{country_code}.txt") + # data_file2 = open( + # route, "r", encoding="utf-8" + # ) + # data_file2.seek(0) + # reader = csv.reader(data_file2, delimiter=" ") + # parsed_csv2 = [row for i, row in enumerate(reader)] + # data_file2.close() + + if res_request.status_code == requests.codes.ok: + f_geonames = zipfile.ZipFile(io.BytesIO(res_request.content)) + tempdir = tempfile.mkdtemp(prefix="odoo") + f_geonames.extract("%s.txt" % country_code, tempdir) + + f_route = os.path.join(tempdir, "%s.txt" % country_code) + else: + logger.warning( + _( + "Got an error %d when trying to download the file %s." + " Searching it locally" + ) % (res_request.status_code, url) ) + addon_dir = get_module_path("base_location_geonames_import") + f_route = os.path.join(addon_dir, f"data/zips/{country_code}.txt") + if not os.path.exists(f_route): + # pylint: disable=translation-positional-used - Don't want to re-translate + raise UserError( + _("Got an error %d when trying to download the file %s.") + % (res_request.status_code, url) + ) - f_geonames = zipfile.ZipFile(io.BytesIO(res_request.content)) - tempdir = tempfile.mkdtemp(prefix="odoo") - f_geonames.extract("%s.txt" % country_code, tempdir) - - data_file = open( - os.path.join(tempdir, "%s.txt" % country_code), "r", encoding="utf-8" - ) + data_file = open(f_route, "r", encoding="utf-8") data_file.seek(0) reader = csv.reader(data_file, delimiter=" ") parsed_csv = [row for i, row in enumerate(reader)] data_file.close() logger.info("The geonames zipfile has been decompressed") + logging.error(parsed_csv) return parsed_csv def _create_states(self, parsed_csv, search_states, max_import, country):