views.py 1.77 KB
# -*- coding: utf-8 -*-
from django.shortcuts import render

from common.decorators import render_ajax, AjaxError
from dictionary.models import Lexeme, PartOfSpeech, Gender, Inflection, \
    Qualifier, LexemeAttributeValue
from tables.util import prepare_table
from patterns.models import Pattern


@render_ajax(template='table_preview.html', method='get')
def table_preview(request, lexeme_id, inflection_id, pattern, attr_data=None,
                  gender=None, entry=None, pos=None):
    lexeme = Lexeme.all_objects.get(pk=lexeme_id)
    if not lexeme.perm(request.user, 'view'):
        raise AjaxError('access denied')
    if pos is not None:
        part_of_speech = PartOfSpeech.objects.get(symbol=pos)
    else:
        part_of_speech = lexeme.part_of_speech
    try:
        if entry is not None:
            lexeme.entry = entry
        pattern = Pattern.objects.get(name=pattern)
        gender = Gender.objects.get(id=gender) if gender else None
        if attr_data:
            attr_vals = LexemeAttributeValue.objects.filter(
                id__in=attr_data)
        else:
            attr_vals = None
        if inflection_id.startswith('inf_add'):
            inflection = Inflection(lexeme=lexeme, index=0)
        else:
            inflection = Inflection.objects.get(pk=int(inflection_id[3:]))
        inflection.pattern = pattern
        inflection.gender = gender
        inflection.root = inflection.get_root()
        qualifiers = Qualifier.visible_qualifiers(request.user)
        table = inflection.table(
            '0', separated=True, qualifiers=qualifiers, edit_view=True,
            attr_vals=attr_vals, pos=part_of_speech)
        prepare_table(table)
    except Pattern.DoesNotExist:
        table = None
    return {'table': table, 'color_scheme': part_of_speech.color_scheme}