ajax_table_view.py 6.44 KB
# -*- coding: utf-8 -*-
from common.decorators import render_ajax, ajax
from common.util import FakeQueryset
from dictionary.forms import CellRestrictionsForm, BaseFormLabelForm, CSSClassForm, TemplatePreviewForm
from dictionary.models import NewTableTemplate, InflectionCharacteristic, BaseFormLabel, PatternType, NewTableCell, NewTableHeader, NewExportCell


def tt_params(tt):
    p_types = FakeQueryset(tt.pattern_types.all())
    parts_of_speech = list(tt.parts_of_speech.all())
    color_scheme = parts_of_speech[0].color_scheme
    ics = FakeQueryset(InflectionCharacteristic.objects.filter(
        part_of_speech__in=parts_of_speech))
    bfls = BaseFormLabel.objects.none()
    for pt in p_types:
        bfls |= pt.base_form_labels()
    bfls = FakeQueryset(bfls)
    return bfls, color_scheme, ics, p_types


@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 = []
    bfls, color_scheme, ics, p_types = tt_params(tt)
    table_cells = tt.table_cells.prefetch_related(
        'pattern_types', 'inflection_characteristics')
    export_cells = tt.export_cells.prefetch_related(
        'pattern_types', 'inflection_characteristics').select_related(
        'base_form_label')
    for tc in table_cells:
        prefix = u'tc%s' % tc.id
        bfl_form = BaseFormLabelForm(
            bfls, tc.base_form_label_id, prefix=prefix)
        form_rows.append((
            (tc.row, tc.col, tc.rowspan, tc.colspan, tc.index),
            ('table', bfl_form, tc.prefix, tc.suffix),
            CellRestrictionsForm(
                tc.pattern_types.all(), p_types,
                tc.inflection_characteristics.all(), ics, prefix=prefix),
        ))
    for ec in export_cells:
        prefix = u'ec%s' % ec.id
        bfl_form = BaseFormLabelForm(
            bfls, ec.base_form_label_id, prefix=prefix)
        form_rows.append((
            ec.tag_template,
            ('export', bfl_form, ec.prefix, ec.suffix),
            CellRestrictionsForm(
                ec.pattern_types.all(), p_types,
                ec.inflection_characteristics.all(), ics, prefix=prefix),
            ec.base_form_label.symbol,
        ))

    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)
        ))
    if export_cells:
        form_rows.sort(key=lambda row: (row[3], row[0], row[1][2:]))
        export = True
    else:
        form_rows.sort()
        export = False
    return {
        'form_rows': form_rows,
        'color_scheme': color_scheme,
        'export': export
    }

@render_ajax(template='table_edit_row.html', method='get')
def new_template_row(request, template_id, row_type, num):
    tt = NewTableTemplate.objects.get(id=template_id)
    bfls, color_scheme, ics, p_types = tt_params(tt)
    prefix = 'new%s' % num
    if row_type == 'export':
        coord = ''
    elif row_type == 'table':
        coord = ('', '', '', '', '')
    else:
        coord = ('', '', '', '')
    if row_type != 'header':
        bfl_form = BaseFormLabelForm(bfls, prefix=prefix)
        params = [row_type, bfl_form, '', '']
    else:
        class_form = CSSClassForm(prefix=prefix)
        params = [row_type, '', class_form]
    form = CellRestrictionsForm(p_types, p_types, ics, ics, prefix=prefix)
    return {'coord': coord, 'params': params, 'form': form}

@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}

@ajax(method='post')
def save_template(request, template_id, table_cells, headers, export_cells):
    tt = NewTableTemplate.objects.get(id=template_id)
    tt.table_cells.all().delete()
    tt.headers.all().delete()
    tt.export_cells.all().delete()
    for tc_data in table_cells:
        row, col, rspan, cspan, index, bfl, prefix, suffix, pts, ics = tc_data
        tc = NewTableCell(
            table_template=tt,
            row=row, col=col, rowspan=rspan, colspan=cspan, index=index,
            base_form_label_id=bfl, prefix=prefix, suffix=suffix)
        tc.save()
        for pt_id in pts:
            tc.pattern_types.add(pt_id) #add
        for ic_id in ics:
            tc.inflection_characteristics.add(ic_id) #add
    for h_data in headers:
        row, col, rspan, cspan, label, css_class, pts, ics = h_data
        th = NewTableHeader(
            table_template=tt,
            row=row, col=col, rowspan=rspan, colspan=cspan, label=label,
            css_class=css_class)
        th.save()
        for pt_id in pts:
            th.pattern_types.add(pt_id) #add
        for ic_id in ics:
            th.inflection_characteristics.add(ic_id) #add
    for ec_data in export_cells:
        bfl, prefix, suffix, tag_template, pts, ics = ec_data
        ec = NewExportCell(
            table_template=tt,
            base_form_label_id=bfl, prefix=prefix, suffix=suffix,
            tag_template=tag_template)
        ec.save()
        for pt_id in pts:
            ec.pattern_types.add(pt_id) #add
        for ic_id in ics:
            ec.inflection_characteristics.add(ic_id) #add
    return {}