Python django.forms.widgets.CheckboxInput() Examples

The following are 9 code examples of django.forms.widgets.CheckboxInput(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.forms.widgets , or try the search function .
Example #1
Source File: widgets.py    From freedomvote with GNU General Public License v3.0 6 votes vote down vote up
def render(self, name, value, attrs=None,):

        substitutions = {
            'clear_checkbox_label': self.clear_checkbox_label,
            'initial' : '<img class="img-responsive img-thumbnail" width="%s" src="%s">' % (
                force_text('100%'),
                force_text(get_thumbnailer(value)['medium'].url if value and hasattr(value, 'url') else static('images/placeholder.svg'))
            )
        }
        template = '%(initial)s%(input)s'

        substitutions['input'] = super(ClearableFileInput, self).render(name, value, attrs)

        if not self.is_required:
            template = '%(initial)s%(clear_template)s%(input)s'
            checkbox_name = self.clear_checkbox_name(name)
            checkbox_id = self.clear_checkbox_id(checkbox_name)
            substitutions['clear_checkbox_name'] = conditional_escape(checkbox_name)
            substitutions['clear_checkbox_id'] = conditional_escape(checkbox_id)
            substitutions['clear'] = CheckboxInput().render(checkbox_name, False, attrs={'id': checkbox_id})
            substitutions['clear_template'] = self.clear_checkbox_name(checkbox_name)

        return mark_safe(template % substitutions) 
Example #2
Source File: forms.py    From django-cas-server with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        super(BootsrapForm, self).__init__(*args, **kwargs)
        for field in self.fields.values():
            # Only tweak the field if it will be displayed
            if not isinstance(field.widget, widgets.HiddenInput):
                attrs = {}
                if (
                    isinstance(field.widget, (widgets.Input, widgets.Select, widgets.Textarea)) and
                    not isinstance(field.widget, (widgets.CheckboxInput,))
                ):
                    attrs['class'] = "form-control"
                if isinstance(field.widget, (widgets.Input, widgets.Textarea)) and field.label:
                    attrs["placeholder"] = field.label
                if field.required:
                    attrs["required"] = "required"
                field.widget.attrs.update(attrs) 
Example #3
Source File: test_config_forms.py    From maas with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_DictCharWidget_renders_with_empty_string_as_input_data(self):
        names = [factory.make_string(), factory.make_string()]
        initials = []
        labels = [factory.make_string(), factory.make_string()]
        widget = DictCharWidget(
            [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput],
            names,
            initials,
            labels,
            skip_check=True,
        )
        name = factory.make_string()
        html_widget = fromstring(
            "<root>" + widget.render(name, "") + "</root>"
        )
        widget_names = XPath("fieldset/input/@name")(html_widget)
        widget_labels = XPath("fieldset/label/text()")(html_widget)
        expected_names = [
            "%s_%s" % (name, widget_name) for widget_name in names
        ]
        self.assertEqual(
            [expected_names, labels], [widget_names, widget_labels]
        ) 
Example #4
Source File: bootstrap_toolkit.py    From yats with MIT License 6 votes vote down vote up
def bootstrap_input_type(field):
    """
    Return input type to use for field
    """
    try:
        widget = field.field.widget
    except:
        raise ValueError("Expected a Field, got a %s" % type(field))
    input_type = getattr(widget, 'bootstrap_input_type', None)
    if input_type:
        return str(input_type)
    if isinstance(widget, TextInput):
        return u'text'
    if isinstance(widget, CheckboxInput):
        return u'checkbox'
    if isinstance(widget, CheckboxSelectMultiple):
        return u'multicheckbox'
    if isinstance(widget, RadioSelect):
        return u'radioset'
    return u'default' 
Example #5
Source File: website_app.py    From starthinker with Apache License 2.0 5 votes vote down vote up
def is_checkbox(field):
  return isinstance(field.field.widget, CheckboxInput) 
Example #6
Source File: widgets.py    From adhocracy4 with GNU Affero General Public License v3.0 5 votes vote down vote up
def value_from_datadict(self, data, files, name):
        """
        Modify value_from_datadict, so that delete takes precedence over
        upload.
        """
        file_value = super(widgets.ClearableFileInput, self)\
            .value_from_datadict(data, files, name)
        checkbox_value = widgets.CheckboxInput()\
            .value_from_datadict(data, files, self.clear_checkbox_name(name))
        if not self.is_required and checkbox_value:
            return False
        return file_value 
Example #7
Source File: test_config_forms.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self):
        names = [factory.make_string(), factory.make_string()]
        initials = []
        labels = [factory.make_string(), factory.make_string()]
        values = [factory.make_string(), factory.make_string()]
        widget = DictCharWidget(
            [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput],
            names,
            initials,
            labels,
            skip_check=True,
        )
        name = factory.make_string()
        html_widget = fromstring(
            "<root>" + widget.render(name, values) + "</root>"
        )
        widget_names = XPath("fieldset/input/@name")(html_widget)
        widget_labels = XPath("fieldset/label/text()")(html_widget)
        widget_values = XPath("fieldset/input/@value")(html_widget)
        expected_names = [
            "%s_%s" % (name, widget_name) for widget_name in names
        ]
        self.assertEqual(
            [expected_names, labels, values],
            [widget_names, widget_labels, widget_values],
        ) 
Example #8
Source File: test_config_forms.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_empty_DictCharWidget_renders_as_empty_string(self):
        widget = DictCharWidget(
            [widgets.CheckboxInput], [], [], [], skip_check=True
        )
        self.assertEqual("", widget.render(factory.make_string(), "")) 
Example #9
Source File: widgets.py    From adhocracy4 with GNU Affero General Public License v3.0 4 votes vote down vote up
def render(self, name, value, attrs=None, renderer=None):
        html_id = attrs and attrs.get('id', name) or name
        has_image_set = self.is_initial(value)
        is_required = self.is_required

        file_placeholder = ugettext('Select a picture from your local folder.')
        file_input = super().render(name, None, {
            'id': html_id,
            'class': 'form-control form-control-file'
        })

        if has_image_set:
            file_name = basename(value.name)
            file_url = conditional_escape(value.url)
        else:
            file_name = ""
            file_url = ""

        text_input = widgets.TextInput().render('__noname__', file_name, {
            'class': 'form-control form-control-file-dummy',
            'placeholder': file_placeholder,
            'tabindex': '-1',
            'id': 'text-{}'.format(html_id)
        })

        checkbox_id = self.clear_checkbox_id(name)
        checkbox_name = self.clear_checkbox_name(name)
        checkbox_input = widgets.CheckboxInput().render(checkbox_name, False, {
            'id': checkbox_id,
            'class': 'clear-image',
            'data-upload-clear': html_id,
        })

        context = {
            'id': html_id,
            'has_image_set': has_image_set,
            'is_required': is_required,
            'file_url': file_url,
            'file_input': file_input,
            'file_id': html_id + '-file',
            'text_input': text_input,
            'checkbox_input': checkbox_input,
            'checkbox_id': checkbox_id
        }

        return loader.render_to_string(
            'a4images/image_upload_widget.html',
            context
        )