98bc8a0bd3fa03547f8a23b8b46cefbf4402b36d
[auf_framonde.git] / project / framonde / templatetags / auf.py
1 # encoding: utf-8
2
3 from django.utils.http import urlunquote, urlquote
4 from django.utils.encoding import smart_text
5 from django import template
6
7 register = template.Library()
8
9
10 @register.simple_tag(takes_context=True, name="show_facet")
11 def do_show_facet(context, facet_name):
12 html = ""
13 if not 'fields' in context['facets']:
14 facets = []
15 else:
16 facets = context['facets']['fields'][facet_name]
17 selected_facets = context['form']['selected_facets'].value()
18 if not selected_facets:
19 selected_facets = []
20
21 path = context['request'].get_full_path()
22
23 for f in facets:
24 long_facet_name = "__".join([facet_name, urlquote(f[0])])
25
26 if urlunquote(long_facet_name) in selected_facets:
27 html += "<dd><a href='%s'><input onclick='window.location=\"%s\"; return true;'type='checkbox' checked='checked'/> %s</a> (%s)</dd>" % (path.replace(
28 "&selected_facets=" + long_facet_name,
29 "").replace(
30 "?selected_facets=" + long_facet_name,
31 "?"),
32 path.replace(
33 "&selected_facets=" + long_facet_name,
34 "").replace(
35 "?selected_facets=" + long_facet_name,
36 "?"),
37 smart_text(
38 f[0]), f[1].capitalize())
39 else:
40 if path.endswith('/'):
41 html += "<dd><a href='%s?selected_facets=%s'><input onclick='window.location=\"%s?selected_facets=%s\"; return true;'type='checkbox' />%s</a> (%s)</dd>" % (
42 path, long_facet_name, path, long_facet_name, smart_text(f[0]), f[1])
43 else:
44 html += "<dd><a href='%s&selected_facets=%s'><input onclick='window.location=\"%s&selected_facets=%s\"; return true;'type='checkbox' /> %s</a> (%s)</dd>" % (
45 path, long_facet_name, path, long_facet_name, smart_text(f[0]), f[1])
46 return html
47