Commit 4ac15ea62059a06b7bf423815702b4d7249b8613

Authored by Bartłomiej Nitoń
1 parent 3dc86ac8

Speeded up counting used phraseology bindings.

Showing 1 changed file with 12 additions and 9 deletions
dictionary/ajax_user_stats.py
... ... @@ -22,8 +22,10 @@
22 22  
23 23 """Module covering functions used in user statistics views"""
24 24  
  25 +import operator
  26 +
25 27 from django.contrib.auth.models import User
26   -from django.db.models import Count, Sum
  28 +from django.db.models import Count, Sum, Q
27 29  
28 30 from common.decorators import render, ajax
29 31 from dictionary.models import Lemma, Lemma_Status
... ... @@ -212,22 +214,23 @@ def get_phraseology_stats(user):
212 214 return phraseology_work_stats
213 215  
214 216 def get_used_bindings(added_bindings):
215   - used_bindings = added_bindings
  217 + unused_bindings = []
216 218 for added_binding in added_bindings.all():
217 219 binded_entry = added_binding.binded_entry
218 220 act_binded_lemma = binded_entry.lemmas.get(old=False)
219 221 if act_binded_lemma.status.type.sym_name == 'erase':
220   - used_bindings = used_bindings.exclude(pk=added_binding.pk)
  222 + unused_bindings.append(added_binding.pk)
221 223 else:
222 224 added_frame = added_binding.phraseologic_frame
223   - act_lemma_phras_frames = act_binded_lemma.frames.annotate(positions_count=Count('positions'))\
224   - .filter(phraseologic=True,
225   - positions_count=added_frame.positions.count())
  225 + act_lemma_phras_frames = act_binded_lemma.frames.filter(phraseologic=True)
  226 + act_lemma_phras_frames = act_lemma_phras_frames.annotate(positions_count=Count('positions'))
  227 + act_lemma_phras_frames = act_lemma_phras_frames.filter(positions_count=added_frame.positions.count())
226 228 for pos in added_frame.positions.all():
227 229 act_lemma_phras_frames = act_lemma_phras_frames.filter(positions__text_rep=pos.text_rep)
228   - if not act_lemma_phras_frames.exists():
229   - used_bindings = used_bindings.exclude(pk=added_binding.pk)
230   - return used_bindings
  230 + if not act_lemma_phras_frames.exists():
  231 + unused_bindings.append(added_binding.pk)
  232 + break
  233 + return added_bindings.exclude(pk__in=unused_bindings)
231 234  
232 235 def get_semantics_stats(user):
233 236 earned_cash = RealizedSemantics.objects.filter(user_stats__user=user).aggregate(Sum('cash'))['cash__sum']
... ...