ajax_table_view.py
3.69 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
# -*- coding: utf-8 -*-
from common.decorators import render_ajax
from dictionary.forms import CellRestrictionsForm, BaseFormLabelForm, CSSClassForm, TemplatePreviewForm
from dictionary.models import NewTableTemplate, InflectionCharacteristic, BaseFormLabel, PatternType
@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 = []
p_types = tt.pattern_types.all()
parts_of_speech = list(tt.parts_of_speech.all())
color_scheme = parts_of_speech[0].color_scheme
ics = InflectionCharacteristic.objects.filter(
part_of_speech__in=parts_of_speech)
bfls = BaseFormLabel.objects.none()
for pt in p_types:
bfls |= pt.base_form_labels()
cells = tt.cells.prefetch_related(
'table_cells__pattern_types', 'table_cells__inflection_characteristics',
'export_cells__pattern_types',
'export_cells__inflection_characteristics')
for cell in cells:
for tc in cell.table_cells.all():
prefix = u'tc%s' % tc.id
bfl_form = BaseFormLabelForm(
bfls, cell.base_form_label_id, prefix=prefix)
form_rows.append((
(tc.row, tc.col, tc.rowspan, tc.colspan, tc.index),
('table', bfl_form, cell.prefix, cell.suffix),
CellRestrictionsForm(
tc.pattern_types.all(), p_types,
tc.inflection_characteristics.all(), ics, prefix=prefix),
))
for ec in cell.export_cells.all():
prefix = u'ec%s' % ec.id
bfl_form = BaseFormLabelForm(
bfls, cell.base_form_label_id, prefix=prefix)
form_rows.append((
ec.tag_template,
('export', bfl_form, cell.prefix, cell.suffix),
CellRestrictionsForm(
ec.pattern_types.all(), p_types,
ec.inflection_characteristics.all(), ics, prefix=prefix),
))
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)
))
return {'form_rows': sorted(form_rows), 'color_scheme': color_scheme}
@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}