Commit 5350572c7b43c4b0732e6efdfce1e9ad70e655d3

Authored by Luan
1 parent ed8574cc

Refactoring append to get method

src/super_archives/templatetags/append_to_get.py
1 1 # -*- coding: utf-8 -*-
2 2  
3   -import urllib
4 3 from django import template
5 4  
  5 +from super_archives.utils import url
  6 +
6 7 register = template.Library()
7 8  
8 9  
9 10 @register.simple_tag(takes_context=True)
10 11 def append_to_get(context, **kwargs):
11   - # Getting the path with the query
12   - current_url = u'{}?{}'.format(
  12 + return url.append_to_get(
13 13 context['request'].META['PATH_INFO'],
14 14 context['request'].META['QUERY_STRING'],
  15 + **kwargs
15 16 )
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
... ...
src/super_archives/utils/url.py 0 → 100644
... ... @@ -0,0 +1,60 @@
  1 +# -*- coding: utf-8 -*-
  2 +
  3 +def append_to_get(path, query=None, **kwargs):
  4 +# Getting the path with the query
  5 + current_url = u'{}?{}'.format(
  6 + path,
  7 + query,
  8 + )
  9 +
  10 + if kwargs and query:
  11 + current_url += '&'
  12 +
  13 + for key, value in kwargs.items():
  14 + # get the key, value to check if the pair exists in the query
  15 + new = u'{}={}'.format(key, value)
  16 +
  17 + if new in current_url:
  18 + continue
  19 +
  20 + if key not in current_url:
  21 + current_url += u'{}={}&'.format(key, value)
  22 + continue
  23 +
  24 + parse_url = current_url.split(key)
  25 +
  26 + if len(parse_url) > 2:
  27 + continue
  28 +
  29 + if unicode(value) in parse_url[1][1:]:
  30 + continue
  31 +
  32 + check_kwargs_values = [
  33 + False for value in kwargs.values()
  34 + if unicode(value) not in parse_url[1]
  35 + ]
  36 +
  37 + if not all(check_kwargs_values):
  38 + list_remaining = parse_url[1][1:].split('&')
  39 + real_remaining = u''
  40 +
  41 + if len(list_remaining) >= 2:
  42 + real_remaining = u'&'.join(list_remaining[1:])
  43 +
  44 + current_url = u'{url}{key}={value}&{remaining}'.format(
  45 + url=parse_url[0],
  46 + key=key,
  47 + value=value,
  48 + remaining=real_remaining,
  49 + )
  50 + continue
  51 +
  52 + current_url = u'{url}{key}={value}+{remaining_get}'.format(
  53 + url=parse_url[0],
  54 + key=key,
  55 + value=value,
  56 + remaining_get=parse_url[1][1:],
  57 + )
  58 + if current_url[-1] == '&':
  59 + return current_url[:-1]
  60 + return current_url
... ...