# Copyright 2015-2016 The Distro Tracker Developers
# See the COPYRIGHT file at the top-level directory of this distribution and
# at https://deb.li/DTAuthors
#
# This file is part of Distro Tracker. It is subject to the license terms
# in the LICENSE file found in the top-level directory of this
# distribution and at https://deb.li/DTLicense. No part of Distro Tracker,
# including this file, may be copied, modified, propagated, or distributed
# except according to the terms contained in the LICENSE file.
# This file has been forked from django-bootstrap-form which was
# BSD licensed:
#
# Copyright (c) Ming Hsien Tzang and individual contributors.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of django-bootstrap-form nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"""Template tags to inject bootstrap-specific content on various elements."""
from django import forms, template
from django.template.loader import get_template
BOOTSTRAP_COLUMN_COUNT = 12
register = template.Library()
[docs]@register.filter
def bootstrap(element):
"""Apply normal bootstrap formatting to a form."""
markup_classes = {'label': '', 'value': '', 'single_value': ''}
return render(element, markup_classes)
[docs]@register.filter
def bootstrap_inline(element):
"""Apply inline bootstrap formatting to a form."""
markup_classes = {'label': 'sr-only', 'value': '', 'single_value': ''}
return render(element, markup_classes)
[docs]@register.filter
def bootstrap_horizontal(element, label_cols=None):
"""Apply horizontal bootstrap formatting to a form."""
if not label_cols:
label_cols = 'col-sm-2'
markup_classes = {
'label': label_cols,
'value': '',
'single_value': ''
}
for cl in label_cols.split(' '):
splited_class = cl.split('-')
try:
value_nb_cols = int(splited_class[-1])
except ValueError:
value_nb_cols = BOOTSTRAP_COLUMN_COUNT
if value_nb_cols >= BOOTSTRAP_COLUMN_COUNT:
splited_class[-1] = BOOTSTRAP_COLUMN_COUNT
else:
offset_class = cl.split('-')
offset_class[-1] = 'offset-' + str(value_nb_cols)
splited_class[-1] = str(BOOTSTRAP_COLUMN_COUNT - value_nb_cols)
markup_classes['single_value'] += ' ' + '-'.join(offset_class)
markup_classes['single_value'] += ' ' + '-'.join(splited_class)
markup_classes['value'] += ' ' + '-'.join(splited_class)
return render(element, markup_classes)
[docs]def render(element, markup_classes):
"""Render various elements with our custom templates."""
element_type = element.__class__.__name__.lower()
if element_type == 'boundfield':
add_input_classes(element)
template = get_template("html/bootstrap-field.html")
context = {'field': element, 'classes': markup_classes}
else:
has_management = getattr(element, 'management_form', None)
if has_management:
for form in element.forms:
for field in form.visible_fields():
add_input_classes(field)
template = get_template("html/bootstrap-formset.html")
context = {'formset': element, 'classes': markup_classes}
else:
for field in element.visible_fields():
add_input_classes(field)
template = get_template("html/bootstrap-form.html")
context = {'form': element, 'classes': markup_classes}
return template.render(context)
[docs]@register.filter
def is_checkbox(field):
"""Return True if the field is a Checkbox, False otherwise."""
return isinstance(field.field.widget, forms.CheckboxInput)
[docs]@register.filter
def is_multiple_checkbox(field):
"""Return True if the field is a MultipleCheckbox, False otherwise."""
return isinstance(field.field.widget, forms.CheckboxSelectMultiple)
[docs]@register.filter
def is_radio(field):
"""Return True if the field is a radio selector, False otherwise."""
return isinstance(field.field.widget, forms.RadioSelect)
[docs]@register.filter
def is_file(field):
"""Return True if the field is a file selector, False otherwise."""
return isinstance(field.field.widget, forms.FileInput)