util.py 2.02 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')