continue
-class SortedMultiQuerySet(MultiQuerySet):
- def __init__(self, *args, **kwargs):
- self.order_by = kwargs.pop('order_by', None)
- self.sortfn = kwargs.pop('sortfn', None)
- if self.order_by is not None:
- self.sortfn = lambda a, b: cmp((getattr(a, f) for f in self.order_by),
- (getattr(b, f) for f in self.order_by))
- super(SortedMultiQuerySet, self).__init__(*args, **kwargs)
-
- def __getitem__(self, item):
- sort_heads = [0] * len(self.querysets)
- try:
- (offset, stop, step) = item.indices(self.count())
- except AttributeError:
- # it's not a slice - make it one
- return self[item:item + 1][0]
- items = []
- total_len = stop - offset
- skipped = 0
- i_s = range(len(sort_heads))
-
- while len(items) < total_len:
- candidate = None
- candidate_i = None
- for i in i_s:
- def get_next():
- return self.querysets[i][sort_heads[i]]
- try:
- if candidate is None:
- candidate = get_next()
- candidate_i = i
- else:
- competitor = get_next()
- if self.sortfn(candidate, competitor) > 0:
- candidate = competitor
- candidate_i = i
- except IndexError:
- continue # continue next sort_head
- # we have no more elements:
- if candidate is None:
- break
- sort_heads[candidate_i] += 1
- if skipped < offset:
- skipped += 1
- continue # continue next item
- items.append(candidate)
-
- return items
-
-
def truncate_html_words(s, num, end_text='...'):
"""Truncates HTML to a certain number of words (not counting tags and
comments). Closes opened tags if they were correctly closed in the given
def status_updated(self):
if self.status == 'COMPLETED':
since = self.schedule.expires_at
- if since is None or since < self.received_at:
- since = self.received_at
+ n = now()
+ if since is None or since < n:
+ since = n
new_exp = self.schedule.plan.get_next_installment(since)
if self.schedule.payed_at is None:
- self.schedule.payed_at = self.received_at
+ self.schedule.payed_at = n
if self.schedule.expires_at is None or self.schedule.expires_at < new_exp:
self.schedule.expires_at = new_exp
self.schedule.save()
lambda f: f[self.POSITION][self.POSITION_INDEX] <= s[self.POSITION][self.POSITION_INDEX] <
f[self.POSITION][self.POSITION_INDEX] + f[self.POSITION][self.POSITION_SPAN], frags))), sect)
- def remove_duplicates(lst, keyfn, compare):
+ def remove_duplicates(lst, keyfn, larger):
els = {}
for e in lst:
eif = keyfn(e)
if eif in els:
- if compare(els[eif], e) >= 1:
+ if larger(els[eif], e):
continue
els[eif] = e
return els.values()
# remove fragments with duplicated fid's and duplicated snippets
- frags = remove_duplicates(frags, lambda f: f[self.FRAGMENT], lambda a, b: cmp(a[self.SCORE], b[self.SCORE]))
- # frags = remove_duplicates(frags, lambda f: f[OTHER]['snippet_pos'] and f[OTHER]['snippet_pos'] or f[FRAGMENT],
- # lambda a, b: cmp(a[SCORE], b[SCORE]))
+ frags = remove_duplicates(frags, lambda f: f[self.FRAGMENT], lambda a, b: a[self.SCORE] > b[self.SCORE])
# remove duplicate sections
sections = {}