Merge lp:~akretion-team/partner-contact-management/base-location-geonames-import into lp:~partner-contact-core-editors/partner-contact-management/7.0
- base-location-geonames-import
- Merge into 7.0
Status: | Needs review |
---|---|
Proposed branch: | lp:~akretion-team/partner-contact-management/base-location-geonames-import |
Merge into: | lp:~partner-contact-core-editors/partner-contact-management/7.0 |
Diff against target: |
437 lines (+400/-0) 7 files modified
base_location_geonames_import/__init__.py (+23/-0) base_location_geonames_import/__openerp__.py (+47/-0) base_location_geonames_import/i18n/base_location_geonames_import.pot (+68/-0) base_location_geonames_import/i18n/fr.po (+68/-0) base_location_geonames_import/wizard/__init__.py (+23/-0) base_location_geonames_import/wizard/geonames_import.py (+127/-0) base_location_geonames_import/wizard/geonames_import_view.xml (+44/-0) |
To merge this branch: | bzr merge lp:~akretion-team/partner-contact-management/base-location-geonames-import |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
Lorenzo Battistini (community) | Needs Resubmitting | ||
Nicolas Bessi - Camptocamp (community) | Needs Fixing | ||
Review via email: mp+214564@code.launchpad.net |
Commit message
Description of the change
Add module base_location_
Nicolas Bessi suggested on twitter that it should be part of the geospacial addons. But, after thinking about it, my opinion is that this module would be better here, because users of the "base_location" module would have a higher chance of finding this module here than in the geospacial addons.
- 40. By Alexis de Lattre
-
Allow to skip entries in the _prepare method.
Nicolas Bessi - Camptocamp (nbessi-c2c-deactivatedaccount) wrote : | # |
Alexis de Lattre (alexis-via) wrote : | # |
@Nicolas
As I said in my comment, I think that it's better for the module "base_location_
Nicolas Bessi - Camptocamp (nbessi-c2c-deactivatedaccount) wrote : | # |
Oups I missed that.
Actually, I do not agrees with you, but I won't argue too long on this as for me the branch organisation problem is more deep. Let's see what other think about it.
Some comments Belows:
Some PEP8 in manifest:
328 + _("The content of the file doesn't correspond to the "
329 + "selected country."))
I will add row value and country info it will be more easy for support.
A question with :
+ if res_request.
349 + raise orm.except_orm(
350 + _('Error:'),
351 + _('Got an error %d when trying to download the file %s.')
352 + % (res_request.
It's been a long time since I used geonames, if I'm correct in case of wrong country it returns 404 that right. If it the case I'm ok with this else we may have to treat a 200 with a message
+ if bzip_ids_to_delete:
356 + bzip_obj.unlink(cr, uid, bzip_ids_to_delete, context=context)
357 + logger.info(
358 + '%d better zip entries deleted for country %s'
359 + % (len(bzip_
This is a quite a direct approach, I agree create, update, unactivate is quite not easy to put in place, but as other development can depends on base location model there should at least be a Big warning in manifest.
Also it would be a good idea to add a small lock to ensure atomicity during the import.
A query "for update no wait" a the begining of transaction would be nice.
Tests are also missing. Without depending on the geoname services having a local excrept to base the tests would be great.
Thanks for the contributions, keep up the good work.
Regards
Nicolas
- 41. By Alexis de Lattre
-
Insist on the fact that the wizard deletes current better zip entries in the module description and in the wizard itself.
Better error message when the country code inside the file is wrong. - 42. By Alexis de Lattre
-
Add a FOR UPDATE NOWAIT at the beginning of the transaction.
Alexis de Lattre (alexis-via) wrote : | # |
@Nicolas:
I confirm that it returns 404 if you try to download a wrong country.
I have taken into account your other remarks in my last commits (unless the tests ; I don't have experience in this). I am not sure I fully understand the benefit of the FOR UPDATE NOWAIT in this particular case... at least I hope I implemented it as you wanted.
Nicolas Bessi - Camptocamp (nbessi-c2c-deactivatedaccount) wrote : | # |
Hello,
Thank for the fixes.
The "UPDATE NO WAIT" will lock the selected rows at postgres database level.
It will avoid a process using the base_location data to alter flushed entries.
It will also avoiding having many concurrent instance of the wizard running on the same rows.
In this case I wonder if we may not want to put a lock on the whole table has we add new entries.
For the test I will see ifI can provide some but I'm in holiday next week, so it have to wait a little.
Regards
Nicolas
- 43. By Alexis de Lattre
-
Remove France-specific code ; moved to new module l10n_fr_
base_location_ geonames_ import on lp:openerp-french-localization - 44. By Alexis de Lattre
-
[MERGE] merge with trunk revno 40.
Lorenzo Battistini (elbati) wrote : | # |
Hello Alexis, many thanks for the module.
What do you think about creating the res.country.state records if they don't exist, before mapping them in the 'states' dictionary?
The current version is supposed to correctly work with states if you first create states data by modules like l10n_fr_state.
But if base_location_
Lorenzo Battistini (elbati) wrote : | # |
This project is now hosted on https:/
Lorenzo Battistini (elbati) wrote : | # |
PR for v8 at https:/
Unmerged revisions
- 44. By Alexis de Lattre
-
[MERGE] merge with trunk revno 40.
- 43. By Alexis de Lattre
-
Remove France-specific code ; moved to new module l10n_fr_
base_location_ geonames_ import on lp:openerp-french-localization - 42. By Alexis de Lattre
-
Add a FOR UPDATE NOWAIT at the beginning of the transaction.
- 41. By Alexis de Lattre
-
Insist on the fact that the wizard deletes current better zip entries in the module description and in the wizard itself.
Better error message when the country code inside the file is wrong. - 40. By Alexis de Lattre
-
Allow to skip entries in the _prepare method.
- 39. By Alexis de Lattre
-
Add support for states (if states are already present in res.country.state).
Add POT file and FR translation. - 38. By Alexis de Lattre
-
Add module base_location_
geonames_ import
Preview Diff
1 | === added directory 'base_location_geonames_import' | |||
2 | === added file 'base_location_geonames_import/__init__.py' | |||
3 | --- base_location_geonames_import/__init__.py 1970-01-01 00:00:00 +0000 | |||
4 | +++ base_location_geonames_import/__init__.py 2014-06-11 06:33:26 +0000 | |||
5 | @@ -0,0 +1,23 @@ | |||
6 | 1 | # -*- encoding: utf-8 -*- | ||
7 | 2 | ############################################################################## | ||
8 | 3 | # | ||
9 | 4 | # Base Location Geonames Import module for OpenERP | ||
10 | 5 | # Copyright (C) 2014 Akretion (http://www.akretion.com) | ||
11 | 6 | # @author Alexis de Lattre <alexis.delattre@akretion.com> | ||
12 | 7 | # | ||
13 | 8 | # This program is free software: you can redistribute it and/or modify | ||
14 | 9 | # it under the terms of the GNU Affero General Public License as | ||
15 | 10 | # published by the Free Software Foundation, either version 3 of the | ||
16 | 11 | # License, or (at your option) any later version. | ||
17 | 12 | # | ||
18 | 13 | # This program is distributed in the hope that it will be useful, | ||
19 | 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
20 | 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
21 | 16 | # GNU Affero General Public License for more details. | ||
22 | 17 | # | ||
23 | 18 | # You should have received a copy of the GNU Affero General Public License | ||
24 | 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
25 | 20 | # | ||
26 | 21 | ############################################################################## | ||
27 | 22 | |||
28 | 23 | from . import wizard | ||
29 | 0 | 24 | ||
30 | === added file 'base_location_geonames_import/__openerp__.py' | |||
31 | --- base_location_geonames_import/__openerp__.py 1970-01-01 00:00:00 +0000 | |||
32 | +++ base_location_geonames_import/__openerp__.py 2014-06-11 06:33:26 +0000 | |||
33 | @@ -0,0 +1,47 @@ | |||
34 | 1 | # -*- encoding: utf-8 -*- | ||
35 | 2 | ############################################################################## | ||
36 | 3 | # | ||
37 | 4 | # Base Location Geonames Import module for OpenERP | ||
38 | 5 | # Copyright (C) 2014 Akretion (http://www.akretion.com) | ||
39 | 6 | # @author Alexis de Lattre <alexis.delattre@akretion.com> | ||
40 | 7 | # | ||
41 | 8 | # This program is free software: you can redistribute it and/or modify | ||
42 | 9 | # it under the terms of the GNU Affero General Public License as | ||
43 | 10 | # published by the Free Software Foundation, either version 3 of the | ||
44 | 11 | # License, or (at your option) any later version. | ||
45 | 12 | # | ||
46 | 13 | # This program is distributed in the hope that it will be useful, | ||
47 | 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
48 | 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
49 | 16 | # GNU Affero General Public License for more details. | ||
50 | 17 | # | ||
51 | 18 | # You should have received a copy of the GNU Affero General Public License | ||
52 | 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
53 | 20 | # | ||
54 | 21 | ############################################################################## | ||
55 | 22 | |||
56 | 23 | |||
57 | 24 | { | ||
58 | 25 | 'name': 'Base Location Geonames Import', | ||
59 | 26 | 'version': '0.1', | ||
60 | 27 | 'category': 'Extra Tools', | ||
61 | 28 | 'license': 'AGPL-3', | ||
62 | 29 | 'summary': 'Import better zip entries from Geonames', | ||
63 | 30 | 'description': """ | ||
64 | 31 | Base Location Geonames Import | ||
65 | 32 | ============================= | ||
66 | 33 | |||
67 | 34 | This module adds a wizard to import better zip entries from Geonames (http://download.geonames.org/export/zip/). When you start the wizard, it will ask you to select a country ; then, for the selected country, it will delete all the current better zip entries, download the latest version of the list of cities from geonames.org and create new better zip entries. | ||
68 | 35 | |||
69 | 36 | Please contact Alexis de Lattre from Akretion <alexis.delattre@akretion.com> for any help or question about this module. | ||
70 | 37 | """, | ||
71 | 38 | 'author': 'Akretion', | ||
72 | 39 | 'website': 'http://www.akretion.com', | ||
73 | 40 | 'depends': ['base_location'], | ||
74 | 41 | 'external_dependencies': {'python': ['requests', 'unicodecsv']}, | ||
75 | 42 | 'data': [ | ||
76 | 43 | 'wizard/geonames_import_view.xml', | ||
77 | 44 | ], | ||
78 | 45 | 'installable': True, | ||
79 | 46 | 'active': False, | ||
80 | 47 | } | ||
81 | 0 | 48 | ||
82 | === added directory 'base_location_geonames_import/i18n' | |||
83 | === added file 'base_location_geonames_import/i18n/base_location_geonames_import.pot' | |||
84 | --- base_location_geonames_import/i18n/base_location_geonames_import.pot 1970-01-01 00:00:00 +0000 | |||
85 | +++ base_location_geonames_import/i18n/base_location_geonames_import.pot 2014-06-11 06:33:26 +0000 | |||
86 | @@ -0,0 +1,68 @@ | |||
87 | 1 | # Translation of OpenERP Server. | ||
88 | 2 | # This file contains the translation of the following modules: | ||
89 | 3 | # * base_location_geonames_import | ||
90 | 4 | # | ||
91 | 5 | msgid "" | ||
92 | 6 | msgstr "" | ||
93 | 7 | "Project-Id-Version: OpenERP Server 7.0\n" | ||
94 | 8 | "Report-Msgid-Bugs-To: \n" | ||
95 | 9 | "POT-Creation-Date: 2014-04-11 20:55+0000\n" | ||
96 | 10 | "PO-Revision-Date: 2014-04-11 20:55+0000\n" | ||
97 | 11 | "Last-Translator: <>\n" | ||
98 | 12 | "Language-Team: \n" | ||
99 | 13 | "MIME-Version: 1.0\n" | ||
100 | 14 | "Content-Type: text/plain; charset=UTF-8\n" | ||
101 | 15 | "Content-Transfer-Encoding: \n" | ||
102 | 16 | "Plural-Forms: \n" | ||
103 | 17 | |||
104 | 18 | #. module: base_location_geonames_import | ||
105 | 19 | #: view:better.zip.geonames.import:0 | ||
106 | 20 | msgid "Cancel" | ||
107 | 21 | msgstr "" | ||
108 | 22 | |||
109 | 23 | #. module: base_location_geonames_import | ||
110 | 24 | #: field:better.zip.geonames.import,country_id:0 | ||
111 | 25 | msgid "Country" | ||
112 | 26 | msgstr "" | ||
113 | 27 | |||
114 | 28 | #. module: base_location_geonames_import | ||
115 | 29 | #: code:addons/base_location_geonames_import/wizard/geonames_import.py:66 | ||
116 | 30 | #: code:addons/base_location_geonames_import/wizard/geonames_import.py:90 | ||
117 | 31 | #, python-format | ||
118 | 32 | msgid "Error:" | ||
119 | 33 | msgstr "" | ||
120 | 34 | |||
121 | 35 | #. module: base_location_geonames_import | ||
122 | 36 | #: view:better.zip.geonames.import:0 | ||
123 | 37 | msgid "For the country selected above, this wizard will DELETE ALL THE CURRENT BETTER ZIP ENTRIES, download the latest version of the list of cities from geonames.org and create new better zip entries." | ||
124 | 38 | msgstr "" | ||
125 | 39 | |||
126 | 40 | #. module: base_location_geonames_import | ||
127 | 41 | #: code:addons/base_location_geonames_import/wizard/geonames_import.py:91 | ||
128 | 42 | #, python-format | ||
129 | 43 | msgid "Got an error %d when trying to download the file %s." | ||
130 | 44 | msgstr "" | ||
131 | 45 | |||
132 | 46 | #. module: base_location_geonames_import | ||
133 | 47 | #: view:better.zip.geonames.import:0 | ||
134 | 48 | msgid "Import" | ||
135 | 49 | msgstr "" | ||
136 | 50 | |||
137 | 51 | #. module: base_location_geonames_import | ||
138 | 52 | #: model:ir.model,name:base_location_geonames_import.model_better_zip_geonames_import | ||
139 | 53 | msgid "Import Better Zip from Geonames" | ||
140 | 54 | msgstr "" | ||
141 | 55 | |||
142 | 56 | #. module: base_location_geonames_import | ||
143 | 57 | #: view:better.zip.geonames.import:0 | ||
144 | 58 | #: model:ir.actions.act_window,name:base_location_geonames_import.better_zip_geonames_import_action | ||
145 | 59 | #: model:ir.ui.menu,name:base_location_geonames_import.better_zip_geonames_import_menu | ||
146 | 60 | msgid "Import Geonames" | ||
147 | 61 | msgstr "" | ||
148 | 62 | |||
149 | 63 | #. module: base_location_geonames_import | ||
150 | 64 | #: code:addons/base_location_geonames_import/wizard/geonames_import.py:67 | ||
151 | 65 | #, python-format | ||
152 | 66 | msgid "The country code inside the file (%s) doesn't correspond to the selected country (%s)." | ||
153 | 67 | msgstr "" | ||
154 | 68 | |||
155 | 0 | 69 | ||
156 | === added file 'base_location_geonames_import/i18n/fr.po' | |||
157 | --- base_location_geonames_import/i18n/fr.po 1970-01-01 00:00:00 +0000 | |||
158 | +++ base_location_geonames_import/i18n/fr.po 2014-06-11 06:33:26 +0000 | |||
159 | @@ -0,0 +1,68 @@ | |||
160 | 1 | # Translation of OpenERP Server. | ||
161 | 2 | # This file contains the translation of the following modules: | ||
162 | 3 | # * base_location_geonames_import | ||
163 | 4 | # | ||
164 | 5 | msgid "" | ||
165 | 6 | msgstr "" | ||
166 | 7 | "Project-Id-Version: OpenERP Server 7.0\n" | ||
167 | 8 | "Report-Msgid-Bugs-To: \n" | ||
168 | 9 | "POT-Creation-Date: 2014-04-11 20:56+0000\n" | ||
169 | 10 | "PO-Revision-Date: 2014-04-11 20:56+0000\n" | ||
170 | 11 | "Last-Translator: <>\n" | ||
171 | 12 | "Language-Team: \n" | ||
172 | 13 | "MIME-Version: 1.0\n" | ||
173 | 14 | "Content-Type: text/plain; charset=UTF-8\n" | ||
174 | 15 | "Content-Transfer-Encoding: \n" | ||
175 | 16 | "Plural-Forms: \n" | ||
176 | 17 | |||
177 | 18 | #. module: base_location_geonames_import | ||
178 | 19 | #: view:better.zip.geonames.import:0 | ||
179 | 20 | msgid "Cancel" | ||
180 | 21 | msgstr "Annuler" | ||
181 | 22 | |||
182 | 23 | #. module: base_location_geonames_import | ||
183 | 24 | #: field:better.zip.geonames.import,country_id:0 | ||
184 | 25 | msgid "Country" | ||
185 | 26 | msgstr "Pays" | ||
186 | 27 | |||
187 | 28 | #. module: base_location_geonames_import | ||
188 | 29 | #: code:addons/base_location_geonames_import/wizard/geonames_import.py:66 | ||
189 | 30 | #: code:addons/base_location_geonames_import/wizard/geonames_import.py:90 | ||
190 | 31 | #, python-format | ||
191 | 32 | msgid "Error:" | ||
192 | 33 | msgstr "Erreur :" | ||
193 | 34 | |||
194 | 35 | #. module: base_location_geonames_import | ||
195 | 36 | #: view:better.zip.geonames.import:0 | ||
196 | 37 | msgid "For the country selected above, this wizard will DELETE ALL THE CURRENT BETTER ZIP ENTRIES, download the latest version of the list of cities from geonames.org and create new better zip entries." | ||
197 | 38 | msgstr "Pour le pays sélectionné ci-dessus, cet assistant va SUPPRIMER TOUTES LES ENTREES BETTER ZIP, télécharger la dernière version de la liste des villes depuis geonames.org et créer de nouveaux enregistrements better zip." | ||
198 | 39 | |||
199 | 40 | #. module: base_location_geonames_import | ||
200 | 41 | #: code:addons/base_location_geonames_import/wizard/geonames_import.py:91 | ||
201 | 42 | #, python-format | ||
202 | 43 | msgid "Got an error %d when trying to download the file %s." | ||
203 | 44 | msgstr "Erreur %d reçue suite à la tentative de téléchargement du fichier %s." | ||
204 | 45 | |||
205 | 46 | #. module: base_location_geonames_import | ||
206 | 47 | #: view:better.zip.geonames.import:0 | ||
207 | 48 | msgid "Import" | ||
208 | 49 | msgstr "Importer" | ||
209 | 50 | |||
210 | 51 | #. module: base_location_geonames_import | ||
211 | 52 | #: model:ir.model,name:base_location_geonames_import.model_better_zip_geonames_import | ||
212 | 53 | msgid "Import Better Zip from Geonames" | ||
213 | 54 | msgstr "Import Better Zip from Geonames" | ||
214 | 55 | |||
215 | 56 | #. module: base_location_geonames_import | ||
216 | 57 | #: view:better.zip.geonames.import:0 | ||
217 | 58 | #: model:ir.actions.act_window,name:base_location_geonames_import.better_zip_geonames_import_action | ||
218 | 59 | #: model:ir.ui.menu,name:base_location_geonames_import.better_zip_geonames_import_menu | ||
219 | 60 | msgid "Import Geonames" | ||
220 | 61 | msgstr "Importer Geonames" | ||
221 | 62 | |||
222 | 63 | #. module: base_location_geonames_import | ||
223 | 64 | #: code:addons/base_location_geonames_import/wizard/geonames_import.py:67 | ||
224 | 65 | #, python-format | ||
225 | 66 | msgid "The country code inside the file (%s) doesn't correspond to the selected country (%s)." | ||
226 | 67 | msgstr "Le code pays utilisé à l'intérieur du fichier (%s) ne correspond pas au pays sélectionné (%s)." | ||
227 | 68 | |||
228 | 0 | 69 | ||
229 | === added directory 'base_location_geonames_import/wizard' | |||
230 | === added file 'base_location_geonames_import/wizard/__init__.py' | |||
231 | --- base_location_geonames_import/wizard/__init__.py 1970-01-01 00:00:00 +0000 | |||
232 | +++ base_location_geonames_import/wizard/__init__.py 2014-06-11 06:33:26 +0000 | |||
233 | @@ -0,0 +1,23 @@ | |||
234 | 1 | # -*- encoding: utf-8 -*- | ||
235 | 2 | ############################################################################## | ||
236 | 3 | # | ||
237 | 4 | # Base Location Geonames Import module for OpenERP | ||
238 | 5 | # Copyright (C) 2014 Akretion (http://www.akretion.com) | ||
239 | 6 | # @author Alexis de Lattre <alexis.delattre@akretion.com> | ||
240 | 7 | # | ||
241 | 8 | # This program is free software: you can redistribute it and/or modify | ||
242 | 9 | # it under the terms of the GNU Affero General Public License as | ||
243 | 10 | # published by the Free Software Foundation, either version 3 of the | ||
244 | 11 | # License, or (at your option) any later version. | ||
245 | 12 | # | ||
246 | 13 | # This program is distributed in the hope that it will be useful, | ||
247 | 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
248 | 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
249 | 16 | # GNU Affero General Public License for more details. | ||
250 | 17 | # | ||
251 | 18 | # You should have received a copy of the GNU Affero General Public License | ||
252 | 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
253 | 20 | # | ||
254 | 21 | ############################################################################## | ||
255 | 22 | |||
256 | 23 | from . import geonames_import | ||
257 | 0 | 24 | ||
258 | === added file 'base_location_geonames_import/wizard/geonames_import.py' | |||
259 | --- base_location_geonames_import/wizard/geonames_import.py 1970-01-01 00:00:00 +0000 | |||
260 | +++ base_location_geonames_import/wizard/geonames_import.py 2014-06-11 06:33:26 +0000 | |||
261 | @@ -0,0 +1,127 @@ | |||
262 | 1 | # -*- encoding: utf-8 -*- | ||
263 | 2 | ############################################################################## | ||
264 | 3 | # | ||
265 | 4 | # Base Location Geonames Import module for OpenERP | ||
266 | 5 | # Copyright (C) 2014 Akretion (http://www.akretion.com) | ||
267 | 6 | # @author Alexis de Lattre <alexis.delattre@akretion.com> | ||
268 | 7 | # | ||
269 | 8 | # This program is free software: you can redistribute it and/or modify | ||
270 | 9 | # it under the terms of the GNU Affero General Public License as | ||
271 | 10 | # published by the Free Software Foundation, either version 3 of the | ||
272 | 11 | # License, or (at your option) any later version. | ||
273 | 12 | # | ||
274 | 13 | # This program is distributed in the hope that it will be useful, | ||
275 | 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
276 | 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
277 | 16 | # GNU Affero General Public License for more details. | ||
278 | 17 | # | ||
279 | 18 | # You should have received a copy of the GNU Affero General Public License | ||
280 | 19 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
281 | 20 | # | ||
282 | 21 | ############################################################################## | ||
283 | 22 | |||
284 | 23 | from openerp.osv import orm, fields | ||
285 | 24 | from openerp.tools.translate import _ | ||
286 | 25 | import requests | ||
287 | 26 | import tempfile | ||
288 | 27 | import StringIO | ||
289 | 28 | import unicodecsv | ||
290 | 29 | import zipfile | ||
291 | 30 | import os | ||
292 | 31 | import logging | ||
293 | 32 | |||
294 | 33 | logger = logging.getLogger(__name__) | ||
295 | 34 | |||
296 | 35 | |||
297 | 36 | class better_zip_geonames_import(orm.TransientModel): | ||
298 | 37 | _name = 'better.zip.geonames.import' | ||
299 | 38 | _description = 'Import Better Zip from Geonames' | ||
300 | 39 | |||
301 | 40 | _columns = { | ||
302 | 41 | 'country_id': fields.many2one('res.country', 'Country', required=True), | ||
303 | 42 | } | ||
304 | 43 | |||
305 | 44 | def _prepare_better_zip( | ||
306 | 45 | self, cr, uid, row, country_id, states, context=None): | ||
307 | 46 | '''This function is designed to be inherited''' | ||
308 | 47 | state_id = False | ||
309 | 48 | if states and row[4] and row[4] in states: | ||
310 | 49 | state_id = states[row[4].upper()] | ||
311 | 50 | vals = { | ||
312 | 51 | 'name': row[1], | ||
313 | 52 | 'city': row[2], | ||
314 | 53 | 'state_id': state_id, | ||
315 | 54 | 'country_id': country_id, | ||
316 | 55 | } | ||
317 | 56 | return vals | ||
318 | 57 | |||
319 | 58 | def create_better_zip( | ||
320 | 59 | self, cr, uid, row, country_id, country_code, states, | ||
321 | 60 | context=None): | ||
322 | 61 | bzip_id = False | ||
323 | 62 | if row[0] != country_code: | ||
324 | 63 | raise orm.except_orm( | ||
325 | 64 | _('Error:'), | ||
326 | 65 | _("The country code inside the file (%s) doesn't " | ||
327 | 66 | "correspond to the selected country (%s).") | ||
328 | 67 | % (row[0], country_code)) | ||
329 | 68 | logger.debug('ZIP = %s - City = %s' % (row[1], row[2])) | ||
330 | 69 | if row[1] and row[2]: | ||
331 | 70 | vals = self._prepare_better_zip( | ||
332 | 71 | cr, uid, row, country_id, states, context=context) | ||
333 | 72 | if vals: | ||
334 | 73 | bzip_id = self.pool['res.better.zip'].create( | ||
335 | 74 | cr, uid, vals, context=context) | ||
336 | 75 | return bzip_id | ||
337 | 76 | |||
338 | 77 | def run_import(self, cr, uid, ids, context=None): | ||
339 | 78 | assert len(ids) == 1, 'Only one ID for the better zip import wizard' | ||
340 | 79 | bzip_obj = self.pool['res.better.zip'] | ||
341 | 80 | wizard = self.browse(cr, uid, ids[0], context=context) | ||
342 | 81 | country_id = wizard.country_id.id | ||
343 | 82 | country_code = wizard.country_id.code.upper() | ||
344 | 83 | url = 'http://download.geonames.org/export/zip/%s.zip' % country_code | ||
345 | 84 | logger.info('Starting to download %s' % url) | ||
346 | 85 | res_request = requests.get(url) | ||
347 | 86 | if res_request.status_code != requests.codes.ok: | ||
348 | 87 | raise orm.except_orm( | ||
349 | 88 | _('Error:'), | ||
350 | 89 | _('Got an error %d when trying to download the file %s.') | ||
351 | 90 | % (res_request.status_code, url)) | ||
352 | 91 | bzip_ids_to_delete = bzip_obj.search( | ||
353 | 92 | cr, uid, [('country_id', '=', country_id)], context=context) | ||
354 | 93 | if bzip_ids_to_delete: | ||
355 | 94 | cr.execute('SELECT id FROM res_better_zip WHERE id in %s ' | ||
356 | 95 | 'FOR UPDATE NOWAIT', (tuple(bzip_ids_to_delete), )) | ||
357 | 96 | bzip_obj.unlink(cr, uid, bzip_ids_to_delete, context=context) | ||
358 | 97 | logger.info( | ||
359 | 98 | '%d better zip entries deleted for country %s' | ||
360 | 99 | % (len(bzip_ids_to_delete), wizard.country_id.name)) | ||
361 | 100 | state_ids = self.pool['res.country.state'].search( | ||
362 | 101 | cr, uid, [('country_id', '=', country_id)], context=context) | ||
363 | 102 | states = {} | ||
364 | 103 | # key = code of the state ; value = ID of the state in OpenERP | ||
365 | 104 | if state_ids: | ||
366 | 105 | states_r = self.pool['res.country.state'].read( | ||
367 | 106 | cr, uid, state_ids, ['code', 'country_id'], context=context) | ||
368 | 107 | for state in states_r: | ||
369 | 108 | states[state['code'].upper()] = state['id'] | ||
370 | 109 | f_geonames = zipfile.ZipFile(StringIO.StringIO(res_request.content)) | ||
371 | 110 | tempdir = tempfile.mkdtemp(prefix='openerp') | ||
372 | 111 | f_geonames.extract('%s.txt' % country_code, tempdir) | ||
373 | 112 | logger.info('The geonames zipfile has been decompressed') | ||
374 | 113 | data_file = open(os.path.join(tempdir, '%s.txt' % country_code), 'r') | ||
375 | 114 | data_file.seek(0) | ||
376 | 115 | logger.info( | ||
377 | 116 | 'Starting to create the better zip entries %s state information' | ||
378 | 117 | % (states and 'with' or 'without')) | ||
379 | 118 | for row in unicodecsv.reader( | ||
380 | 119 | data_file, encoding='utf-8', delimiter=' '): | ||
381 | 120 | self.create_better_zip( | ||
382 | 121 | cr, uid, row, country_id, country_code, states, | ||
383 | 122 | context=context) | ||
384 | 123 | data_file.close() | ||
385 | 124 | logger.info( | ||
386 | 125 | 'The wizard to create better zip entries from geonames ' | ||
387 | 126 | 'has been successfully completed.') | ||
388 | 127 | return True | ||
389 | 0 | 128 | ||
390 | === added file 'base_location_geonames_import/wizard/geonames_import_view.xml' | |||
391 | --- base_location_geonames_import/wizard/geonames_import_view.xml 1970-01-01 00:00:00 +0000 | |||
392 | +++ base_location_geonames_import/wizard/geonames_import_view.xml 2014-06-11 06:33:26 +0000 | |||
393 | @@ -0,0 +1,44 @@ | |||
394 | 1 | <?xml version="1.0" encoding="utf-8"?> | ||
395 | 2 | <!-- | ||
396 | 3 | Copyright (C) 2014 Akretion (http://www.akretion.com/) | ||
397 | 4 | @author: Alexis de Lattre <alexis.delattre@akretion.com> | ||
398 | 5 | The licence is in the file __openerp__.py | ||
399 | 6 | --> | ||
400 | 7 | |||
401 | 8 | <openerp> | ||
402 | 9 | <data> | ||
403 | 10 | |||
404 | 11 | <record id="better_zip_geonames_import_form" model="ir.ui.view"> | ||
405 | 12 | <field name="name">asterisk.server.company</field> | ||
406 | 13 | <field name="model">better.zip.geonames.import</field> | ||
407 | 14 | <field name="arch" type="xml"> | ||
408 | 15 | <form string="Import Geonames" version="7.0"> | ||
409 | 16 | <group name="main"> | ||
410 | 17 | <field name="country_id"/> | ||
411 | 18 | <label string="For the country selected above, this wizard will DELETE ALL THE CURRENT BETTER ZIP ENTRIES, download the latest version of the list of cities from geonames.org and create new better zip entries." | ||
412 | 19 | colspan="2" name="import-help"/> | ||
413 | 20 | </group> | ||
414 | 21 | <footer> | ||
415 | 22 | <button name="run_import" type="object" | ||
416 | 23 | class="oe_highlight" string="Import"/> | ||
417 | 24 | <button special="cancel" string="Cancel" class="oe_link"/> | ||
418 | 25 | </footer> | ||
419 | 26 | </form> | ||
420 | 27 | </field> | ||
421 | 28 | </record> | ||
422 | 29 | |||
423 | 30 | <record id="better_zip_geonames_import_action" model="ir.actions.act_window"> | ||
424 | 31 | <field name="name">Import Geonames</field> | ||
425 | 32 | <field name="res_model">better.zip.geonames.import</field> | ||
426 | 33 | <field name="view_type">form</field> | ||
427 | 34 | <field name="view_mode">form</field> | ||
428 | 35 | <field name="target">new</field> | ||
429 | 36 | </record> | ||
430 | 37 | |||
431 | 38 | <menuitem id="better_zip_geonames_import_menu" | ||
432 | 39 | action="better_zip_geonames_import_action" | ||
433 | 40 | parent="base.menu_localisation" | ||
434 | 41 | sequence="50"/> | ||
435 | 42 | |||
436 | 43 | </data> | ||
437 | 44 | </openerp> |
Hello thanks for the patch.
Is there a good reason not to merge it here: https:/ /launchpad. net/geospatial- addons ?
Some tests would also be a nice addition.