def unauthorized_handler(): user_unauthorized.send( user_unauthorized.send(current_app._get_current_object())) url = URLObject(login_url(login_manager.login_view, request.url)) prev_param = make_next_param(url.without_query(), request.referrer or '/') return redirect( url.add_query_param('prev', prev_param).add_query_param('mode', 'action'))
def add_tag(context, tagname): # Current url url = URLObject(context.request.get_full_path()) # Dictionary of tags in current url tagdict = url.query.multi_dict tagvalues = tagdict.get("tag", []) new_url = URLObject("/") # Prevent adding same value more than once if tagname not in tagvalues: new_url = new_url.add_query_param("tag", tagname) for tag in tagvalues: new_url = new_url.add_query_param("tag", tag) return new_url
def remove_tag(context, tagname): # Current url url = URLObject(context.request.get_full_path()) # Dictionary of tags in current url tagdict = url.query.multi_dict tagvalues = tagdict.get("tag", []) # Remove the value of to be removed tagname tagvalues.remove(tagname) # Make clean url url = url.del_query_param("tag") # Add only the remaining tags to the new url for stay_tag in tagvalues: url = url.add_query_param("tag", stay_tag) return url
class URLObjectModificationTest(unittest.TestCase): def setUp(self): self.url = URLObject('https://github.com/zacharyvoase/urlobject?spam=eggs#foo') def test_with_scheme_replaces_scheme(self): assert (self.url.with_scheme('http') == 'http://github.com/zacharyvoase/urlobject?spam=eggs#foo') def test_with_netloc_replaces_netloc(self): assert (self.url.with_netloc('example.com') == 'https://example.com/zacharyvoase/urlobject?spam=eggs#foo') def test_with_hostname_replaces_hostname(self): url = URLObject('https://*****:*****@github.com/') assert (url.with_hostname('example.com') == 'https://*****:*****@example.com/') def test_with_username_adds_username(self): url = URLObject('https://github.com/') assert url.with_username('zack') == 'https://[email protected]/' def test_with_username_replaces_username(self): url = URLObject('https://[email protected]/') assert url.with_username('alice') == 'https://[email protected]/' def test_without_username_removes_username(self): url = URLObject('https://[email protected]/') assert url.without_username() == 'https://github.com/' def test_with_password_adds_password(self): url = URLObject('https://[email protected]/') assert url.with_password('1234') == 'https://*****:*****@github.com/' def test_with_password_raises_ValueError_when_there_is_no_username(self): url = URLObject('https://github.com/') assert_raises(ValueError, lambda: url.with_password('1234')) def test_with_password_replaces_password(self): url = URLObject('https://*****:*****@github.com/') assert url.with_password('5678') == 'https://*****:*****@github.com/' def test_without_password_removes_password(self): url = URLObject('https://*****:*****@github.com/') assert url.without_password() == 'https://[email protected]/' def test_with_auth_with_one_arg_adds_username(self): url = URLObject('https://github.com/') assert url.with_auth('zack') == 'https://[email protected]/' def test_with_auth_with_one_arg_replaces_whole_auth_string_with_username(self): url = URLObject('https://*****:*****@github.com/') assert url.with_auth('zack') == 'https://[email protected]/' def test_with_auth_with_two_args_adds_username_and_password(self): url = URLObject('https://github.com/') assert url.with_auth('zack', '1234') == 'https://*****:*****@github.com/' def test_with_auth_with_two_args_replaces_whole_auth_string_with_username_and_password(self): # Replaces username-only auth string url = URLObject('https://[email protected]/') assert url.with_auth('zack', '1234') == 'https://*****:*****@github.com/' # Replaces username and password. url = URLObject('https://*****:*****@github.com/') assert url.with_auth('zack', '1234') == 'https://*****:*****@github.com/' def test_without_auth_removes_entire_auth_string(self): # No username or password => no-op. url = URLObject('https://github.com/') assert url.without_auth() == 'https://github.com/' # Username-only. url = URLObject('https://[email protected]/') assert url.without_auth() == 'https://github.com/' # Username and password. url = URLObject('https://*****:*****@github.com/') assert url.without_auth() == 'https://github.com/' def test_with_port_adds_port_number(self): assert (self.url.with_port(24) == 'https://github.com:24/zacharyvoase/urlobject?spam=eggs#foo') def test_with_port_replaces_port_number(self): url = URLObject('https://github.com:59/') assert url.with_port(67) == 'https://github.com:67/' def test_without_port_removes_port_number(self): url = URLObject('https://github.com:59/') assert url.without_port() == 'https://github.com/' def test_with_path_replaces_path(self): assert (self.url.with_path('/dvxhouse/intessa') == 'https://github.com/dvxhouse/intessa?spam=eggs#foo') def test_root_goes_to_root_path(self): assert self.url.root == 'https://github.com/?spam=eggs#foo' def test_parent_jumps_up_one_level(self): url = URLObject('https://github.com/zacharyvoase/urlobject') assert url.parent == 'https://github.com/zacharyvoase/' assert url.parent.parent == 'https://github.com/' def test_add_path_segment_adds_a_path_segment(self): url = URLObject('https://github.com/zacharyvoase/urlobject') assert (url.add_path_segment('tree') == 'https://github.com/zacharyvoase/urlobject/tree') assert (url.add_path_segment('tree/master') == 'https://github.com/zacharyvoase/urlobject/tree%2Fmaster') def test_add_path_adds_a_partial_path(self): url = URLObject('https://github.com/zacharyvoase/urlobject') assert (url.add_path('tree') == 'https://github.com/zacharyvoase/urlobject/tree') assert (url.add_path('tree/master') == 'https://github.com/zacharyvoase/urlobject/tree/master') def test_is_leaf(self): assert URLObject('https://github.com/zacharyvoase/urlobject').is_leaf assert not URLObject('https://github.com/zacharyvoase/').is_leaf def test_with_query_replaces_query(self): assert (self.url.with_query('spam-ham-eggs') == 'https://github.com/zacharyvoase/urlobject?spam-ham-eggs#foo') def test_without_query_removes_query(self): assert (self.url.without_query() == 'https://github.com/zacharyvoase/urlobject#foo') def test_add_query_param_adds_one_query_parameter(self): assert (self.url.add_query_param('spam', 'ham') == 'https://github.com/zacharyvoase/urlobject?spam=eggs&spam=ham#foo') def test_add_query_params_adds_multiple_query_parameters(self): assert (self.url.add_query_params([('spam', 'ham'), ('foo', 'bar')]) == 'https://github.com/zacharyvoase/urlobject?spam=eggs&spam=ham&foo=bar#foo') def test_add_query_params_with_multiple_values_adds_the_same_query_parameter_multiple_times(self): assert (self.url.add_query_params({'foo': ['bar', 'baz']}) == 'https://github.com/zacharyvoase/urlobject?spam=eggs&foo=bar&foo=baz#foo') def test_set_query_param_adds_or_replaces_one_query_parameter(self): assert (self.url.set_query_param('spam', 'ham') == 'https://github.com/zacharyvoase/urlobject?spam=ham#foo') def test_set_query_params_adds_or_replaces_multiple_query_parameters(self): assert (self.url.set_query_params({'foo': 'bar'}, spam='ham') == 'https://github.com/zacharyvoase/urlobject?foo=bar&spam=ham#foo') def test_set_query_params_with_multiple_values_adds_or_replaces_the_same_parameter_multiple_times(self): assert (self.url.set_query_params({'spam': ['bar', 'baz']}) == 'https://github.com/zacharyvoase/urlobject?spam=bar&spam=baz#foo') assert (self.url.set_query_params({'foo': ['bar', 'baz']}) == 'https://github.com/zacharyvoase/urlobject?spam=eggs&foo=bar&foo=baz#foo') # Ensure it removes all appearances of an existing name before adding # the new ones. url = URLObject('https://github.com/zacharyvoase/urlobject?foo=bar&foo=baz#foo') assert (url.set_query_params({'foo': ['spam', 'ham']}) == 'https://github.com/zacharyvoase/urlobject?foo=spam&foo=ham#foo') def test_del_query_param_removes_one_query_parameter(self): assert (self.url.del_query_param('spam') == 'https://github.com/zacharyvoase/urlobject#foo') def test_del_query_params_removes_multiple_query_parameters(self): url = URLObject('https://github.com/zacharyvoase/urlobject?foo=bar&baz=spam#foo') assert (url.del_query_params(['foo', 'baz']) == 'https://github.com/zacharyvoase/urlobject#foo') def test_with_fragment_replaces_fragment(self): assert (self.url.with_fragment('part') == 'https://github.com/zacharyvoase/urlobject?spam=eggs#part') def test_with_fragment_encodes_fragment_correctly(self): assert (self.url.with_fragment('foo bar#baz') == 'https://github.com/zacharyvoase/urlobject?spam=eggs#foo%20bar%23baz') def test_without_fragment_removes_fragment(self): assert (self.url.without_fragment() == 'https://github.com/zacharyvoase/urlobject?spam=eggs')
class URLObjectModificationTest(unittest.TestCase): def setUp(self): self.url = URLObject(u'https://github.com/zacharyvoase/urlobject?spam=eggs#foo') def test_with_scheme_replaces_scheme(self): assert (self.url.with_scheme('http') == u'http://github.com/zacharyvoase/urlobject?spam=eggs#foo') def test_with_netloc_replaces_netloc(self): assert (self.url.with_netloc('example.com') == u'https://example.com/zacharyvoase/urlobject?spam=eggs#foo') def test_with_hostname_replaces_hostname(self): url = URLObject(u'https://*****:*****@github.com/') assert (url.with_hostname('example.com') == u'https://*****:*****@example.com/') def test_with_username_adds_username(self): url = URLObject(u'https://github.com/') assert url.with_username('zack') == u'https://[email protected]/' def test_with_username_replaces_username(self): url = URLObject(u'https://[email protected]/') assert url.with_username('alice') == u'https://[email protected]/' def test_without_username_removes_username(self): url = URLObject(u'https://[email protected]/') assert url.without_username() == u'https://github.com/' def test_with_password_adds_password(self): url = URLObject(u'https://[email protected]/') assert url.with_password('1234') == u'https://*****:*****@github.com/' def test_with_password_raises_ValueError_when_there_is_no_username(self): url = URLObject(u'https://github.com/') assert_raises(ValueError, lambda: url.with_password('1234')) def test_with_password_replaces_password(self): url = URLObject(u'https://*****:*****@github.com/') assert url.with_password('5678') == u'https://*****:*****@github.com/' def test_without_password_removes_password(self): url = URLObject(u'https://*****:*****@github.com/') assert url.without_password() == u'https://[email protected]/' def test_with_auth_with_one_arg_adds_username(self): url = URLObject(u'https://github.com/') assert url.with_auth('zack') == u'https://[email protected]/' def test_with_auth_with_one_arg_replaces_whole_auth_string_with_username(self): url = URLObject(u'https://*****:*****@github.com/') assert url.with_auth('zack') == u'https://[email protected]/' def test_with_auth_with_two_args_adds_username_and_password(self): url = URLObject(u'https://github.com/') assert url.with_auth('zack', '1234') == u'https://*****:*****@github.com/' def test_with_auth_with_two_args_replaces_whole_auth_string_with_username_and_password(self): # Replaces username-only auth string url = URLObject(u'https://[email protected]/') assert url.with_auth('zack', '1234') == u'https://*****:*****@github.com/' # Replaces username and password. url = URLObject(u'https://*****:*****@github.com/') assert url.with_auth('zack', '1234') == u'https://*****:*****@github.com/' def test_without_auth_removes_entire_auth_string(self): # No username or password => no-op. url = URLObject(u'https://github.com/') assert url.without_auth() == u'https://github.com/' # Username-only. url = URLObject(u'https://[email protected]/') assert url.without_auth() == u'https://github.com/' # Username and password. url = URLObject(u'https://*****:*****@github.com/') assert url.without_auth() == u'https://github.com/' def test_with_port_adds_port_number(self): assert (self.url.with_port(24) == u'https://github.com:24/zacharyvoase/urlobject?spam=eggs#foo') def test_with_port_replaces_port_number(self): url = URLObject(u'https://github.com:59/') assert url.with_port(67) == u'https://github.com:67/' def test_without_port_removes_port_number(self): url = URLObject(u'https://github.com:59/') assert url.without_port() == u'https://github.com/' def test_with_path_replaces_path(self): assert (self.url.with_path('/dvxhouse/intessa') == u'https://github.com/dvxhouse/intessa?spam=eggs#foo') def test_root_goes_to_root_path(self): assert self.url.root == u'https://github.com/?spam=eggs#foo' def test_parent_jumps_up_one_level(self): url = URLObject(u'https://github.com/zacharyvoase/urlobject') assert url.parent == u'https://github.com/zacharyvoase/' assert url.parent.parent == u'https://github.com/' def test_add_path_segment_adds_a_path_segment(self): url = URLObject(u'https://github.com/zacharyvoase/urlobject') assert (url.add_path_segment('tree') == u'https://github.com/zacharyvoase/urlobject/tree') assert (url.add_path_segment('tree/master') == u'https://github.com/zacharyvoase/urlobject/tree%2Fmaster') def test_add_path_adds_a_partial_path(self): url = URLObject(u'https://github.com/zacharyvoase/urlobject') assert (url.add_path('tree') == u'https://github.com/zacharyvoase/urlobject/tree') assert (url.add_path('tree/master') == u'https://github.com/zacharyvoase/urlobject/tree/master') def test_is_leaf(self): assert URLObject(u'https://github.com/zacharyvoase/urlobject').is_leaf assert not URLObject(u'https://github.com/zacharyvoase/').is_leaf def test_with_query_replaces_query(self): assert (self.url.with_query('spam-ham-eggs') == u'https://github.com/zacharyvoase/urlobject?spam-ham-eggs#foo') def test_without_query_removes_query(self): assert (self.url.without_query() == u'https://github.com/zacharyvoase/urlobject#foo') def test_add_query_param_adds_one_query_parameter(self): assert (self.url.add_query_param(u'spam', u'ham') == u'https://github.com/zacharyvoase/urlobject?spam=eggs&spam=ham#foo') def test_add_query_params_adds_multiple_query_parameters(self): assert (self.url.add_query_params([(u'spam', u'ham'), (u'foo', u'bar')]) == u'https://github.com/zacharyvoase/urlobject?spam=eggs&spam=ham&foo=bar#foo') def test_add_query_params_with_multiple_values_adds_the_same_query_parameter_multiple_times(self): assert (self.url.add_query_params({u'foo': [u'bar', 'baz']}) == u'https://github.com/zacharyvoase/urlobject?spam=eggs&foo=bar&foo=baz#foo') def test_set_query_param_adds_or_replaces_one_query_parameter(self): assert (self.url.set_query_param(u'spam', u'ham') == u'https://github.com/zacharyvoase/urlobject?spam=ham#foo') def test_set_query_params_adds_or_replaces_multiple_query_parameters(self): assert (self.url.set_query_params({u'foo': u'bar'}, spam=u'ham') == u'https://github.com/zacharyvoase/urlobject?foo=bar&spam=ham#foo') def test_set_query_params_with_multiple_values_adds_or_replaces_the_same_parameter_multiple_times(self): assert (self.url.set_query_params({u'spam': [u'bar', 'baz']}) == u'https://github.com/zacharyvoase/urlobject?spam=bar&spam=baz#foo') assert (self.url.set_query_params({u'foo': [u'bar', 'baz']}) == u'https://github.com/zacharyvoase/urlobject?spam=eggs&foo=bar&foo=baz#foo') # Ensure it removes all appearances of an existing name before adding # the new ones. url = URLObject(u'https://github.com/zacharyvoase/urlobject?foo=bar&foo=baz#foo') assert (url.set_query_params({u'foo': [u'spam', u'ham']}) == u'https://github.com/zacharyvoase/urlobject?foo=spam&foo=ham#foo') def test_del_query_param_removes_one_query_parameter(self): assert (self.url.del_query_param(u'spam') == u'https://github.com/zacharyvoase/urlobject#foo') def test_del_query_params_removes_multiple_query_parameters(self): url = URLObject(u'https://github.com/zacharyvoase/urlobject?foo=bar&baz=spam#foo') assert (url.del_query_params(['foo', 'baz']) == u'https://github.com/zacharyvoase/urlobject#foo') def test_with_fragment_replaces_fragment(self): assert (self.url.with_fragment('part') == u'https://github.com/zacharyvoase/urlobject?spam=eggs#part') def test_with_fragment_encodes_fragment_correctly(self): assert (self.url.with_fragment('foo bar#baz') == u'https://github.com/zacharyvoase/urlobject?spam=eggs#foo%20bar%23baz') def test_without_fragment_removes_fragment(self): assert (self.url.without_fragment() == u'https://github.com/zacharyvoase/urlobject?spam=eggs')
def wrap_url(self, url): o_url = URLObject(url) return o_url.add_query_param(settings.URLKEY_NAME, self.id)
def render(self, context): kwargs = MultiValueDict() for key in self.kwargs: key = smart_str(key, 'ascii') values = [value.resolve(context) for value in self.kwargs.getlist(key)] kwargs.setlist(key, values) if 'base' in kwargs: url = URLObject.parse(kwargs['base']) else: url = URLObject(scheme='http') if 'secure' in kwargs: if convert_to_boolean(kwargs['secure']): url = url.with_scheme('https') else: url = url.with_scheme('http') if 'query' in kwargs: query = kwargs['query'] if isinstance(query, basestring): query = render_template_from_string_without_autoescape(query, context) url = url.with_query(query) if 'add_query' in kwargs: for query_to_add in kwargs.getlist('add_query'): if isinstance(query_to_add, basestring): query_to_add = render_template_from_string_without_autoescape(query_to_add, context) query_to_add = dict(decode_query(query_to_add)) for key, value in query_to_add.items(): url = url.add_query_param(key, value) if 'scheme' in kwargs: url = url.with_scheme(kwargs['scheme']) if 'host' in kwargs: url = url.with_host(kwargs['host']) if 'path' in kwargs: url = url.with_path(kwargs['path']) if 'add_path' in kwargs: for path_to_add in kwargs.getlist('add_path'): url = url.add_path_component(path_to_add) if 'fragment' in kwargs: url = url.with_fragment(kwargs['fragment']) if 'port' in kwargs: url = url.with_port(kwargs['port']) # sensible default if not url.host: url = url.with_scheme('') # Convert the URLObject to its unicode representation url = unicode(url) # Handle escaping. By default, use the value of # context.autoescape. This can be overridden by # passing an "autoescape" keyword to the tag. if 'autoescape' in kwargs: autoescape = convert_to_boolean(kwargs['autoescape']) else: autoescape = context.autoescape if autoescape: url = escape(url) if self.asvar: context[self.asvar] = url return '' return url
def unauthorized_handler(): user_unauthorized.send(user_unauthorized.send(current_app._get_current_object())) url = URLObject(login_url(login_manager.login_view, request.url)) prev_param = make_next_param(url.without_query(), request.referrer or "/") return redirect(url.add_query_param("prev", prev_param).add_query_param("mode", "action"))
def get_url_path(cls, system): returned = URL("/api/rest/components") if cls is not system.components.object_type: returned = returned.add_query_param("type", "eq:{0}".format(cls.get_type_name())) return returned
def wrap_url(self, url): o_url = URLObject(url) return o_url.add_query_param(settings.URLKEY_NAME, self.id)