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

from common.decorators import render_ajax, AjaxError
from dictionary.models import Lexeme, PartOfSpeech, Gender, \
    LexemeAttributeValue, LexemeInflectionPattern, Qualifier
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, lip_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 lip_id.startswith('lip_add'):
            lip = LexemeInflectionPattern(lexeme=lexeme, index=0)
        else:
            lip = LexemeInflectionPattern.objects.get(pk=int(lip_id[3:]))
        lip.pattern = pattern
        lip.gender = gender
        lip.root = lip.get_root()
        qualifiers = Qualifier.visible_qualifiers(request.user)
        table = lip.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}