forms.py 1.35 KB
# -*- 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'])