util.py 3.95 KB
# -*- coding: utf-8 -*-
from dictionary.models import Gender, Inflection, Lexeme, LexemeAttribute
from dictionary.util import make_filter_url


def pattern_filter_rule(pattern):
    return {
        'field': 'pattern_name',
        'op': 'eq',
        'data': pattern.name
    }


def value_filter_rule(value):
    return {
        'field': 'extra-%s' % value.attribute.id,
        'op': 'in',
        'data': [str(value.id)],
    }


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
    elif pattern.type.inflection_type_id == 'v':
        aspect_attr = LexemeAttribute.objects.get(name='aspekt')
        quasi_attr = LexemeAttribute.objects.get(name='właściwy')
        quasi_value = quasi_attr.values.get(value='Q')
        aspects = aspect_attr.values.all()
        lexeme_ids = list(
            inflections.values_list('lexeme', flat=True).order_by().distinct())
        lexemes = Lexeme.objects.filter(id__in=lexeme_ids)
        for aspect in aspects:
            aspect_lexemes = lexemes.filter(lexemeattributevalue=aspect)
            aspect_count = len(aspect_lexemes)
            if aspect_count > 0:
                example_lexeme = aspect_lexemes.first()
                example_root = example_lexeme.inflection_set.filter(
                    pattern=pattern).first().root
                example = (example_lexeme, example_root)
                quasi_lexemes = aspect_lexemes.filter(
                    lexemeattributevalue=quasi_value)
                quasi_count = len(quasi_lexemes)
                if quasi_count > 0:
                    quasi_lexeme = quasi_lexemes.first()
                    quasi_root = quasi_lexeme.inflection_set.filter(
                        pattern=pattern).first().root
                    quasi_example = (quasi_lexeme, quasi_root)
                else:
                    quasi_example = None
                if filter_urls:
                    filter_url = make_filter_url([
                        pattern_filter_rule(pattern),
                        value_filter_rule(aspect),
                    ], filter_urls == 'reader')
                    quasi_filter_url = make_filter_url([
                        pattern_filter_rule(pattern),
                        value_filter_rule(aspect),
                        value_filter_rule(quasi_value),
                    ], filter_urls == 'reader')
                    extras = (quasi_count, quasi_example, quasi_filter_url)
                    yield (aspect.value, aspect_count, example,
                           filter_url, extras)
                else:
                    yield aspect.value, aspect_count, example
    else:
        return