Commit 618177cc7cc8d98d02afdde4953ce08590a6a143
1 parent
f99fe23a
optymalizacja eksportu listy leksemów
Showing
2 changed files
with
31 additions
and
13 deletions
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 | |
... | ... |