url.py
2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# -*- 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 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
def pop_from_get(path, query=None, **kwargs):
# Getting the path with the query
print query
current_url = u'{}?{}'.format(
path,
query,
)
for key, value in kwargs.items():
popitem = u'{}={}'.format(key, value)
if query == popitem:
return path
if key not in current_url:
return current_url
first_path, end_path = current_url.split(key)
end_path_without_element = end_path.split(value, 1)
path_list = first_path + end_path_without_element
print path_list
return u''.join(path_list)