Package madrona :: Package common :: Package templatetags :: Module absurl
[hide private]

Source Code for Module madrona.common.templatetags.absurl

 1  import urlparse 
 2  from django.template import Library 
 3  from django.templatetags.future import url, URLNode 
 4  from django.contrib.sites.models import Site 
 5   
 6  register = Library() 
 7   
8 -class AbsoluteURLNode(URLNode):
9 - def __init__(self, view_name, args, kwargs, asvar, legacy_view_name=True):
10 super(AbsoluteURLNode, self).__init__(view_name, 11 args, 12 kwargs, 13 None, 14 legacy_view_name) 15 self.abs_asvar = asvar
16
17 - def render(self, context):
18 path = super(AbsoluteURLNode, self).render(context) 19 domain = "http://%s" % Site.objects.get_current().domain 20 url = urlparse.urljoin(domain, path) 21 22 if self.abs_asvar: 23 context[self.abs_asvar] = url 24 return '' 25 else: 26 return url
27
28 -def absurl(parser, token):
29 """Just like {% url 'urlname' %} but ads the domain of the current site.""" 30 node_instance = url(parser, token) 31 return AbsoluteURLNode(view_name=node_instance.view_name, 32 args=node_instance.args, 33 kwargs=node_instance.kwargs, 34 asvar=node_instance.asvar, 35 legacy_view_name=node_instance.legacy_view_name)
36 37 absurl = register.tag(absurl) 38