export_template.py
2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- 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