Commit 618177cc7cc8d98d02afdde4953ce08590a6a143

Authored by janek37
1 parent f99fe23a

optymalizacja eksportu listy leksemów

dictionary/ajax_lexeme_slickgrid.py
... ... @@ -90,19 +90,24 @@ class LexemeQuery(SlickGridQuery):
90 90 return super(LexemeQuery, self).filter_from(
91 91 queryset, from_value, upward)
92 92  
93   - def response_row(self, lexeme):
94   - lip_data = lexeme.lip_data()
95   - cont_vocabs = '/'.join(v.id for v in lexeme.vocabularies.all())
96   - return {
97   - 'id': lexeme.id,
98   - 'entry': lexeme.entry,
99   - 'pos': lexeme.part_of_speech.symbol,
100   - 'patterns': lip_data['patterns'],
101   - 'genders': lip_data['genders'],
102   - 'vocabs': cont_vocabs,
103   - 'owner': lexeme.owner_vocabulary.id,
104   - 'status': dict(Lexeme.STATUS_CHOICES).get(lexeme.status),
  93 + def response_row(self, lexeme, columns=None):
  94 + if columns is None:
  95 + columns = (
  96 + 'id', 'entry', 'pos', 'patterns', 'genders', 'vocabs',
  97 + 'owner', 'status')
  98 + if 'patterns' in columns or 'genders' in columns:
  99 + lip_data = lexeme.lip_data()
  100 + lazy_response = {
  101 + 'id': lambda: lexeme.id,
  102 + 'entry': lambda: lexeme.entry,
  103 + 'pos': lambda: lexeme.part_of_speech.symbol,
  104 + 'patterns': lambda: lip_data['patterns'],
  105 + 'genders': lambda: lip_data['genders'],
  106 + 'vocabs': lambda: '/'.join(v.id for v in lexeme.vocabularies.all()),
  107 + 'owner': lambda: lexeme.owner_vocabulary.id,
  108 + 'status': lambda: dict(Lexeme.STATUS_CHOICES).get(lexeme.status),
105 109 }
  110 + return dict((column, lazy_response[column]()) for column in columns)
106 111  
107 112 # indeks wiersza w danym sortowaniu, w którym
108 113 # znajdzie się rekord o danym id
... ... @@ -174,8 +179,20 @@ class LexemeQuery(SlickGridQuery):
174 179  
175 180 def export_list(self, columns, output_file):
176 181 lexemes = Lexeme.objects.filter(id__in=self.get_id_list())
  182 + if 'pos' in columns:
  183 + lexemes = lexemes.select_related('part_of_speech')
  184 + if 'patterns' in columns:
  185 + lexemes = lexemes.prefetch_related(
  186 + 'lexemeinflectionpattern_set__pattern')
  187 + if 'genders' in columns:
  188 + lexemes = lexemes.prefetch_related(
  189 + 'lexemeinflectionpattern_set__gender')
  190 + if 'vocabs' in columns:
  191 + lexemes = lexemes.prefetch_related('vocabularies')
  192 + if 'owner' in columns:
  193 + lexemes = lexemes.select_related('owner_vocabulary')
177 194 for lexeme in prefetch(lexemes):
178   - row = self.response_row(lexeme)
  195 + row = self.response_row(lexeme, columns)
179 196 print >>output_file, '\t'.join(
180 197 unicode(row[column]) for column in columns)
181 198  
... ...
dictionary/models.py
... ... @@ -302,6 +302,7 @@ class PatternType(Model):
302 302  
303 303 class Meta:
304 304 db_table = 'typywzorow'
  305 + ordering = ['symbol']
305 306  
306 307  
307 308 class Pattern(Model):
... ...