Commit 1fcdb550a64cfced03bae8749b57f881261767b3

Authored by janek37
1 parent a26ced3f

jeszcze mniej zapytań przy przewijaniu leksemów

common/util.py
... ... @@ -88,3 +88,9 @@ def error_messages(form):
88 88 '%s: %s' % (form.fields[k].label, ' '.join(v)) if k != '__all__'
89 89 else ' '.join(v)
90 90 for k, v in form.errors.iteritems())
  91 +
  92 +class GroupDict(dict):
  93 + def add(self, key, value):
  94 + if key not in self:
  95 + self[key] = []
  96 + self[key].append(value)
91 97 \ No newline at end of file
... ...
dictionary/ajax_jqgrid.py
... ... @@ -245,14 +245,14 @@ class JqGridAjax(object):
245 245 return total_pages, start, response_rowcount
246 246  
247 247 @staticmethod
248   - def response_row(instance):
  248 + def response_row(instance, extra=None):
249 249 pass # abstract
250 250  
251 251 @classmethod
252   - def make_response(cls, response_qs, count, page, total_pages):
  252 + def make_response(cls, response_qs, count, page, total_pages, extra=None):
253 253 rows = [{
254 254 'id': instance.pk,
255   - 'cell': cls.response_row(instance),
  255 + 'cell': cls.response_row(instance, extra=extra),
256 256 } for instance in response_qs]
257 257 return {
258 258 'page': page,
... ...
dictionary/ajax_lexeme_jqgrid.py
... ... @@ -2,10 +2,10 @@
2 2  
3 3 from django.utils.simplejson import dumps as json_encode
4 4 from django.db.models import Count
5   -from dictionary.models import Lexeme, filter_visible, visible_vocabularies
  5 +from dictionary.models import Lexeme, filter_visible, visible_vocabularies, LexemeInflectionPattern, LexemeAssociation
6 6 from dictionary.ajax_jqgrid import JqGridAjax, JqGridQuery
7 7 from common.decorators import ajax
8   -from common.util import bisect_left, reverse
  8 +from common.util import bisect_left, reverse, GroupDict
9 9 from django.core.cache import cache
10 10  
11 11 class LexemeGrid(JqGridAjax):
... ... @@ -122,9 +122,14 @@ class LexemeGrid(JqGridAjax):
122 122 return False, None
123 123  
124 124 @staticmethod
125   - def response_row(lexeme):
126   - lip_data = lexeme.lip_data()
127   - cont_vocabs = '/'.join(lexeme.vocabularies.values_list('id', flat=True))
  125 + def response_row(lexeme, extra=None):
  126 + if extra:
  127 + lip_dict, vocab_dict = extra
  128 + lip_data = lexeme.lip_data(lips=lip_dict[lexeme.id])
  129 + cont_vocabs = '/'.join(v.id for v in vocab_dict[lexeme.id])
  130 + else:
  131 + lip_data = lexeme.lip_data()
  132 + cont_vocabs = '/'.join(lexeme.vocabularies.values_list('id', flat=True))
128 133 return [
129 134 lexeme.id,
130 135 lexeme.entry,
... ... @@ -266,4 +271,14 @@ def get_lexemes(request, page, rows, sort_rules, filters=None, mask='',
266 271 sublist = pk_list[start:start + response_rowcount]
267 272 lexemes_dict = dict((l.pk, l) for l in Lexeme.objects.filter(pk__in=sublist))
268 273 lexemes = [lexemes_dict[pk] for pk in sublist]
269   - return LexemeGrid.make_response(lexemes, count, page, total_pages)
  274 + all_lips = LexemeInflectionPattern.objects.filter(lexeme__pk__in=sublist)
  275 + all_lips = all_lips.prefetch_related('pattern', 'inflection_characteristic')
  276 + lip_dict = GroupDict()
  277 + for lip in all_lips:
  278 + lip_dict.add(lip.lexeme_id, lip)
  279 + all_assoc = LexemeAssociation.objects.filter(lexeme__pk__in=sublist)
  280 + vocab_dict = GroupDict()
  281 + for assoc in all_assoc:
  282 + vocab_dict.add(assoc.lexeme_id, assoc.vocabulary)
  283 + return LexemeGrid.make_response(
  284 + lexemes, count, page, total_pages, extra=(lip_dict, vocab_dict))
... ...
dictionary/history.py
1 1 #-*- coding:utf-8 -*-
2 2  
3 3 from django.core.exceptions import ObjectDoesNotExist
  4 +from common.util import GroupDict
4 5 from dictionary.models import Lexeme, LexemeInflectionPattern, \
5 6 Pattern, InflectionCharacteristic, ClassificationValue, Qualifier, History, \
6 7 CrossReferenceType, LexemeAttributeValue
... ... @@ -122,7 +123,7 @@ def prepare_value(table, column, value):
122 123  
123 124 def transaction_table(transaction_data):
124 125 transaction_dict = {}
125   - lips = {}
  126 + lips = GroupDict()
126 127 extra_attributes = {}
127 128 classifications = {}
128 129 qualifiers = []
... ... @@ -216,20 +217,17 @@ def transaction_table(transaction_data):
216 217 prepared = None
217 218 before_after.append(prepared)
218 219 rows.append((attr, tuple(before_after)))
219   - lip_dict = {}
  220 + lip_dict = GroupDict()
220 221 for lip_id, lip_data in lips.iteritems():
221   - lip_dict[lip_id] = []
222 222 for attr in lip_attribute_order:
223 223 if attr in lip_data:
224   - lip_dict[lip_id].append((attr, lip_data[attr]))
  224 + lip_dict.add(lip_id, (attr, lip_data[attr]))
225 225 for q_data in lip_qualifiers.itervalues():
226 226 if q_data: # stare DELETE...
227 227 attr = u'kwalifikator'
228 228 lip_data = q_data['lexemeinflectionpattern_id']
229 229 lip_id = int(lip_data[0] or lip_data[1])
230   - if lip_id not in lips:
231   - lip_dict[lip_id] = []
232   - lip_dict[lip_id].append((attr, q_data['qualifier_id']))
  230 + lip_dict.add(lip_id, (attr, q_data['qualifier_id']))
233 231 lip_tables = []
234 232 for lip_id, lip_data in lip_dict.iteritems():
235 233 lip = LexemeInflectionPattern.all_objects.filter(pk=lip_id)
... ...
dictionary/management/commands/import_morfologik.py
... ... @@ -3,7 +3,7 @@
3 3 from django.core.management.base import BaseCommand
4 4 import sys
5 5 import json
6   -from common.util import suffixes, cut_end, debug
  6 +from common.util import suffixes, cut_end, debug, GroupDict
7 7 from dictionary.models import Pattern, Lexeme, InflectionCharacteristic, \
8 8 Ending, LexicalClass, CrossReference, BaseFormLabel, Vocabulary
9 9 from dictionary.util import expand_tag, compare_patterns
... ... @@ -736,14 +736,12 @@ def filter_patterns(filter, action_name, type, patterns, included, including,
736 736  
737 737 def create_derived(pos, base_forms, forms, patterns):
738 738 tab = {'ger': ('11', u'ie'), 'pact': ('3', u'cy'), 'ppas': ('10', u'y')}
739   - entries = {}
  739 + entries = GroupDict()
740 740 for pattern, root in patterns:
741 741 bfl = tab[pos][0]
742 742 ending = pattern.endings.get(base_form_label__symbol=bfl)
743 743 entry = root + ending.string + tab[pos][1]
744   - if entry not in entries:
745   - entries[entry] = []
746   - entries[entry].append(pattern.name)
  744 + entries.add(entry, pattern.name)
747 745 output = []
748 746 for entry, patterns in entries.iteritems():
749 747 if entry in (form[0] for form in forms):
... ...
dictionary/management/commands/import_resztki.py
... ... @@ -2,7 +2,7 @@
2 2  
3 3 from django.core.management.base import BaseCommand
4 4 import json
5   -from common.util import suffixes, cut_end, debug
  5 +from common.util import suffixes, cut_end, debug, GroupDict
6 6 from dictionary.models import Pattern, Lexeme, InflectionCharacteristic,\
7 7 Ending, LexicalClass, BaseFormLabel, Vocabulary
8 8 from dictionary.pattern_blacklist import blacklist
... ... @@ -402,14 +402,12 @@ def filter_patterns(filter, action_name, type, patterns, included, including,
402 402  
403 403 def create_derived(pos, base_forms, forms, patterns):
404 404 tab = {'ger': ('11', u'ie'), 'pact': ('3', u'cy'), 'ppas': ('10', u'y')}
405   - entries = {}
  405 + entries = GroupDict()
406 406 for pattern, root in patterns:
407 407 bfl = tab[pos][0]
408 408 ending = pattern.endings.get(base_form_label__symbol=bfl)
409 409 entry = root + ending.string + tab[pos][1]
410   - if entry not in entries:
411   - entries[entry] = []
412   - entries[entry].append(pattern.name)
  410 + entries.add(entry, pattern.name)
413 411 output = []
414 412 for entry, patterns in entries.iteritems():
415 413 if entry in forms:
... ...
dictionary/models.py
... ... @@ -2,7 +2,7 @@
2 2  
3 3 from django.db.models import *
4 4 from django.contrib.auth.models import User, Permission
5   -from common.util import no_history
  5 +from common.util import no_history, GroupDict
6 6 from accounts.util import users_with_perm
7 7  
8 8 class NotDeletedManager(Manager):
... ... @@ -367,12 +367,10 @@ class Lexeme(Model):
367 367  
368 368 def inflection_tables(self, variant, qualifiers=None):
369 369 lips = self.lexemeinflectionpattern_set.order_by('index')
370   - ics = {}
  370 + ics = GroupDict()
371 371 for lip in lips:
372 372 ic = lip.inflection_characteristic
373   - if ic not in ics:
374   - ics[ic] = []
375   - ics[ic].append(lip.pattern.name)
  373 + ics.add(ic, lip.pattern.name)
376 374 return [(ic, patterns,
377 375 self.inflection_table(variant, ic, qualifiers=qualifiers))
378 376 for ic, patterns in ics.iteritems()]
... ... @@ -434,8 +432,9 @@ class Lexeme(Model):
434 432 def classification_values(self, classification):
435 433 return self.classificationvalue_set.filter(classification=classification)
436 434  
437   - def lip_data(self):
438   - lips = self.lexemeinflectionpattern_set.all()
  435 + def lip_data(self, lips=None):
  436 + if lips is None:
  437 + lips = self.lexemeinflectionpattern_set.all()
439 438 patterns = []
440 439 ics = []
441 440 for lip in lips:
... ...