util.py 1.69 KB
# -*- 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