export_template.py 2.96 KB
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from common.util import json_encode, uniprint
from dictionary.models import NewTableTemplate


class Command(BaseCommand):
    help = "Exports a table template to JSON."

    def handle(self, name, variant, *args, **options):
        uniprint(json_encode(export_template(name.decode('utf-8'), variant)))

def export_template(name, v_id):
    tt = NewTableTemplate.objects.get(name=name, variant__id=v_id)
    data = {'name': name, 'variant': v_id}
    if tt.variant.type == 'table':
        data['table_cells'] = []
        data['headers'] = []
    else:
        data['export_cells'] = []
    table_cells = tt.table_cells.select_related(
        'base_form_label').prefetch_related(
        'pattern_types', 'inflection_characteristics', 'attribute_values')
    for tc in table_cells:
        data['table_cells'].append({
            'row': tc.row,
            'col': tc.col,
            'rowspan': tc.rowspan,
            'colspan': tc.colspan,
            'index': tc.index,
            'bfl': tc.base_form_label.symbol,
            'prefix': tc.prefix,
            'suffix': tc.suffix,
            'pattern_types': list(
                tc.pattern_types.values_list('symbol', 'lexical_class_id')),
            'ics': list(
                tc.inflection_characteristics.values_list(
                    'symbol', 'part_of_speech_id')),
            'attr_vals': list(tc.attribute_values.values_list(
                'value', 'attribute__name')),
        })
    export_cells = tt.export_cells.select_related(
        'base_form_label').prefetch_related(
        'pattern_types', 'inflection_characteristics', 'attribute_values')
    for ec in export_cells:
        data['export_cells'].append({
            'bfl': ec.base_form_label.symbol,
            'prefix': ec.prefix,
            'suffix': ec.suffix,
            'tag': ec.tag_template,
            'pattern_types': list(
                ec.pattern_types.values_list('symbol', 'lexical_class_id')),
            'ics': list(
                ec.inflection_characteristics.values_list(
                    'symbol', 'part_of_speech_id')),
            'attr_vals': list(ec.attribute_values.values_list(
                'value', 'attribute__name')),
        })
    headers = tt.headers.prefetch_related(
        'pattern_types', 'inflection_characteristics', 'attribute_values')
    for h in headers:
        data['headers'].append({
            'row': h.row,
            'col': h.col,
            'rowspan': h.rowspan,
            'colspan': h.colspan,
            'label': h.label,
            'css_class': h.css_class,
            'pattern_types': list(
                h.pattern_types.values_list('symbol', 'lexical_class_id')),
            'ics': list(
                h.inflection_characteristics.values_list(
                    'symbol', 'part_of_speech_id')),
            'attr_vals': list(h.attribute_values.values_list(
                'value', 'attribute__name')),
        })
    return data