11
11
except ImportError :
12
12
import pickle
13
13
14
+ from pympler .asizeof import asizeof
15
+
14
16
import progressbar
15
17
16
18
from django .core .management .base import BaseCommand
17
- from django .db import transaction
19
+ from django .db import transaction , reset_queries
18
20
from django .utils .encoding import force_unicode
19
21
20
22
from ...exceptions import *
@@ -136,6 +138,8 @@ def handle(self, *args, **options):
136
138
elif url in TRANSLATION_SOURCES :
137
139
self .translation_parse (items )
138
140
141
+ reset_queries ()
142
+
139
143
i += 1
140
144
progress .update (i )
141
145
@@ -152,34 +156,34 @@ def handle(self, *args, **options):
152
156
self .logger .info ('Importing parsed translation in the database' )
153
157
self .translation_import ()
154
158
155
- def _get_country (self , code2 ):
159
+ def _get_country_id (self , code2 ):
156
160
'''
157
161
Simple lazy identity map for code2->country
158
162
'''
159
163
if not hasattr (self , '_country_codes' ):
160
164
self ._country_codes = {}
161
165
162
166
if code2 not in self ._country_codes .keys ():
163
- self ._country_codes [code2 ] = Country .objects .get (code2 = code2 )
167
+ self ._country_codes [code2 ] = Country .objects .get (code2 = code2 ). pk
164
168
165
169
return self ._country_codes [code2 ]
166
170
167
- def _get_region (self , country_code2 , region_id ):
171
+ def _get_region_id (self , country_code2 , region_id ):
168
172
'''
169
173
Simple lazy identity map for (country_code2, region_id)->region
170
174
'''
171
175
if not hasattr (self , '_region_codes' ):
172
176
self ._region_codes = {}
173
177
174
- country = self ._get_country (country_code2 )
175
- if country . code2 not in self ._region_codes :
176
- self ._region_codes [country . code2 ] = {}
178
+ country_id = self ._get_country_id (country_code2 )
179
+ if country_id not in self ._region_codes :
180
+ self ._region_codes [country_id ] = {}
177
181
178
- if region_id not in self ._region_codes [country . code2 ]:
179
- self ._region_codes [country . code2 ][region_id ] = Region .objects .get (
180
- country = country , geoname_code = region_id )
182
+ if region_id not in self ._region_codes [country_id ]:
183
+ self ._region_codes [country_id ][region_id ] = Region .objects .get (
184
+ country_id = country_id , geoname_code = region_id ). pk
181
185
182
- return self ._region_codes [country . code2 ][region_id ]
186
+ return self ._region_codes [country_id ][region_id ]
183
187
184
188
def country_import (self , items ):
185
189
try :
@@ -211,14 +215,14 @@ def region_import(self, items):
211
215
212
216
code2 , geoname_code = items [0 ].split ('.' )
213
217
214
- country = self ._get_country (code2 )
218
+ country_id = self ._get_country_id (code2 )
215
219
216
220
if items [3 ]:
217
221
kwargs = dict (geoname_id = items [3 ])
218
222
else :
219
223
try :
220
224
kwargs = dict (name = name ,
221
- country = country )
225
+ country_id = country_id )
222
226
except Country .DoesNotExist :
223
227
if self .noinsert :
224
228
return
@@ -236,7 +240,7 @@ def region_import(self, items):
236
240
region .name = name
237
241
238
242
if not region .country_id :
239
- region .country = country
243
+ region .country_id = country_id
240
244
241
245
if not region .geoname_code :
242
246
region .geoname_code = geoname_code
@@ -254,7 +258,7 @@ def city_import(self, items):
254
258
return
255
259
256
260
try :
257
- country = self ._get_country (items [8 ])
261
+ country_id = self ._get_country_id (items [8 ])
258
262
except Country .DoesNotExist :
259
263
if self .noinsert :
260
264
return
@@ -263,7 +267,7 @@ def city_import(self, items):
263
267
264
268
try :
265
269
kwargs = dict (name = force_unicode (items [1 ]),
266
- country = self ._get_country (items [8 ]))
270
+ country_id = self ._get_country_id (items [8 ]))
267
271
except Country .DoesNotExist :
268
272
if self .noinsert :
269
273
return
@@ -276,17 +280,17 @@ def city_import(self, items):
276
280
try :
277
281
city = City .objects .get (geoname_id = items [0 ])
278
282
city .name = force_unicode (items [1 ])
279
- city .country = self ._get_country (items [8 ])
283
+ city .country_id = self ._get_country_id (items [8 ])
280
284
except City .DoesNotExist :
281
285
if self .noinsert :
282
286
return
283
287
284
288
city = City (** kwargs )
285
289
286
290
save = False
287
- if not city .region :
291
+ if not city .region_id :
288
292
try :
289
- city .region = self ._get_region (items [8 ], items [10 ])
293
+ city .region_id = self ._get_region_id (items [8 ], items [10 ])
290
294
except Region .DoesNotExist :
291
295
pass
292
296
else :
@@ -317,6 +321,10 @@ def city_import(self, items):
317
321
city .save ()
318
322
319
323
def translation_parse (self , items ):
324
+ # free some memory
325
+ self ._country_codes = None
326
+ self ._region_codes = None
327
+
320
328
if not hasattr (self , 'translation_data' ):
321
329
self .country_ids = Country .objects .values_list ('geoname_id' ,
322
330
flat = True )
0 commit comments