Cleaner join_url helper method

This commit is contained in:
Alvaro Lopez Garcia 2015-04-07 18:06:40 +02:00
parent edac5e2383
commit 1adaa68be9
3 changed files with 11 additions and 9 deletions

View File

@ -44,4 +44,4 @@ class Category(object):
@property
def type_id(self):
return helpers.join_url(self.scheme, "", fragments=self.term)
return helpers.join_url(self.scheme, "#%s" % self.term)

View File

@ -32,7 +32,11 @@ def check_type(obj_list, obj_type):
raise TypeError('object must be of class %s' % obj_type)
def join_url(prefix, remainder, fragments=None):
if fragments:
remainder = "%s#%s" % (remainder, fragments)
return urlparse.urljoin(prefix, remainder)
def join_url(base, parts):
url = base
if not isinstance(parts, (list, tuple)):
parts = [parts]
for p in parts:
url = urlparse.urljoin(url, p)
return url

View File

@ -56,13 +56,11 @@ class KindRenderer(CategoryRenderer):
class ActionRenderer(CategoryRenderer):
def render(self, instance=None, env={}):
# FIXME(aloga): ugly code
# We have an instance id, render it as a link
if instance is not None:
url = env.get("application_url", "")
url = helpers.join_url(url, instance)
d = {"location": helpers.join_url(url, self.obj.location),
url = helpers.join_url(url, [instance, self.obj.location])
d = {"location": url,
"rel": self.obj.type_id}
link = "<%(location)s>; rel=%(rel)s" % d
return [('Link', link)]