From cefe623f0b25e45e3de3a8f0bc433cbd918d3399 Mon Sep 17 00:00:00 2001 From: Luan Date: Fri, 18 Oct 2013 10:13:18 -0300 Subject: [PATCH] Refactoring append_to_get to use urllib and urlparse --- src/super_archives/utils/url.py | 61 ++++++------------------------------------------------------- 1 file changed, 6 insertions(+), 55 deletions(-) diff --git a/src/super_archives/utils/url.py b/src/super_archives/utils/url.py index b95a364..a53adfd 100644 --- a/src/super_archives/utils/url.py +++ b/src/super_archives/utils/url.py @@ -1,63 +1,14 @@ # -*- coding: utf-8 -*- -def append_to_get(path, query=None, **kwargs): -# Getting the path with the query - current_url = u'{}?{}'.format( - path, - query, - ) +import urllib +import urlparse - if kwargs and query: - current_url += '&' +def append_to_get(path, query=None, **kwargs): + query_dict = dict(urlparse.parse_qsl(query)) 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 u'&{}='.format(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 + query_dict[key] = value + return u'{}?{}'.format(path, urllib.urlencode(query_dict)) def pop_from_get(path, query=None, **kwargs): -- libgit2 0.21.2