ajax_table_view.py 3.69 KB
# -*- coding: utf-8 -*-
from common.decorators import render_ajax
from dictionary.forms import CellRestrictionsForm, BaseFormLabelForm, CSSClassForm, TemplatePreviewForm
from dictionary.models import NewTableTemplate, InflectionCharacteristic, BaseFormLabel, PatternType


@render_ajax(template='table_edit_form.html', method='get')
def table_edit_form(request, template_id):
    tt = NewTableTemplate.objects.get(id=template_id)
    # potrzebne są zestawy: współrzędne, dane o formie, formularze warunków
    # dla nagłówków: współrzędne, etykieta, klasa, formularze warunków
    # i współrzędnych w tabeli
    # i dodawania nowych form (prefiks, efobaz, sufiks)
    form_rows = []
    p_types = tt.pattern_types.all()
    parts_of_speech = list(tt.parts_of_speech.all())
    color_scheme = parts_of_speech[0].color_scheme
    ics = InflectionCharacteristic.objects.filter(
        part_of_speech__in=parts_of_speech)
    bfls = BaseFormLabel.objects.none()
    for pt in p_types:
        bfls |= pt.base_form_labels()
    cells = tt.cells.prefetch_related(
        'table_cells__pattern_types', 'table_cells__inflection_characteristics',
        'export_cells__pattern_types',
        'export_cells__inflection_characteristics')
    for cell in cells:
        for tc in cell.table_cells.all():
            prefix = u'tc%s' % tc.id
            bfl_form = BaseFormLabelForm(
                bfls, cell.base_form_label_id, prefix=prefix)
            form_rows.append((
                (tc.row, tc.col, tc.rowspan, tc.colspan, tc.index),
                ('table', bfl_form, cell.prefix, cell.suffix),
                CellRestrictionsForm(
                    tc.pattern_types.all(), p_types,
                    tc.inflection_characteristics.all(), ics, prefix=prefix),
            ))
        for ec in cell.export_cells.all():
            prefix = u'ec%s' % ec.id
            bfl_form = BaseFormLabelForm(
                bfls, cell.base_form_label_id, prefix=prefix)
            form_rows.append((
                ec.tag_template,
                ('export', bfl_form, cell.prefix, cell.suffix),
                CellRestrictionsForm(
                    ec.pattern_types.all(), p_types,
                    ec.inflection_characteristics.all(), ics, prefix=prefix),
            ))

    headers = tt.headers.prefetch_related(
        'pattern_types', 'inflection_characteristics')
    for header in headers:
        prefix = u'th%s' % header.id
        class_form = CSSClassForm(
            header.css_class, prefix=prefix)
        form_rows.append((
            (header.row, header.col, header.rowspan, header.colspan),
            ('header', header.label, class_form),
            CellRestrictionsForm(
                header.pattern_types.all(), p_types,
                header.inflection_characteristics.all(), ics, prefix=prefix)
        ))
    return {'form_rows': sorted(form_rows), 'color_scheme': color_scheme}

@render_ajax(template='template_preview_form.html', method='get')
def template_preview_form(request, template_id):
    tt = NewTableTemplate.objects.get(id=template_id)
    p_types = tt.pattern_types.all()
    parts_of_speech = list(tt.parts_of_speech.all())
    ics = InflectionCharacteristic.objects.filter(
        part_of_speech__in=parts_of_speech)
    form = TemplatePreviewForm(p_types, ics)
    return {'form': form}

@render_ajax(template='template_preview_table.html', method='get')
def template_preview(request, template_id, pattern_type_id, ic_id):
    tt = NewTableTemplate.objects.get(id=template_id)
    pt = PatternType.objects.get(id=pattern_type_id)
    ic = InflectionCharacteristic.objects.get(id=ic_id)
    table = tt.render_with_pattern_type(pt, ic)
    return {'table': table, 'color_scheme': ic.part_of_speech.color_scheme}