a9a308d51c921ea9a4a9416315543a8375aed932
[wolnelektury.git] / src / ajaxable / templatetags / ajaxable_tags.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
4 from django import template
5 from django.utils.encoding import force_str
6 from django.utils.safestring import mark_safe
7 from ajaxable.utils import placeholdized
8
9
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': ('*' if field.field.required else '') + force_str(field.label),
36         'helptext': force_str(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>''')