util.py
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
from dictionary.models import Gender, Inflection, Lexeme
from dictionary.util import make_filter_url
def pattern_filter_rule(pattern):
return {
'field': 'pattern_name',
'op': 'eq',
'data': pattern.name
}
def get_detailed_counts(pattern, inflections, filter_urls=None):
"""
:param pattern:
:param inflections:
:param filter_urls: possible values: None, 'reader', 'edit'
"""
if pattern.type.inflection_type_id == 'subst':
for gender in Gender.objects.all():
gender_count = Inflection.lexeme_count(
inflections.filter(gender=gender))
if gender_count > 0:
example = pattern.get_example(gender)
if example:
example_qs = Lexeme.objects.filter(id=example[0].id)
if not Lexeme.filter_reader(example_qs):
example = pattern.get_example(gender, refresh=True)
if filter_urls:
filter_url = make_filter_url([
pattern_filter_rule(pattern),
{
'field': 'gender',
'op': 'in',
'data': [str(gender.id)],
}
], filter_urls == 'reader')
else:
filter_url = None
else:
filter_url = None
if filter_urls:
yield gender.symbol, gender_count, example, filter_url
else:
yield gender.symbol, gender_count, example
else:
return