util.py 3.2 KB
# -*- coding: utf-8 -*-
from common.decorators import AjaxError

from patterns.models import BaseFormLabel


def expand_tag(tag):
    if '+' in tag:
        tags = tag.split('+')
        expanded = []
        for tag in tags:
            expanded += expand_tag(tag)
        return expanded
    parts = tag.split(':')
    variants_list = []
    for part in parts:
        variants_list.append(part.split('.'))
        # return [':'.join(parts) for parts in expand_variants(variants_list)]
    return expand_variants(variants_list)


def expand_variants(variants_list):
    if not variants_list:
        return [()]
    expanded = []
    tails = expand_variants(variants_list[1:])
    for variant in variants_list[0]:
        expanded += [(variant,) + tail for tail in tails]
    return expanded


def compare_patterns(patterns):
    pattern0, root0 = patterns[0]
    bfl_pks0 = set(pattern0.endings.values_list('base_form_label', flat=True))
    differing_bfls = set()
    forms = dict((p, dict(
        (bfl, root + e)
        for bfl, e in p.endings.values_list('base_form_label', 'string')))
        for p, root in patterns)
    for pattern, root in patterns[1:]:
        bfl_pks = set(pattern.endings.values_list('base_form_label', flat=True))
        for pk in bfl_pks ^ bfl_pks0:  # różnica symetryczna
            differing_bfls.add(pk)
        for ending in pattern.endings.all():
            if ending.base_form_label.pk not in differing_bfls:
                if root + ending.string != forms[pattern0][
                        ending.base_form_label.pk]:
                    differing_bfls.add(ending.base_form_label.pk)
    differing_bfls = list(BaseFormLabel.objects.filter(pk__in=differing_bfls))
    diffs = dict((pattern, [
        (bfl, forms[pattern].get(bfl.pk))
        for bfl in differing_bfls])
        for pattern, root in patterns)
    return diffs


def check_query_params(request, query_params):
    reader = query_params.get('reader', True)
    if not request.user.is_authenticated() and not reader:
        raise AjaxError('access denied')


def prepare_table(table):
    for row in table:
        for cell in row:
            if type(cell) == dict and 'forms' in cell:
                cell['forms'].sort()
                seen_forms = []
                unique_forms = []
                form_patterns = {}
                for form in cell['forms']:
                    if form[1] not in seen_forms:
                        seen_forms.append(form[1])
                        unique_forms.append(form)
                        form_patterns[form[1]] = set()
                    form_patterns[form[1]].add(form[3])
                cell['forms'] = [
                    {
                        'form': form,
                        'qualifiers': qualifiers,
                        'patterns': form_patterns[form],
                    }
                    for (key, form, qualifiers, pattern) in unique_forms]
            elif type(cell) == dict and 'label' in cell:
                seen_labels = []

                def is_new(label):
                    new = label not in seen_labels
                    seen_labels.append(label)
                    return new

                cell['label'] = filter(is_new, cell['label'])
    return table