ajax_table_view.py
6.44 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# -*- 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 {}