7
7
import logging
8
8
from argparse import RawTextHelpFormatter
9
9
import sys
10
+
10
11
if sys .platform != 'win32' :
11
12
import resource
12
13
@@ -71,34 +72,40 @@ def create_parser(self, *args, **kwargs):
71
72
return parser
72
73
73
74
def add_arguments (self , parser ):
74
- parser .add_argument ('--force-import-all' , action = 'store_true' ,
75
+ parser .add_argument (
76
+ '--force-import-all' , action = 'store_true' ,
75
77
default = False , help = 'Import even if files are up-to-date.'
76
78
),
77
- parser .add_argument ('--force-all' , action = 'store_true' , default = False ,
79
+ parser .add_argument (
80
+ '--force-all' , action = 'store_true' , default = False ,
78
81
help = 'Download and import if files are up-to-date.'
79
82
),
80
- parser .add_argument ('--force-import' , action = 'append' , default = [],
83
+ parser .add_argument (
84
+ '--force-import' , action = 'append' , default = [],
81
85
help = 'Import even if files matching files are up-to-date'
82
86
),
83
- parser .add_argument ('--force' , action = 'append' , default = [],
87
+ parser .add_argument (
88
+ '--force' , action = 'append' , default = [],
84
89
help = 'Download and import even if matching files are up-to-date'
85
90
),
86
91
parser .add_argument ('--noinsert' , action = 'store_true' ,
87
- default = False ,
88
- help = 'Update existing data only'
89
- ),
90
- parser .add_argument ('--hack-translations' , action = 'store_true' ,
92
+ default = False ,
93
+ help = 'Update existing data only'
94
+ ),
95
+ parser .add_argument (
96
+ '--hack-translations' , action = 'store_true' ,
91
97
default = False ,
92
98
help = 'Set this if you intend to import translations a lot'
93
99
),
94
- parser .add_argument ('--keep-slugs' , action = 'store_true' ,
100
+ parser .add_argument (
101
+ '--keep-slugs' , action = 'store_true' ,
95
102
default = False ,
96
103
help = 'Do not update slugs'
97
104
),
98
105
parser .add_argument ('--progress' , action = 'store_true' ,
99
- default = False ,
100
- help = 'Show progress bar'
101
- ),
106
+ default = False ,
107
+ help = 'Show progress bar'
108
+ ),
102
109
103
110
def progress_init (self ):
104
111
"""Initialize progress bar."""
@@ -281,10 +288,12 @@ def _get_subregion_id(self, country_code2, region_id, subregion_id):
281
288
if region_id not in self ._region_codes [country_id ]:
282
289
self ._region_codes [country_id ][region_id ] = Region .objects .get (
283
290
country_id = country_id , geoname_code = region_id ).pk
291
+
284
292
if subregion_id not in self ._subregion_codes [country_id ]:
285
293
self ._subregion_codes [country_id ][subregion_id ] = \
286
294
SubRegion .objects .get (
287
- country_id = country_id , geoname_code = subregion_id ).pk
295
+ region_id = self ._region_codes [country_id ][region_id ],
296
+ geoname_code = subregion_id ).pk
288
297
return self ._subregion_codes [country_id ][subregion_id ]
289
298
290
299
def country_import (self , items ):
@@ -293,9 +302,10 @@ def country_import(self, items):
293
302
except InvalidItems :
294
303
return
295
304
305
+ force_insert = False
306
+ force_update = False
296
307
try :
297
- force_insert = False
298
- force_update = False
308
+
299
309
country = Country .objects .get (geoname_id = items [ICountry .geonameid ])
300
310
force_update = True
301
311
except Country .DoesNotExist :
@@ -336,9 +346,9 @@ def region_import(self, items):
336
346
except InvalidItems :
337
347
return
338
348
349
+ force_insert = False
350
+ force_update = False
339
351
try :
340
- force_insert = False
341
- force_update = False
342
352
region = Region .objects .get (geoname_id = items [IRegion .geonameid ])
343
353
force_update = True
344
354
except Region .DoesNotExist :
@@ -393,8 +403,8 @@ def subregion_import(self, items):
393
403
except InvalidItems :
394
404
return
395
405
406
+ force_insert = force_update = False
396
407
try :
397
- force_insert = force_update = False
398
408
subregion = SubRegion .objects .filter (
399
409
geoname_id = items [ISubRegion .geonameid ]).first ()
400
410
if subregion :
@@ -471,9 +481,9 @@ def city_import(self, items):
471
481
except InvalidItems :
472
482
return
473
483
484
+ force_insert = False
485
+ force_update = False
474
486
try :
475
- force_insert = False
476
- force_update = False
477
487
city = City .objects .get (geoname_id = items [ICity .geonameid ])
478
488
force_update = True
479
489
except City .DoesNotExist :
@@ -504,7 +514,7 @@ def city_import(self, items):
504
514
items [ICity .admin1Code ],
505
515
items [ICity .admin2Code ]
506
516
)
507
- except SubRegion .DoesNotExist :
517
+ except ( SubRegion .DoesNotExist , Region . DoesNotExist ) :
508
518
subregion_id = None
509
519
510
520
save = False
0 commit comments