-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathwidgets.py
319 lines (266 loc) · 11.5 KB
/
widgets.py
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
from django.contrib.admin import widgets as admin_widgets
from django.forms import widgets as form_widgets
from django.forms import TextInput, Select, Textarea, fields
from django.utils.safestring import mark_safe
from django.core.urlresolvers import reverse
from django import forms
from django.utils import formats, translation
from django.utils.translation import ugettext as _
from django.contrib.admin.templatetags.admin_static import static
from suit import utils
class LinkedSelect(Select):
"""
Linked select - Adds link to foreign item, when used with foreign key field
"""
def __init__(self, attrs=None, choices=()):
attrs = _make_attrs(attrs, classes="linked-select")
super(LinkedSelect, self).__init__(attrs, choices)
class EnclosedInput(TextInput):
"""
Widget for bootstrap appended/prepended inputs
"""
def __init__(self, attrs=None, prepend=None, append=None):
"""
For prepend, append parameters use string like %, $ or html
"""
self.prepend = prepend
self.append = append
super(EnclosedInput, self).__init__(attrs=attrs)
def enclose_value(self, value):
"""
If value doesn't starts with html open sign "<", enclose in add-on tag
"""
cls = 'addon'
if value.startswith("<"):
cls = 'btn'
if value.startswith("glyphicon-"):
value = '<i class="glyphicon %s"></i>' % value
return '<span class="input-group-%s">%s</span>' % (cls, value)
def render(self, name, value, attrs=None):
output = super(EnclosedInput, self).render(name, value, attrs)
div_classes = []
if self.prepend:
# div_classes.append('input-prepend')
self.prepend = self.enclose_value(self.prepend)
output = ''.join((self.prepend, output))
if self.append:
# div_classes.append('input-append')
self.append = self.enclose_value(self.append)
output = ''.join((output, self.append))
return mark_safe(
'<div class="input-group %s">%s</div>' % (
' '.join(div_classes), output))
class AutosizedTextarea(Textarea):
"""
Autosized Textarea - textarea height dynamically grows based on user input
"""
def __init__(self, attrs=None):
new_attrs = _make_attrs(attrs, {"rows": 2}, "autosize")
super(AutosizedTextarea, self).__init__(new_attrs)
@property
def media(self):
return forms.Media(js=[static("suit/js/jquery.autosize-min.js")])
def render(self, name, value, attrs=None):
output = super(AutosizedTextarea, self).render(name, value, attrs)
output += mark_safe(
"<script type=\"text/javascript\">Suit.$('#id_%s').autosize("
");</script>"
% name)
return output
class SuitDateWidget(forms.DateInput):
@property
def media(self):
js = ['datepicker/bootstrap-datepicker.js']
dp_lang = self.language()
if dp_lang != 'en':
js.append('datepicker/locales/bootstrap-datepicker.%s.js' % dp_lang)
return forms.Media(
js=[static("suit/js/%s" % path) for path in js],
css={'all': [static("suit/js/datepicker/css/datepicker3.css")]}
)
def __init__(self, attrs=None, format=None):
attrs = attrs or {}
self.format = format
super(SuitDateWidget, self).__init__(attrs=attrs, format=format)
def language(self):
lang = str(translation.get_language() or 'en')[:2].lower()
dp_lang = lang
if lang.startswith('zh'):
dp_lang = {
'zh-cn': 'zh-CN',
'zh-tw': 'zh-TW'
}.get(lang, 'en')
return dp_lang
def date_format(self):
return self.format or formats.get_format(self.format_key)[0]
def datepicker_date_format(self, django_format):
mapping = {
'%Y': 'yyyy',
'%y': 'yy',
'%m': 'mm',
'%b': 'M', # Oct, Nov
'%B': 'MM', # October, November
'%d': 'dd',
'%M': 'i',
'%S': 's',
'.%f': '', # Microseconds are not supported by datepicker
}
for dj_fmt, dp_fmt in mapping.items():
django_format = django_format.replace(dj_fmt, dp_fmt)
django_format = django_format.replace('%', '')
return django_format
def render(self, name, value, attrs=None):
output = super(SuitDateWidget, self).render(name, value, attrs)
# Because we wrap input tag in input-group we must copy data-* attrs
# for datepicker formats
attrs = utils.attrs_by_prefix(output, 'data-date-')
if 'data-date-format' not in attrs:
attrs['data-date-format'] = self.datepicker_date_format(
self.date_format())
attrs = utils.dict_to_attrs(attrs)
return mark_safe(
'<div class="input-group date" %s>%s<span '
'class="input-group-addon"><i class="glyphicon '
'glyphicon-th"></i></span></div>' % (
attrs, output))
class SuitDateTimeWidget(forms.DateTimeInput, SuitDateWidget):
def __init__(self, attrs=None, format=None):
self.format = format
attrs = attrs or {}
attrs['data-date-show-time'] = 'true'
super(SuitDateTimeWidget, self).__init__(attrs, format)
class SuitTimeWidget(admin_widgets.AdminTimeWidget):
def __init__(self, attrs=None, format=None):
defaults = {'placeholder': _('Time:')[:-1]}
new_attrs = _make_attrs(attrs, defaults, "vTimeField input-small")
super(SuitTimeWidget, self).__init__(attrs=new_attrs, format=format)
def render(self, name, value, attrs=None):
output = super(SuitTimeWidget, self).render(name, value, attrs)
return mark_safe(
'<div class="input-append suit-date suit-time">%s<span '
'class="add-on"><i class="icon-time"></i></span></div>' %
output)
class SuitSplitDateTimeWidget(forms.SplitDateTimeWidget):
"""
A SplitDateTime Widget that has some admin-specific styling.
"""
def __init__(self, attrs=None):
widgets = [SuitDateWidget, SuitTimeWidget]
forms.MultiWidget.__init__(self, widgets, attrs)
def format_output(self, rendered_widgets):
out_tpl = '<div class="datetime">%s %s</div>'
return mark_safe(out_tpl % (rendered_widgets[0], rendered_widgets[1]))
def _make_attrs(attrs, defaults=None, classes=None):
result = defaults.copy() if defaults else {}
if attrs:
result.update(attrs)
if classes:
result["class"] = " ".join((classes, result.get("class", "")))
return result
def wrap_as_input_group(s, append=''):
return mark_safe('<div class="input-group">%s</div>%s' % (s, append))
def add_bs3_markup():
def _build_attrs(self, *args, **kwargs):
"""
Merge bs3_class attribute with existing class
"""
attrs = self.build_attrs_original(*args, **kwargs) or {}
bs3_class = attrs.pop('bs3_class', '')
if bs3_class:
css_class = attrs.get('class')
css_classes = [css_class] if css_class else []
css_classes.append(bs3_class)
attrs['class'] = ' '.join(css_classes)
return attrs
def _widget_attrs(self, widget):
"""
Add Bootstrap 3 form-control CSS class
"""
apply_to = (fields.CharField, fields.BaseTemporalField,
fields.IntegerField, fields.ChoiceField)
attrs = self.widget_attrs_original(widget) or {}
if issubclass(self.__class__, apply_to) and not \
issubclass(widget.__class__, (
form_widgets.HiddenInput, form_widgets.RadioSelect)):
attrs.update({'bs3_class': 'form-control'})
return attrs
# Override widget_attrs and build_attrs globally to add form-control class
fields.Field.widget_attrs_original = fields.Field.widget_attrs
fields.Field.widget_attrs = _widget_attrs
form_widgets.Widget.build_attrs_original = form_widgets.Widget.build_attrs
form_widgets.Widget.build_attrs = _build_attrs
###########
#
# Adjust markup for special widgets
# I monkey patched, to support all the inherited classes in 3rd party apps
#
# Select dropdown + PLUS button
def render_RelatedFieldWidgetWrapper(self, name, value, *args, **kwargs):
"""
Method is a clone from original widget, with adjusted markup
"""
from django.contrib.admin.views.main import TO_FIELD_VAR
rel_to = self.rel.to
info = (rel_to._meta.app_label, rel_to._meta.model_name)
self.widget.choices = self.choices
output = ['<div class="input-group">',
self.widget.render(name, value, *args, **kwargs)]
if self.can_add_related:
related_url = reverse('admin:%s_%s_add' % info,
current_app=self.admin_site.name)
url_params = '?%s=%s' % (
TO_FIELD_VAR, self.rel.get_related_field().name)
# TODO: "add_id_" is hard-coded here. This should instead use the
# correct API to determine the ID dynamically.
output.append(
'<span class="input-group-btn">'
'<a href="%s%s" class="add-another btn btn-default" '
'id="add_id_%s" onclick="return showAddAnotherPopup(this);" '
'title="%s"><span class="glyphicon glyphicon-plus-sign '
'color-success"></span></a></span>'
% (related_url, url_params, name, _('Add Another')))
output.append('</div>')
return mark_safe(''.join(output))
admin_widgets.RelatedFieldWidgetWrapper.render = \
render_RelatedFieldWidgetWrapper
# Select dropdown + PLUS button
def render_ForeignKeyRawIdWidget(self, name, value, attrs=None):
rel_to = self.rel.to
if attrs is None:
attrs = {}
extra = []
if rel_to in self.admin_site._registry:
# The related object is registered with the same AdminSite
related_url = reverse(
'admin:%s_%s_changelist' % (
rel_to._meta.app_label,
rel_to._meta.model_name,
),
current_app=self.admin_site.name,
)
params = self.url_parameters()
if params:
url = '?' + '&'.join(
'%s=%s' % (k, v) for k, v in params.items())
else:
url = ''
if "class" not in attrs:
attrs['class'] = 'vForeignKeyRawIdAdminField'
# The JavaScript code looks for this hook.
# TODO: "lookup_id_" is hard-coded here. This should instead use
# the correct API to determine the ID dynamically.
extra.append(
'<span class="input-group-btn"><a href="%s%s" '
'class="related-lookup btn btn-default" id="lookup_id_%s" '
'onclick="return showRelatedObjectLookupPopup(this);" '
'title="%s"><span class="glyphicon '
'glyphicon-search"></span></a></span>' %
(related_url, url, name, _('Lookup')))
output = ['<div class="input-group">',
super(self.__class__, self).render(name, value, attrs)]
output.extend(extra)
output.append('</div>')
if value:
output.append(self.label_for_value(value))
return mark_safe(''.join(output))
admin_widgets.ForeignKeyRawIdWidget.render = render_ForeignKeyRawIdWidget