55059e55b7e6292c953f3f1ef0935444db3f1580
[wolnelektury.git] / src / ajaxable / templatetags / ajaxable_tags.py
1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django import template
6 from django.utils.encoding import force_unicode
7 from django.utils.safestring import mark_safe
8
9 from ajaxable.utils import placeholdized
10 register = template.Library()
11
12
13 @register.filter
14 def placeholdize(form):
15     return placeholdized(form)
16
17
18 @register.filter
19 def placeholdized_ul(form):
20     return placeholdized(form).as_ul()
21
22
23 @register.filter
24 def pretty_field(field, template=None):
25     if template is None:
26         template = '''
27             <li>
28               <span class="error">%(errors)s</span>
29               <label class="nohide"><span class="label">%(label)s: </span>%(input)s</label>
30               <span class="helptext">%(helptext)s</span>
31             </li>'''
32     return mark_safe(template % {
33         'errors': field.errors,
34         'input': field,
35         'label': force_unicode(field.label),
36         'helptext': force_unicode(field.help_text),
37     })
38
39
40 @register.filter
41 def pretty_checkbox(field):
42     return pretty_field(field, template='''
43         <li class="checkbox">
44           <span class="error">%(errors)s</span>
45           <label class="nohide">%(input)s<span class="label"> %(label)s</span></label>
46           <span class="helptext">%(helptext)s</span>
47         </li>''')