Commit 449a023b74f33a0efc972898715a8249e9ecb602
1 parent
0f152d6a
Exists in
master
and in
39 other branches
Refactoring append_to_get templatetag
Showing
2 changed files
with
64 additions
and
53 deletions
Show diff stats
src/super_archives/templates/message-list.html
... | ... | @@ -13,9 +13,9 @@ |
13 | 13 | <h4>{% trans "Sort by" %}</h4> |
14 | 14 | <ul class="unstyled-list"> |
15 | 15 | <li {% ifequal order_by "hottest" %} title="{% trans "Remove filter" %}" {% endifequal %}> |
16 | - <span class="glyphicon glyphicon-chevron-right"></span> <a href="{% ifequal order_by "hottest" %} {% append_to_get order="",p=1 %} {% else %} {% append_to_get order='hottest',p=1 %} {% endifequal %}">{% trans "Relevance" %}</a></li> | |
16 | + <span class="glyphicon glyphicon-chevron-right"></span> <a href="{% ifequal order_by "hottest" %} {% append_to_get order="" p=1 %} {% else %} {% append_to_get order='hottest' p=1 %} {% endifequal %}">{% trans "Relevance" %}</a></li> | |
17 | 17 | <li {% ifequal order_by "latest" %} title="{% trans "Remove filter" %}" {% endifequal %}> |
18 | - <span class="glyphicon glyphicon-chevron-right"></span> <a href="{% ifequal order_by "latest" %} {% append_to_get order="",p=1 %} {% else %} {% append_to_get order='latest',p=1 %} {% endifequal %}"> | |
18 | + <span class="glyphicon glyphicon-chevron-right"></span> <a href="{% ifequal order_by "latest" %} {% append_to_get order="" p=1 %} {% else %} {% append_to_get order='latest' p=1 %} {% endifequal %}"> | |
19 | 19 | {% trans "Recent activity" %}</a></li> |
20 | 20 | </ul> |
21 | 21 | |
... | ... | @@ -23,7 +23,7 @@ |
23 | 23 | <ul class="unstyled-list"> |
24 | 24 | {% for list in lists %} |
25 | 25 | <li {% if list.name == selected_list %} title="{% trans "Remove filter" %}" class="selected" {% endif %}> |
26 | - <span class="glyphicon {% if list.name == selected_list %}glyphicon-remove{% else %}glyphicon-chevron-right{% endif %}"></span> <a href="{% ifnotequal list.name selected_list %} {% append_to_get list=list.name,p=1 %} {% else %} {% append_to_get list="",p=1 %} | |
26 | + <span class="glyphicon {% if list.name == selected_list %}glyphicon-remove{% else %}glyphicon-chevron-right{% endif %}"></span> <a href="{% ifnotequal list.name selected_list %} {% append_to_get list=list.name p=1 %} {% else %} {% append_to_get list="" p=1 %} | |
27 | 27 | {% endifnotequal %}">{{ list.name }}</a></li> |
28 | 28 | {% endfor %} |
29 | 29 | </ul> | ... | ... |
src/super_archives/templatetags/append_to_get.py
1 | +# -*- coding: utf-8 -*- | |
1 | 2 | |
2 | 3 | import urllib |
3 | 4 | from django import template |
4 | 5 | |
5 | 6 | register = template.Library() |
6 | 7 | |
7 | -""" | |
8 | -Decorator to facilitate template tag creation | |
9 | -""" | |
10 | -def easy_tag(func): | |
11 | - """deal with the repetitive parts of parsing template tags""" | |
12 | - def inner(parser, token): | |
13 | - #print token | |
14 | - try: | |
15 | - return func(*token.split_contents()) | |
16 | - except TypeError: | |
17 | - raise template.TemplateSyntaxError('Bad arguments for tag "%s"' % | |
18 | - token.split_contents()[0]) | |
19 | - inner.__name__ = func.__name__ | |
20 | - inner.__doc__ = inner.__doc__ | |
21 | - return inner | |
22 | - | |
23 | - | |
24 | -class AppendGetNode(template.Node): | |
25 | - def __init__(self, dict): | |
26 | - self.dict_pairs = {} | |
27 | - for pair in dict.split(','): | |
28 | - pair = pair.split('=') | |
29 | - self.dict_pairs[pair[0]] = template.Variable(pair[1]) | |
30 | - | |
31 | - def render(self, context): | |
32 | - get = context['request'].GET.copy() | |
33 | - | |
34 | - for key in self.dict_pairs: | |
35 | - get[key] = self.dict_pairs[key].resolve(context) | |
36 | - | |
37 | - path = context['request'].META['PATH_INFO'] | |
38 | - | |
39 | - if len(get): | |
40 | - # Convert all unicode objects in the get dict to | |
41 | - # str (utf-8 encoded) | |
42 | - get_utf_encoded = {} | |
43 | - for (key, value) in get.items(): | |
44 | - if isinstance(value, unicode): | |
45 | - value = value.encode('utf-8') | |
46 | - get_utf_encoded.update({key: value}) | |
47 | - get_utf_encoded = dict(get_utf_encoded) | |
48 | - | |
49 | - path = '?' + urllib.urlencode(get_utf_encoded) | |
50 | - | |
51 | - return path | |
52 | - | |
53 | -@register.tag() | |
54 | -@easy_tag | |
55 | -def append_to_get(_tag_name, dict): | |
56 | - return AppendGetNode(dict) | |
8 | + | |
9 | +@register.simple_tag(takes_context=True) | |
10 | +def append_to_get(context, **kwargs): | |
11 | + # Getting the path with the query | |
12 | + current_url = u'{}?{}'.format( | |
13 | + context['request'].META['PATH_INFO'], | |
14 | + context['request'].META['QUERY_STRING'], | |
15 | + ) | |
16 | + | |
17 | + if kwargs and context['request'].META['QUERY_STRING']: | |
18 | + current_url += '&' | |
19 | + | |
20 | + for key, value in kwargs.items(): | |
21 | + # get the key, value to check if the pair exists in the query | |
22 | + new = u'{}={}'.format(key, value) | |
23 | + | |
24 | + if new in current_url: | |
25 | + continue | |
26 | + | |
27 | + if key not in current_url: | |
28 | + current_url += u'{}={}&'.format(key, value) | |
29 | + continue | |
30 | + | |
31 | + parse_url = current_url.split(key) | |
32 | + | |
33 | + if len(parse_url) > 2: | |
34 | + continue | |
35 | + | |
36 | + if unicode(value) in parse_url[1][1:]: | |
37 | + continue | |
38 | + | |
39 | + check_kwargs_values = [ | |
40 | + False for value in kwargs.values() | |
41 | + if unicode(value) not in parse_url[1] | |
42 | + ] | |
43 | + | |
44 | + if not all(check_kwargs_values): | |
45 | + list_remaining = parse_url[1][1:].split('&') | |
46 | + real_remaining = u'' | |
47 | + | |
48 | + if len(list_remaining) >= 2: | |
49 | + real_remaining = u'&'.join(list_remaining[1:]) | |
50 | + | |
51 | + current_url = u'{url}{key}={value}&{remaining}'.format( | |
52 | + url=parse_url[0], | |
53 | + key=key, | |
54 | + value=value, | |
55 | + remaining=real_remaining, | |
56 | + ) | |
57 | + continue | |
58 | + | |
59 | + current_url = u'{url}{key}={value}+{remaining_get}'.format( | |
60 | + url=parse_url[0], | |
61 | + key=key, | |
62 | + value=value, | |
63 | + remaining_get=parse_url[1][1:], | |
64 | + ) | |
65 | + if current_url[-1] == '&': | |
66 | + return current_url[:-1] | |
67 | + return current_url | ... | ... |