You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
9.4 KiB

  1. # Copyright 2014-2016 Akretion (Alexis de Lattre
  2. # <alexis.delattre@akretion.com>)
  3. # Copyright 2014 Lorenzo Battistini <lorenzo.battistini@agilebg.com>
  4. # Copyright 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
  5. # Copyright 2017 Eficent Business and IT Consulting Services, S.L.
  6. # <contact@eficent.com>
  7. # Copyright 2018 Aitor Bouzas <aitor.bouzas@adaptivecity.com>
  8. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  9. from odoo import _, api, fields, models
  10. from odoo.exceptions import UserError
  11. import requests
  12. import tempfile
  13. import io
  14. import zipfile
  15. import os
  16. import logging
  17. import csv
  18. logger = logging.getLogger(__name__)
  19. class CityZipGeonamesImport(models.TransientModel):
  20. _name = 'city.zip.geonames.import'
  21. _description = 'Import City Zips from Geonames'
  22. _rec_name = 'country_id'
  23. country_id = fields.Many2one('res.country', 'Country', required=True)
  24. code_row_index = fields.Integer(
  25. related='country_id.geonames_state_code_column',
  26. readonly=True)
  27. name_row_index = fields.Integer(
  28. related='country_id.geonames_state_name_column')
  29. letter_case = fields.Selection([
  30. ('unchanged', 'Unchanged'),
  31. ('title', 'Title Case'),
  32. ('upper', 'Upper Case'),
  33. ], string='Letter Case', default='unchanged',
  34. help="Converts retreived city and state names to Title Case "
  35. "(upper case on each first letter of a word) or Upper Case "
  36. "(all letters upper case).")
  37. @api.model
  38. def transform_city_name(self, city, country):
  39. """Override it for transforming city name (if needed)
  40. :param city: Original city name
  41. :param country: Country record
  42. :return: Transformed city name
  43. """
  44. res = city
  45. if self.letter_case == 'title':
  46. res = city.title()
  47. elif self.letter_case == 'upper':
  48. res = city.upper()
  49. return res
  50. @api.model
  51. def _domain_search_res_city(self, row, country):
  52. return [('name', '=', self.transform_city_name(row[2], country)),
  53. ('country_id', '=', country.id)]
  54. @api.model
  55. def _domain_search_city_zip(self, row, res_city):
  56. domain = [('name', '=', row[1])]
  57. if res_city:
  58. domain += [('city_id', '=', res_city.id)]
  59. return domain
  60. @api.model
  61. def select_state(self, row, country):
  62. code = row[self.code_row_index or 4]
  63. return self.env['res.country.state'].search(
  64. [('country_id', '=', country.id),
  65. ('code', '=', code)], limit=1,
  66. )
  67. @api.model
  68. def select_city(self, row, country):
  69. res_city_model = self.env['res.city']
  70. return res_city_model.search(self._domain_search_res_city(
  71. row, country), limit=1)
  72. @api.model
  73. def select_zip(self, row, country):
  74. city = self.select_city(row, country)
  75. return self.env['res.city.zip'].search(self._domain_search_city_zip(
  76. row, city))
  77. @api.model
  78. def prepare_state(self, row, country):
  79. return {
  80. 'name': row[self.name_row_index or 3],
  81. 'code': row[self.code_row_index or 4],
  82. 'country_id': country.id,
  83. }
  84. @api.model
  85. def prepare_city(self, row, country, state_id):
  86. vals = {
  87. 'name': self.transform_city_name(row[2], country),
  88. 'state_id': state_id,
  89. 'country_id': country.id,
  90. }
  91. return vals
  92. @api.model
  93. def prepare_zip(self, row, city_id):
  94. vals = {
  95. 'name': row[1],
  96. 'city_id': city_id,
  97. }
  98. return vals
  99. @api.model
  100. def get_and_parse_csv(self):
  101. country_code = self.country_id.code
  102. config_url = self.env['ir.config_parameter'].get_param(
  103. 'geonames.url',
  104. default='http://download.geonames.org/export/zip/%s.zip')
  105. url = config_url % country_code
  106. logger.info('Starting to download %s' % url)
  107. res_request = requests.get(url)
  108. if res_request.status_code != requests.codes.ok:
  109. raise UserError(
  110. _('Got an error %d when trying to download the file %s.')
  111. % (res_request.status_code, url))
  112. f_geonames = zipfile.ZipFile(io.BytesIO(res_request.content))
  113. tempdir = tempfile.mkdtemp(prefix='odoo')
  114. f_geonames.extract('%s.txt' % country_code, tempdir)
  115. data_file = open(os.path.join(tempdir, '%s.txt' % country_code), 'r',
  116. encoding='utf-8')
  117. data_file.seek(0)
  118. reader = csv.reader(data_file, delimiter=' ')
  119. parsed_csv = [row for i, row in enumerate(reader)]
  120. data_file.close()
  121. logger.info('The geonames zipfile has been decompressed')
  122. return parsed_csv
  123. def _create_states(self, parsed_csv, search_states, max_import):
  124. # States
  125. state_vals_list = []
  126. state_dict = {}
  127. for i, row in enumerate(parsed_csv):
  128. if max_import and i == max_import:
  129. break
  130. state = self.select_state(
  131. row, self.country_id) if search_states else False
  132. if not state:
  133. state_vals = self.prepare_state(row, self.country_id)
  134. if state_vals not in state_vals_list:
  135. state_vals_list.append(state_vals)
  136. else:
  137. state_dict[state.code] = state.id
  138. created_states = self.env['res.country.state'].create(state_vals_list)
  139. for i, vals in enumerate(state_vals_list):
  140. state_dict[vals['code']] = created_states[i].id
  141. return state_dict
  142. def _create_cities(self, parsed_csv,
  143. search_cities, max_import, state_dict):
  144. # Cities
  145. city_vals_list = []
  146. city_dict = {}
  147. for i, row in enumerate(parsed_csv):
  148. if max_import and i == max_import:
  149. break
  150. city = self.select_city(
  151. row, self.country_id) if search_cities else False
  152. if not city:
  153. state_id = state_dict[
  154. row[self.code_row_index or 4]]
  155. city_vals = self.prepare_city(
  156. row, self.country_id, state_id)
  157. if city_vals not in city_vals_list:
  158. city_vals_list.append(city_vals)
  159. else:
  160. city_dict[city.name] = city.id
  161. created_cities = self.env['res.city'].create(city_vals_list)
  162. for i, vals in enumerate(city_vals_list):
  163. city_dict[vals['name']] = created_cities[i].id
  164. return city_dict
  165. @api.multi
  166. def run_import(self):
  167. self.ensure_one()
  168. state_model = self.env['res.country.state']
  169. zip_model = self.env['res.city.zip']
  170. res_city_model = self.env['res.city']
  171. # Store current record list
  172. current_zips = zip_model.search(
  173. [('city_id.country_id', '=', self.country_id.id)])
  174. search_zips = True and len(current_zips) > 0 or False
  175. current_cities = res_city_model.search(
  176. [('country_id', '=', self.country_id.id)])
  177. search_cities = True and len(current_cities) > 0 or False
  178. current_states = state_model.search(
  179. [('country_id', '=', self.country_id.id)])
  180. search_states = True and len(current_states) > 0 or False
  181. parsed_csv = self.get_and_parse_csv()
  182. max_import = self.env.context.get('max_import', 0)
  183. logger.info('Starting to create the cities and/or city zip entries')
  184. state_dict = self._create_states(parsed_csv,
  185. search_states, max_import)
  186. city_dict = self._create_cities(parsed_csv,
  187. search_cities, max_import, state_dict)
  188. # Zips
  189. zip_vals_list = []
  190. for i, row in enumerate(parsed_csv):
  191. if max_import and i == max_import:
  192. break
  193. # Don't search if there aren't any records
  194. zip = False
  195. if search_zips:
  196. zip = self.select_zip(row, self.country_id)
  197. if not zip:
  198. city_id = city_dict[
  199. self.transform_city_name(row[2], self.country_id)]
  200. zip_vals = self.prepare_zip(row, city_id)
  201. if zip_vals not in zip_vals_list:
  202. zip_vals_list.append(zip_vals)
  203. delete_zips = self.env['res.city.zip'].create(zip_vals_list)
  204. current_zips -= delete_zips
  205. if not max_import:
  206. current_zips.unlink()
  207. logger.info('%d city zip entries deleted for country %s' %
  208. (len(current_zips), self.country_id.name))
  209. # Since we wrapped the entire cities
  210. # creation in a function we need
  211. # to perform a search with city_dict in
  212. # order to know which are the new ones so
  213. # we can delete the old ones
  214. created_cities = res_city_model.search(
  215. [('country_id', '=', self.country_id.id),
  216. ('id', 'in', [value for key, value in city_dict.items()])]
  217. )
  218. current_cities -= created_cities
  219. current_cities.unlink()
  220. logger.info('%d res.city entries deleted for country %s' %
  221. (len(current_cities), self.country_id.name))
  222. logger.info(
  223. 'The wizard to create cities and/or city zip entries from '
  224. 'geonames has been successfully completed.')
  225. return True