forked from earle/django-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widgets.py
executable file
·53 lines (39 loc) · 2.21 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
from django.forms.widgets import Input, RadioInput, RadioFieldRenderer, RadioSelect, TextInput
from django.utils.html import conditional_escape
from django.utils.encoding import force_unicode
from django.utils.safestring import mark_safe
class OptionsRadioInput(RadioInput):
def __unicode__(self):
if 'id' in self.attrs:
label_for = ' for="%s_%s"' % (self.attrs['id'], self.index)
else:
label_for = ''
choice_label = conditional_escape(force_unicode(self.choice_label))
return mark_safe(u'<label%s>%s <span>%s</span></label>' %
(label_for, self.tag(), choice_label))
class OptionsRadioRenderer(RadioFieldRenderer):
def render(self):
return mark_safe(u'<ul class="inputs-list">\n%s\n</ul>' %
u'\n'.join([u'<li>%s</li>' %
force_unicode(w) for w in self]))
class OptionsRadio(RadioSelect):
renderer = OptionsRadioRenderer
class AppendedText(TextInput):
def render(self, name, value, attrs=None):
append_text = self.attrs.get('text', '')
return mark_safe(u'%s<span class="add-on">%s</span>' % (super(AppendedText, self).render(name, value, attrs),
append_text))
class PrependedText(TextInput):
def render(self, name, value, attrs=None):
prepend_text = self.attrs.get('text', '')
return mark_safe(u'<span class="add-on">%s</span>%s' % (prepend_text, super(PrependedText, self).render(name, value, attrs)))
class AppendPrependText(TextInput):
def render(self, name, value, attrs=None):
append_text, prepend_text = self.attrs.get('append_text', ''), self.attrs.get('prepend_text', '')
return mark_safe(u'<span class="add-on">%s</span>%s<span class="add-on">%s</span>' % (prepend_text, super(AppendPrependText, self).render(name, value, attrs), append_text))
class EmailInput(Input):
input_type = 'email'
def render(self, name, value, attrs=None):
append_text = self.attrs.get('text', '@')
return mark_safe(u'%s<span class="add-on">%s</span>' % (super(EmailInput, self).render(name, value, attrs),
append_text))