Here are the examples of the python api django.forms.CheckboxInput taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
14 Examples
3
Source : helpers.py
with GNU General Public License v3.0
from Aghoreshwar
with GNU General Public License v3.0
from Aghoreshwar
def __init__(self, form, field, is_first):
self.field = form[field] # A django.forms.BoundField instance
self.is_first = is_first # Whether this field is first on the line
self.is_checkbox = isinstance(self.field.field.widget, forms.CheckboxInput)
self.is_readonly = False
def label_tag(self):
3
Source : test_checkboxinput.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_render_check_exception(self):
"""
Calling check_test() shouldn't swallow exceptions (#17888).
"""
widget = CheckboxInput(
check_test=lambda value: value.startswith('hello'),
)
with self.assertRaises(AttributeError):
widget.render('greeting', True)
def test_value_from_datadict(self):
3
Source : test_array.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_checkbox_get_context_attrs(self):
context = SplitArrayWidget(
forms.CheckboxInput(),
size=2,
).get_context('name', [True, False])
self.assertEqual(context['widget']['value'], '[True, False]')
self.assertEqual(
[subwidget['attrs'] for subwidget in context['widget']['subwidgets']],
[{'checked': True}, {}]
)
def test_render(self):
3
Source : forms.py
with GNU Lesser General Public License v3.0
from longguikeji
with GNU Lesser General Public License v3.0
from longguikeji
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
super(LoginForm, self).__init__(*args, **kwargs)
if getattr(settings, 'MAMA_CAS_ALLOW_AUTH_WARN', False):
self.fields['warn'] = forms.BooleanField(
widget=forms.CheckboxInput(),
label=_("Warn before automatic login to other services"),
required=False)
def clean(self):
3
Source : helpers.py
with Apache License 2.0
from lumanjiao
with Apache License 2.0
from lumanjiao
def __init__(self, form, field, is_first):
self.field = form[field] # A django.forms.BoundField instance
self.is_first = is_first # Whether this field is first on the line
self.is_checkbox = isinstance(self.field.field.widget, forms.CheckboxInput)
def label_tag(self):
3
Source : test_form_tags.py
with MIT License
from nhsuk
with MIT License
from nhsuk
def test_add_class_to_empty_widget():
"""Test adding a class to widget without preexisting class attribute"""
widget = forms.CheckboxInput(attrs={})
new_class = "new-class"
expected = {"attrs": {"class": "new-class"}}
result = add_class(widget.__dict__, new_class)
assert result["attrs"] == expected["attrs"]
def test_add_class_to_widget_with_class():
3
Source : test_form_tags.py
with MIT License
from nhsuk
with MIT License
from nhsuk
def test_add_class_to_widget_with_class():
"""Test adding a class to widget with preexisting class attribute"""
widget = forms.CheckboxInput(attrs={"class": "old-class"})
new_class = "new-class"
expected = {"attrs": {"class": "old-class new-class"}}
result = add_class(widget.__dict__, new_class)
assert result["attrs"] == expected["attrs"]
class ExampleForm(forms.Form):
0
Source : widgets.py
with GNU General Public License v3.0
from abaoMAO
with GNU General Public License v3.0
from abaoMAO
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = []
has_id = attrs and 'id' in attrs
if DJANGO_11:
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
else:
final_attrs = self.build_attrs(attrs, name=name)
output = []
# Normalize to strings
str_values = set([force_text(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = forms.CheckboxInput(
final_attrs, check_test=lambda value: value in str_values)
option_value = force_text(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_text(option_label))
if final_attrs.get('inline', False):
output.append(u' < label%s class="checkbox-inline">%s %s < /label>' % (label_for, rendered_cb, option_label))
else:
output.append(u' < div class="checkbox"> < label%s>%s %s < /label> < /div>' % (label_for, rendered_cb, option_label))
return mark_safe(u'\n'.join(output))
class AdminSelectMultiple(forms.SelectMultiple):
0
Source : forms.py
with MIT License
from bihealth
with MIT License
from bihealth
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.initial["prio_algorithm"] = PRIO_HIPHIVE_HUMAN
self.fields["prio_enabled"] = forms.BooleanField(
label="Enable phenotype-based prioritization",
help_text=(
"Note well that only variants in the first %d genes returned by your query will be prioritized!"
)
% settings.VARFISH_EXOMISER_PRIORITISER_MAX_GENES,
required=False,
widget=forms.CheckboxInput(),
)
self.fields["prio_algorithm"] = forms.ChoiceField(
label="Algorithm",
help_text=(
"Prioritizer algorithm to use. Phenix uses known human disease gene, Phive also uses information "
"from mouse models, and HiPhive also uses zebrafish and protein-protein interaction."
),
choices=PRIO_ALGORITHM_CHOICES,
required=False,
)
self.fields["prio_hpo_terms"] = forms.CharField(
label="",
help_text=(
"Click a suggestion to add it to the text field, or type into text field. This field is sodar "
"compatible. HPO and OMIM terms are < kbd>; < /kbd> separated. Enter an HPO or OMIM id, optionally with the name, "
"separated by < kbd> - < /kbd>. Only HPO or OMIM ids are considered, i.e. the text can be arbitrary and does not "
"have to match the actual name. It serves just your orientation."
),
widget=forms.Textarea(
attrs={
"placeholder": "Enter HPO or OMIM terms here (e.g. HP:1234567; OMIM:123456) or click a suggestion above ...",
"rows": 3,
"class": "form-control",
}
),
max_length=5000,
required=False,
)
self.fields["patho_enabled"] = forms.BooleanField(
label="Enable variant pathogenicity-based prioritization",
help_text=(
"First try to filter your variants without pathogenicity-based prioritization before enabling it. "
"Note well that only the first %d variants returned by your query will be prioritized!"
)
% settings.VARFISH_CADD_MAX_VARS,
required=False,
widget=forms.CheckboxInput(),
)
#: Choices for variant scoring methods.
PATHO_SCORE_CHOICES = [(PATHO_MUTATIONTASTER, PATHO_MUTATIONTASTER_LABEL)]
PATHO_SCORE_INITIAL = (PATHO_MUTATIONTASTER, PATHO_MUTATIONTASTER_LABEL)
if settings.VARFISH_ENABLE_CADD:
PATHO_SCORE_CHOICES.append((PATHO_CADD, PATHO_CADD_LABEL))
PATHO_SCORE_INITIAL = (PATHO_CADD, PATHO_CADD_LABEL)
if app_settings.get_app_setting("variants", "umd_predictor_api_token", user=self.superuser):
PATHO_SCORE_CHOICES.append((PATHO_UMD, PATHO_UMD_LABEL))
self.fields["patho_score"] = forms.ChoiceField(
label="Score",
help_text="Pathogenicity scoring method to use.",
choices=PATHO_SCORE_CHOICES,
initial=PATHO_SCORE_INITIAL,
required=False,
)
def clean(self):
0
Source : semanticui.py
with GNU General Public License v3.0
from foonsun
with GNU General Public License v3.0
from foonsun
def is_checkbox(field):
return isinstance(field.field.widget, forms.CheckboxInput)
@register.filter
0
Source : test_checkboxinput.py
with Apache License 2.0
from gethue
with Apache License 2.0
from gethue
def test_render_check_test(self):
"""
You can pass 'check_test' to the constructor. This is a callable that
takes the value and returns True if the box should be checked.
"""
widget = CheckboxInput(check_test=lambda value: value.startswith('hello'))
self.check_html(widget, 'greeting', '', html=(
' < input type="checkbox" name="greeting" />'
))
self.check_html(widget, 'greeting', 'hello', html=(
' < input checked type="checkbox" name="greeting" value="hello" />'
))
self.check_html(widget, 'greeting', 'hello there', html=(
' < input checked type="checkbox" name="greeting" value="hello there" />'
))
self.check_html(widget, 'greeting', 'hello & goodbye', html=(
' < input checked type="checkbox" name="greeting" value="hello & goodbye" />'
))
def test_render_check_exception(self):
0
Source : widgets.py
with MIT License
from MarkHoo
with MIT License
from MarkHoo
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = []
has_id = attrs and 'id' in attrs
if DJANGO_11:
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
else:
final_attrs = self.build_attrs(attrs, name=name)
output = []
# Normalize to strings
str_values = set([force_text(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = forms.CheckboxInput(
final_attrs, check_test=lambda value: value in str_values)
option_value = force_text(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_text(option_label))
final_attrs['inline'] = True if self.attrs['inline'] else False
if final_attrs.get('inline', False):
output.append(u' < label%s class="checkbox-inline">%s %s < /label>' % (label_for, rendered_cb, option_label))
else:
output.append(u' < div class="checkbox"> < label%s>%s %s < /label> < /div>' % (label_for, rendered_cb, option_label))
return mark_safe(u'\n'.join(output))
class AdminSelectMultiple(forms.SelectMultiple):
0
Source : wrappers.py
with MIT License
from matthiask
with MIT License
from matthiask
def __str__(self):
extra_classes = []
if not hasattr(self.field.form, "error_css_class") and self.field.errors:
extra_classes.append(self.error_css_class)
if (
not hasattr(self.field.form, "required_css_class")
and self.field.field.required
):
extra_classes.append(self.required_css_class)
widget = self.field.field.widget
extra_label_classes = []
if isinstance(widget, forms.CheckboxInput):
widget.attrs["class"] = widget.attrs.get("class", "") + " form-check-input"
extra_label_classes.append("form-check-label")
elif isinstance(widget, (forms.RadioSelect, forms.CheckboxSelectMultiple)):
widget.attrs["class"] = " ".join(
filter(None, (widget.attrs.get("class", ""), "my-2"))
)
else:
cls = "form-control"
if isinstance(widget, forms.Select):
cls = "custom-select"
elif isinstance(widget, forms.FileInput):
cls = "form-control-file"
widget.attrs["class"] = " ".join(
filter(
None,
(
widget.attrs.get("class", ""),
cls,
" is-invalid" if self.field.errors else "",
),
)
)
html = render_to_string(
self.template_name,
{
"field": self.field,
"widget_then_label": isinstance(
self.field.field.widget, forms.CheckboxInput
),
"label_tag": self.field.label_tag(
label_suffix=self.label_suffix,
attrs=({"class": " ".join(extra_classes + extra_label_classes)}),
),
"css_classes": self.field.css_classes(
extra_classes=extra_classes
+ [
"widget--%s"
% (self.field.field.widget.__class__.__name__.lower(),)
]
),
},
)
if isinstance(widget, forms.RadioSelect):
html = html.replace('type="radio"', 'class="form-check-input" type="radio"')
elif isinstance(widget, forms.CheckboxSelectMultiple):
html = html.replace(
'type="checkbox"', 'class="form-check-input" type="checkbox"'
)
return html
0
Source : widgets.py
with MIT License
from Superbsco
with MIT License
from Superbsco
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = []
has_id = attrs and 'id' in attrs
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
output = []
# Normalize to strings
str_values = set([force_text(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = forms.CheckboxInput(
final_attrs, check_test=lambda value: value in str_values)
option_value = force_text(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_text(option_label))
if final_attrs.get('inline', False):
output.append(u' < label%s class="checkbox-inline">%s %s < /label>' % (label_for, rendered_cb, option_label))
else:
output.append(u' < div class="checkbox"> < label%s>%s %s < /label> < /div>' % (label_for, rendered_cb, option_label))
return mark_safe(u'\n'.join(output))
class AdminSelectMultiple(forms.SelectMultiple):