1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

# 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() 

 

 

@register.filter 

def bootstrap(element): 

"""Apply normal bootstrap formatting to a form.""" 

markup_classes = {'label': '', 'value': '', 'single_value': ''} 

return render(element, markup_classes) 

 

 

@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) 

 

 

@register.filter 

def bootstrap_horizontal(element, label_cols=None): 

"""Apply horizontal bootstrap formatting to a form.""" 

68 ↛ 71line 68 didn't jump to line 71 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 

 

85 ↛ 86line 85 didn't jump to line 86, because the condition on line 85 was never true 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) 

 

 

def add_input_classes(field): 

"""Add bootstrap classes to a field.""" 

if (not is_checkbox(field) and not is_multiple_checkbox(field) and 

not is_radio(field) and not is_file(field)): 

field_classes = field.field.widget.attrs.get('class', '') 

field_classes += ' form-control' 

field.field.widget.attrs['class'] = field_classes 

 

 

def render(element, markup_classes): 

"""Render various elements with our custom templates.""" 

element_type = element.__class__.__name__.lower() 

 

112 ↛ 113line 112 didn't jump to line 113, because the condition on line 112 was never true 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) 

118 ↛ 119line 118 didn't jump to line 119, because the condition on line 118 was never true 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) 

 

 

@register.filter 

def is_checkbox(field): 

"""Return True if the field is a Checkbox, False otherwise.""" 

return isinstance(field.field.widget, forms.CheckboxInput) 

 

 

@register.filter 

def is_multiple_checkbox(field): 

"""Return True if the field is a MultipleCheckbox, False otherwise.""" 

return isinstance(field.field.widget, forms.CheckboxSelectMultiple) 

 

 

@register.filter 

def is_radio(field): 

"""Return True if the field is a radio selector, False otherwise.""" 

return isinstance(field.field.widget, forms.RadioSelect) 

 

 

@register.filter 

def is_file(field): 

"""Return True if the field is a file selector, False otherwise.""" 

return isinstance(field.field.widget, forms.FileInput)