Skip to content

Commit

Permalink
[IMP] base_location_geonames_import: manually add zip files to folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tisho99 committed Sep 5, 2024
1 parent 888fc0b commit 097b1a6
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
15 changes: 15 additions & 0 deletions base_location_geonames_import/data/zips/KY.txt
Original file line number Diff line number Diff line change
@@ -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
50 changes: 39 additions & 11 deletions base_location_geonames_import/wizard/geonames_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 097b1a6

Please sign in to comment.