forms.py
1.35 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
# -*- coding: utf-8 -*-
from django.forms import ModelForm, Form, SelectMultiple
from django.forms.widgets import CheckboxSelectMultiple
from common.forms import disable_field
from dictionary.forms import QualifiersField
from dictionary.models import Qualifier
from patterns.models import PatternType, Pattern
class PatternEditForm(ModelForm):
def __init__(self, editable=True, **kwargs):
super(PatternEditForm, self).__init__(**kwargs)
self.fields['type'].choices = PatternType.options()
if not editable:
instance = getattr(self, 'instance', None)
if instance and instance.id:
for name, field in self.fields.iteritems():
disable_field(field)
class Meta:
model = Pattern
fields = (
'type', 'name', 'example', 'basic_form_ending', 'status', 'comment')
class QualifierForm(Form):
qualifiers = QualifiersField(
required=False, label=u'',
widget=SelectMultiple(attrs={'class': 'qualifiers'}))
def __init__(self, user, qualified=None, editable=True,
**kwargs):
super(QualifierForm, self).__init__(**kwargs)
self.fields['qualifiers'].set_qualifiers(
user, qualified, types=[Qualifier.TYPE_STYLE, Qualifier.TYPE_FORM])
if not editable:
disable_field(self.fields['qualifiers'])