1
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2
# not use this file except in compliance with the License. You may obtain
3
# a copy of the License at
5
# http://www.apache.org/licenses/LICENSE-2.0
7
# Unless required by applicable law or agreed to in writing, software
8
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
# License for the specific language governing permissions and limitations
14
from django import template as django_template
16
register = django_template.Library()
20
def add_bootstrap_class(field):
21
"""Add a "form-control" CSS class to the field's widget.
23
This is so that Bootstrap styles it properly.
25
if not isinstance(field.field.widget, (
26
django.forms.widgets.CheckboxInput,
27
django.forms.widgets.CheckboxSelectMultiple,
28
django.forms.widgets.RadioSelect,
29
django.forms.widgets.FileInput,
32
field_classes = set(field.field.widget.attrs.get('class', '').split())
33
field_classes.add('form-control')
34
field.field.widget.attrs['class'] = ' '.join(field_classes)
39
def is_checkbox(field):
40
return isinstance(field.field.widget, django.forms.CheckboxInput)
44
def is_multiple_checkbox(field):
45
return isinstance(field.field.widget, django.forms.CheckboxSelectMultiple)
50
return isinstance(field.field.widget, django.forms.RadioSelect)
55
return isinstance(field.field.widget, django.forms.FileInput)
59
def is_dynamic_select(field):
60
return hasattr(field.field.widget, 'add_item_link')
64
def wrapper_classes(field):
66
if is_multiple_checkbox(field):
67
classes.append('multiple-checkbox')
68
if is_dynamic_select(field):
69
classes.append('dynamic-select')
70
return ' '.join(classes)