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


def tt_params(tt):
    pt_queryset = tt.pattern_types.all()
    p_types = FakeQueryset(pt_queryset)
    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)
    tt_attrs = tt.cell_attributes.prefetch_related('values')
    def cr_form(x):
        return CellRestrictionsForm(
            x.pattern_types.all(), p_types,
            x.inflection_characteristics.all(), ics, prefix=prefix)
    def attr_forms(x):
        vals = x.attribute_values.all()
        return [
            AttributeValuesForm(
                attr, [v for v in vals if v.attribute == attr],
                prefix='%s_%s' % (prefix, attr.id))
            for attr in tt_attrs]

    form_rows = []
    bfls, color_scheme, ics, p_types = tt_params(tt)
    table_cells = tt.table_cells.prefetch_related(
        'pattern_types', 'inflection_characteristics', 'attribute_values')
    export_cells = tt.export_cells.prefetch_related(
        'pattern_types', 'inflection_characteristics').select_related(
        'base_form_label', 'attribute_values')
    headers = tt.headers.prefetch_related(
        'pattern_types', 'inflection_characteristics', 'attribute_values')
    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),
            cr_form(tc),
            attr_forms(tc),
        ))
    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),
            cr_form(ec),
            attr_forms(ec),
            ec.base_form_label.symbol,
        ))
    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),
            cr_form(header),
            attr_forms(header),
        ))
    if export_cells:
        form_rows.sort(key=lambda row: (row[-1], row[0], row[1][2:]))
        export = True
    else:
        form_rows.sort()
        export = False
    return {
        'form_rows': form_rows,
        'color_scheme': color_scheme,
        'export': export,
        'template_attrs': tt_attrs,
    }

@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,
        'cr_form': form,
        'attr_forms': [
            AttributeValuesForm(attr) for attr in tt.cell_attributes.all()]
    }

@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):
    def add_restrictions(x):
        for pt_id in pts:
            x.pattern_types.add(pt_id) #add
        for ic_id in ics:
            x.inflection_characteristics.add(ic_id) #add
        for av in attr_vals:
            x.attribute_values.add(av)

    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, \
            attr_vals = 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()
        add_restrictions(tc)
    for h_data in headers:
        row, col, rspan, cspan, label, css_class, pts, ics, attr_vals = h_data
        th = NewTableHeader(
            table_template=tt,
            row=row, col=col, rowspan=rspan, colspan=cspan, label=label,
            css_class=css_class)
        th.save()
        add_restrictions(th)
    for ec_data in export_cells:
        bfl, prefix, suffix, tag_template, pts, ics, attr_vals = ec_data
        ec = NewExportCell(
            table_template=tt,
            base_form_label_id=bfl, prefix=prefix, suffix=suffix,
            tag_template=tag_template)
        ec.save()
        add_restrictions(ec)
    return {}