forms.py
8.83 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: utf-8 -*-
from django.core.exceptions import ValidationError
from django.forms import ModelForm, HiddenInput, Form, ModelChoiceField, \
ModelMultipleChoiceField, SelectMultiple, Select, ChoiceField
from django.forms.widgets import Textarea
from django.utils.translation import ugettext_lazy as _
from common.forms import hidden_id
from common.models import FlatPage
from common.util import GroupDict
from dictionary.models import Classification, ClassificationValue, Qualifier, \
QualifierExclusionClass, Lexeme, LexemeInflectionPattern, Ending, \
Vocabulary, TableTemplate, PatternType, Gender, LexemeAttributeValue, \
BaseFormLabel, MultilingualText
CSS_CLASS_CHOICES = (
('header', _(u'head.')),
('header-c', _(u'cent.')),
('top', _(u'top')),
('bottom', _(u'bottom')),
('left', _(u'left')),
('right', _(u'right')),
('data', _(u'data')),
('blank', _(u'blank')),
)
class AddClassificationForm(ModelForm):
det = hidden_id('classification_form')
def save(self, *args, **kwargs):
cl = super(AddClassificationForm, self)
multi_name, created = MultilingualText.objects.get_or_create(
text_id=cl.name)
multi_name.add_translated_texts()
cl.multilingual_name = multi_name
cl.save()
class Meta:
model = Classification
class AddClassificationValueForm(ModelForm):
det = hidden_id('value_form')
def __init__(self, classification=None, **kwargs):
super(AddClassificationValueForm, self).__init__(**kwargs)
if classification is not None:
parent_choices = (
[('', _(u'<none>'))] + classification.make_choices())
self.fields['parent_node'].choices = parent_choices
self.fields['classification'].initial = classification.pk
class Meta:
model = ClassificationValue
fields = ['label', 'parent_node', 'classification']
widgets = {
'classification': HiddenInput(),
}
class AddExclusionClassForm(ModelForm):
det = hidden_id('add_exclusion_class')
def __init__(self, vocabulary=None, **kwargs):
super(AddExclusionClassForm, self).__init__(**kwargs)
self.fields['vocabulary'].initial = vocabulary
class Meta:
model = QualifierExclusionClass
widgets = {
'vocabulary': HiddenInput(),
}
class ExclusionClassForm(Form):
det = hidden_id('remove_exclusion_class')
ec = ModelChoiceField(
queryset=QualifierExclusionClass.objects.all(), label=u'') # dziura
def __init__(self, queryset=None, **kwargs):
super(ExclusionClassForm, self).__init__(**kwargs)
if queryset:
self.fields['ec'].queryset = queryset
class AddQualifierForm(ModelForm):
det = hidden_id('add_qualifier')
def __init__(self, vocabulary=None, **kwargs):
super(AddQualifierForm, self).__init__(**kwargs)
self.fields['vocabulary'].initial = vocabulary
if vocabulary:
self.fields['exclusion_class'].queryset = (
QualifierExclusionClass.objects
.filter(vocabulary=vocabulary).distinct())
def clean(self):
cleaned_data = self.cleaned_data
ec = cleaned_data['exclusion_class']
if ec:
ec_vocab = ec.vocabulary
if ec_vocab and ec_vocab != cleaned_data['vocabulary']:
raise ValidationError(
_(u'Exclusion class doesn\'t match the dictionary'))
return cleaned_data
class Meta:
model = Qualifier
fields = ['label', 'exclusion_class', 'vocabulary', 'type']
widgets = {
'vocabulary': HiddenInput(),
}
class ChangeClassForm(ModelForm):
def __init__(self, **kwargs):
super(ChangeClassForm, self).__init__(**kwargs)
exclusion_classes = (
self.instance.vocabulary.qualifierexclusionclass_set.all())
self.fields['exclusion_class'].queryset = exclusion_classes
def clean(self):
ec = self.cleaned_data['exclusion_class']
q = self.instance
if ec:
ec_qualifiers = ec.qualifier_set.all()
if Lexeme.objects.filter(qualifiers=q).filter(
qualifiers__in=ec_qualifiers):
raise ValidationError(_(u'Collision in the exclusion class'))
if LexemeInflectionPattern.objects.filter(
qualifiers=q).filter(qualifiers__in=ec_qualifiers):
raise ValidationError(_(u'Collision in the exclusion class'))
if Ending.objects.filter(qualifiers=q).filter(
qualifiers__in=ec_qualifiers):
raise ValidationError(_(u'Collision in the exclusion class'))
return self.cleaned_data
class Meta:
model = Qualifier
fields = ['exclusion_class', 'type']
class VocabularyForm(Form):
det = hidden_id('vocabulary_form')
vocabulary = ModelChoiceField(
label=_(u"add dictionary"), queryset=Vocabulary.objects.none())
classification = ModelChoiceField(
widget=HiddenInput(), queryset=Classification.objects.all())
def __init__(self, queryset, classification=None, **kwargs):
super(VocabularyForm, self).__init__(**kwargs)
self.fields['vocabulary'].queryset = queryset
if classification:
self.fields['classification'].initial = classification
class TemplateChoiceForm(Form):
table_template = ModelChoiceField(
TableTemplate.objects.none(), label=_(u'Table template'))
def __init__(self, **kwargs):
super(TemplateChoiceForm, self).__init__(**kwargs)
choices = GroupDict()
template_field = self.fields['table_template']
for tt in TableTemplate.objects.order_by('variant', 'name'):
choices.add(tt.variant.id, (tt.id, tt))
template_field.choices = sorted(choices.iteritems())
class CellRestrictionsForm(Form):
pattern_types = ModelMultipleChoiceField(
queryset=PatternType.objects.none(), label=u'',
widget=SelectMultiple(attrs={'class': 'pattern-types-select'}))
# wybrane/dostępne typy wzorów
def __init__(self, sel_pt, av_pt, **kwargs):
super(CellRestrictionsForm, self).__init__(**kwargs)
self.fields['pattern_types'].queryset = av_pt
self.fields['pattern_types'].initial = sel_pt
class GenderForm(Form):
genders = ModelMultipleChoiceField(
queryset=Gender.objects.all(), label=u'',
widget=SelectMultiple(attrs={'class': 'gender-select'}))
def __init__(self, initial=None, **kwargs):
super(GenderForm, self).__init__(**kwargs)
if initial:
self.fields['genders'].initial = initial
else:
self.fields['genders'].initial = self.fields['genders'].queryset
class AttributeValuesForm(Form):
values = ModelMultipleChoiceField(
queryset=LexemeAttributeValue.objects.none(),
widget=SelectMultiple(attrs={'class': 'attrs-select'}))
def __init__(self, attribute, initial=None, **kwargs):
assert attribute.closed and not attribute.multiple
super(AttributeValuesForm, self).__init__(**kwargs)
self.fields['values'].label = attribute.name
self.fields['values'].queryset = attribute.values
if initial is None:
initial = attribute.values.all()
self.fields['values'].initial = initial
class BaseFormLabelForm(Form):
base_form_label = ModelChoiceField(
queryset=BaseFormLabel.objects.none(), label=u'',
widget=Select(attrs={'class': 'bfl-select'}))
def __init__(self, bfls, initial=None, **kwargs):
super(BaseFormLabelForm, self).__init__(**kwargs)
self.fields['base_form_label'].queryset = bfls
self.fields['base_form_label'].initial = initial
class CSSClassForm(Form):
css_class = ChoiceField(
choices=CSS_CLASS_CHOICES, label=_(u'class'),
widget=Select(attrs={'class': 'css-class-select'}))
def __init__(self, initial=None, **kwargs):
super(CSSClassForm, self).__init__(**kwargs)
self.fields['css_class'].initial = initial
class TemplatePreviewForm(Form):
pattern_type = ModelChoiceField(
queryset=PatternType.objects.none(), label=_(u'pattern type'),
widget=Select(attrs={'class': 'pattern-type-select'}))
def __init__(self, pattern_types, **kwargs):
super(TemplatePreviewForm, self).__init__(**kwargs)
self.fields['pattern_type'].queryset = pattern_types
class TemplatePreviewGenderForm(Form):
gender = ModelChoiceField(
queryset=Gender.objects.all(), label=_(u'gender'),
widget=Select(attrs={'class': 'gender-select'}))
class FlatPageForm(ModelForm):
class Meta:
model = FlatPage
exclude = ['text_id']
labels = {'html_content': ''}
widgets = {
'html_content': Textarea(
attrs={'style': 'width: 700px; height: 1000px;'}),
}