示例#1
0
    def test_stacked_config(self):
        form = RegistrationForm()
        config = FormConfig()

        config.push()
        config.configure(
            'widget',
            widgets.Textarea(),
            filter=ConfigFilter("CharField"),
        )

        config.push()
        config.configure(
            'widget',
            widgets.HiddenInput(),
            filter=ConfigFilter('short_biography'),
        )

        widget = config.retrieve('widget', bound_field=form['short_biography'])
        self.assertEqual(widget.__class__, widgets.HiddenInput)

        config.pop()
        widget = config.retrieve('widget', bound_field=form['short_biography'])
        self.assertEqual(widget.__class__, widgets.Textarea)

        config.pop()
        widget = config.retrieve('widget', bound_field=form['short_biography'])
        self.assertEqual(widget.__class__, widgets.TextInput)
示例#2
0
    def test_filter_for_widget_class_name(self):
        form = RegistrationForm()

        config = FormConfig()
        config.configure('widget',
                         widgets.TextInput(),
                         filter=ConfigFilter('Textarea'))

        widget = config.retrieve('widget', bound_field=form['comment'])
        self.assertEqual(widget.__class__, widgets.TextInput)

        widget = config.retrieve('widget', bound_field=form['name'])
        self.assertEqual(widget.__class__, widgets.TextInput)

        # swap widgets TextInput <> Textarea

        config = FormConfig()
        config.configure('widget',
                         widgets.Textarea(),
                         filter=ConfigFilter('TextInput'))
        config.configure('widget',
                         widgets.TextInput(),
                         filter=ConfigFilter('Textarea'))

        widget = config.retrieve('widget', bound_field=form['comment'])
        self.assertEqual(widget.__class__, widgets.TextInput)

        widget = config.retrieve('widget', bound_field=form['name'])
        self.assertEqual(widget.__class__, widgets.Textarea)
示例#3
0
    def test_filter_for_name_object(self):
        form = RegistrationForm()

        config = FormConfig()
        config.configure('widget', widgets.Textarea(), filter=ConfigFilter('object'))

        widget = config.retrieve('widget', bound_field=form['email'])
        self.assertEqual(widget.__class__, widgets.EmailInput)

        widget = config.retrieve('widget', bound_field=form['name'])
        self.assertEqual(widget.__class__, widgets.TextInput)

        widget = config.retrieve('widget', bound_field=form['comment'])
        self.assertEqual(widget.__class__, widgets.Textarea)
示例#4
0
    def test_retrieve_for_multiple_valid_values(self):
        form = RegistrationForm()
        config = FormConfig()

        config.configure('widget',
                         widgets.Textarea(),
                         filter=ConfigFilter('CharField'))
        config.configure('widget',
                         widgets.HiddenInput(),
                         filter=ConfigFilter('short_biography'))

        widget = config.retrieve('widget', bound_field=form['name'])
        self.assertEqual(widget.__class__, widgets.Textarea)
        widget = config.retrieve('widget', bound_field=form['comment'])
        self.assertEqual(widget.__class__, widgets.Textarea)

        # we get HiddenInput since this was configured last, even the Textarea
        # config applies to ``short_biography``
        widget = config.retrieve('widget', bound_field=form['short_biography'])
        self.assertEqual(widget.__class__, widgets.HiddenInput)
class BasicForm(forms.Form):
    """
    TODO:--------------------------
    input           TextInput               OK
    inputN          NumberInput             OK
    inputEmail      EmailInput
    textarea        TextInput               OK
    drowpdown       Select                  OK
    drowpdown       SelectMultiple          OK
    checkbox        CheckboxInput
    checkbox2       MultiCheckbox?
    radiobox        RadioSelect
    date            DateInput
    time            TimeInput
    datetime        DateTimeInput
    """

    COLORS_CHOICES = [
        ('blue', 'Blue'),
        ('green', 'Green'),
        ('black', 'Black'),
    ]

    name = forms.CharField(max_length=32, widget=widgets.TextInput())
    year = forms.IntegerField(widget=widgets.NumberInput())
    description = forms.CharField(max_length=32, widget=widgets.Textarea(attrs={'rows': '4'}))
    color = forms.ChoiceField(widget=widgets.Select(), choices=COLORS_CHOICES)
    colors = forms.MultipleChoiceField(widget=widgets.Select(attrs={'multiple': True}), choices=COLORS_CHOICES)
    is_boolean = forms.CharField(max_length=32, widget=widgets.CheckboxInput())
    option = forms.ChoiceField(widget=widgets.RadioSelect(), choices=COLORS_CHOICES)
    is_not_boolean = forms.CharField(max_length=32, widget=widgets.CheckboxInput())
    option_again = forms.CharField(max_length=32, widget=widgets.CheckboxInput())

    def __init__(self, *args, **kwargs):
        super(BasicForm, self).__init__(*args, **kwargs)
        self.fields["year"].initial = 2021