From 5350572c7b43c4b0732e6efdfce1e9ad70e655d3 Mon Sep 17 00:00:00 2001 From: Luan Date: Thu, 17 Oct 2013 13:13:49 -0300 Subject: [PATCH] Refactoring append to get method --- src/super_archives/templatetags/append_to_get.py | 59 ++++------------------------------------------------------- src/super_archives/utils/url.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 55 deletions(-) create mode 100644 src/super_archives/utils/url.py diff --git a/src/super_archives/templatetags/append_to_get.py b/src/super_archives/templatetags/append_to_get.py index 79dbf1c..a88e234 100644 --- a/src/super_archives/templatetags/append_to_get.py +++ b/src/super_archives/templatetags/append_to_get.py @@ -1,67 +1,16 @@ # -*- coding: utf-8 -*- -import urllib from django import template +from super_archives.utils import url + register = template.Library() @register.simple_tag(takes_context=True) def append_to_get(context, **kwargs): - # Getting the path with the query - current_url = u'{}?{}'.format( + return url.append_to_get( context['request'].META['PATH_INFO'], context['request'].META['QUERY_STRING'], + **kwargs ) - - if kwargs and context['request'].META['QUERY_STRING']: - current_url += '&' - - for key, value in kwargs.items(): - # get the key, value to check if the pair exists in the query - new = u'{}={}'.format(key, value) - - if new in current_url: - continue - - if key not in current_url: - current_url += u'{}={}&'.format(key, value) - continue - - parse_url = current_url.split(key) - - if len(parse_url) > 2: - continue - - if unicode(value) in parse_url[1][1:]: - continue - - check_kwargs_values = [ - False for value in kwargs.values() - if unicode(value) not in parse_url[1] - ] - - if not all(check_kwargs_values): - list_remaining = parse_url[1][1:].split('&') - real_remaining = u'' - - if len(list_remaining) >= 2: - real_remaining = u'&'.join(list_remaining[1:]) - - current_url = u'{url}{key}={value}&{remaining}'.format( - url=parse_url[0], - key=key, - value=value, - remaining=real_remaining, - ) - continue - - current_url = u'{url}{key}={value}+{remaining_get}'.format( - url=parse_url[0], - key=key, - value=value, - remaining_get=parse_url[1][1:], - ) - if current_url[-1] == '&': - return current_url[:-1] - return current_url diff --git a/src/super_archives/utils/url.py b/src/super_archives/utils/url.py new file mode 100644 index 0000000..c1dbd89 --- /dev/null +++ b/src/super_archives/utils/url.py @@ -0,0 +1,60 @@ +# -*- coding: utf-8 -*- + +def append_to_get(path, query=None, **kwargs): +# Getting the path with the query + current_url = u'{}?{}'.format( + path, + query, + ) + + if kwargs and query: + current_url += '&' + + for key, value in kwargs.items(): + # get the key, value to check if the pair exists in the query + new = u'{}={}'.format(key, value) + + if new in current_url: + continue + + if key not in current_url: + current_url += u'{}={}&'.format(key, value) + continue + + parse_url = current_url.split(key) + + if len(parse_url) > 2: + continue + + if unicode(value) in parse_url[1][1:]: + continue + + check_kwargs_values = [ + False for value in kwargs.values() + if unicode(value) not in parse_url[1] + ] + + if not all(check_kwargs_values): + list_remaining = parse_url[1][1:].split('&') + real_remaining = u'' + + if len(list_remaining) >= 2: + real_remaining = u'&'.join(list_remaining[1:]) + + current_url = u'{url}{key}={value}&{remaining}'.format( + url=parse_url[0], + key=key, + value=value, + remaining=real_remaining, + ) + continue + + current_url = u'{url}{key}={value}+{remaining_get}'.format( + url=parse_url[0], + key=key, + value=value, + remaining_get=parse_url[1][1:], + ) + if current_url[-1] == '&': + return current_url[:-1] + return current_url -- libgit2 0.21.2