Permalink
Browse files

Upgrade to URLObject 2.0.0

  • Loading branch information...
1 parent 557d59e commit 18d9d2ff95f2576dde09f21c7b97ac16c9febdd5 @j4mie committed Feb 23, 2012
Showing with 34 additions and 37 deletions.
  1. +1 −1 requirements.txt
  2. +1 −1 setup.py
  3. +32 −35 spurl/templatetags/spurl.py
View
@@ -1 +1 @@
-urlobject==0.6.0
+urlobject>=2.0.0
View
@@ -26,7 +26,7 @@ def get_version():
author_email='jamie.matthews@gmail.com',
url='http://github.com/j4mie/django-spurl/',
packages=find_packages(),
- install_requires=['urlobject==0.6.0'],
+ install_requires=['urlobject>=2.0.0'],
classifiers = [
'Programming Language :: Python',
'Development Status :: 3 - Alpha',
@@ -2,7 +2,8 @@
from django.conf import settings
from django.utils.html import escape
from django.utils.encoding import smart_str
-from urlobject import URLObject, decode_query
+from urlobject import URLObject
+from urlobject.query_string import QueryString
from django.template import StringOrigin, Lexer, Parser
from django.template.defaulttags import kwarg_re
from django.template import Template, Library, Node, TemplateSyntaxError
@@ -28,7 +29,7 @@ def __init__(self, args, context, tags, filters):
self.tags = tags
self.filters = filters
self.autoescape = self.context.autoescape
- self.url = URLObject(scheme='http')
+ self.url = URLObject()
def build(self):
for argument, value in self.args:
@@ -54,7 +55,7 @@ def handle_argument(self, argument, value):
def handle_base(self, value):
base = self.prepare_value(value)
- self.url = URLObject.parse(base)
+ self.url = URLObject(base)
def handle_secure(self, value):
is_secure = convert_to_boolean(value)
@@ -63,46 +64,42 @@ def handle_secure(self, value):
def handle_query(self, value):
query = self.prepare_value(value)
- self.url = self.url.with_query(query)
+ if isinstance(query, dict):
+ query = QueryString().set_params(**query)
+ self.url = self.url.with_query(QueryString(query))
def handle_query_from(self, value):
- url = URLObject.parse(value)
+ url = URLObject(value)
self.url = self.url.with_query(url.query)
def handle_add_query(self, value):
query_to_add = self.prepare_value(value)
if isinstance(query_to_add, basestring):
- query_to_add = dict(decode_query(query_to_add))
- for key, value in query_to_add.items():
- self.url = self.url.add_query_param(key, value)
+ query_to_add = QueryString(query_to_add).dict
+ self.url = self.url.add_query_params(**query_to_add)
def handle_add_query_from(self, value):
- url = URLObject.parse(value)
- query_to_add = dict(decode_query(url.query))
- for key, value in query_to_add.items():
- self.url = self.url.add_query_param(key, value)
+ url = URLObject(value)
+ self.url = self.url.add_query_params(**url.query.dict)
def handle_set_query(self, value):
query_to_set = self.prepare_value(value)
if isinstance(query_to_set, basestring):
- query_to_set = dict(decode_query(query_to_set))
- for key, value in query_to_set.items():
- self.url = self.url.set_query_param(key, value)
+ query_to_set = QueryString(query_to_set).dict
+ self.url = self.url.set_query_params(**query_to_set)
def handle_set_query_from(self, value):
- url = URLObject.parse(value)
- query_to_set = dict(decode_query(url.query))
- for key, value in query_to_set.items():
- self.url = self.url.set_query_param(key, value)
+ url = URLObject(value)
+ self.url = self.url.set_query_params(**url.query.dict)
def handle_remove_query_param(self, value):
- self.url = self.url.without_query_param(value)
+ self.url = self.url.del_query_param(value)
def handle_toggle_query(self, value):
query_to_toggle = self.prepare_value(value)
if isinstance(query_to_toggle, basestring):
- query_to_toggle = dict(decode_query(query_to_toggle))
- current_query = self.url.query_dict()
+ query_to_toggle = QueryString(query_to_toggle).dict
+ current_query = self.url.query.dict
for key, value in query_to_toggle.items():
if isinstance(value, basestring):
value = value.split(',')
@@ -116,57 +113,57 @@ def handle_scheme(self, value):
self.url = self.url.with_scheme(value)
def handle_scheme_from(self, value):
- url = URLObject.parse(value)
+ url = URLObject(value)
self.url = self.url.with_scheme(url.scheme)
def handle_host(self, value):
host = self.prepare_value(value)
- self.url = self.url.with_host(host)
+ self.url = self.url.with_hostname(host)
def handle_host_from(self, value):
- url = URLObject.parse(value)
- self.url = self.url.with_host(url.host)
+ url = URLObject(value)
+ self.url = self.url.with_hostname(url.hostname)
def handle_path(self, value):
path = self.prepare_value(value)
self.url = self.url.with_path(path)
def handle_path_from(self, value):
- url = URLObject.parse(value)
+ url = URLObject(value)
self.url = self.url.with_path(url.path)
def handle_add_path(self, value):
path_to_add = self.prepare_value(value)
- self.url = self.url.add_path_component(path_to_add)
+ self.url = self.url.add_path(path_to_add)
def handle_add_path_from(self, value):
- url = URLObject.parse(value)
+ url = URLObject(value)
path_to_add = url.path
if path_to_add.startswith('/'):
path_to_add = path_to_add[1:]
- self.url = self.url.add_path_component(path_to_add)
+ self.url = self.url.add_path(path_to_add)
def handle_fragment(self, value):
fragment = self.prepare_value(value)
self.url = self.url.with_fragment(fragment)
def handle_fragment_from(self, value):
- url = URLObject.parse(value)
+ url = URLObject(value)
self.url = self.url.with_fragment(url.fragment)
def handle_port(self, value):
- self.url = self.url.with_port(value)
+ self.url = self.url.with_port(int(value))
def handle_port_from(self, value):
- url = URLObject.parse(value)
+ url = URLObject(value)
self.url = self.url.with_port(url.port)
def handle_autoescape(self, value):
self.autoescape = convert_to_boolean(value)
def set_sensible_defaults(self):
- if not self.url.host:
- self.url = self.url.with_scheme('')
+ if self.url.hostname and not self.url.scheme:
+ self.url = self.url.with_scheme('http')
def prepare_value(self, value):
"""Prepare a value by unescaping embedded template tags

0 comments on commit 18d9d2f

Please sign in to comment.