diff options
| author | Vladimir Gorej | 2012-06-20 21:13:38 +0200 | 
|---|---|---|
| committer | Vladimir Gorej | 2012-06-20 21:13:38 +0200 | 
| commit | a45e3e9f98aac03c61a1167e61ff15be3e45c1b4 (patch) | |
| tree | 2fc036d819665377eb4b2b828dc45452b454f70c /brevisurl/utils.py | |
| parent | 178d820541e874fe9913080c5125876b9c0b82ca (diff) | |
| download | django-brevisurl-a45e3e9f98aac03c61a1167e61ff15be3e45c1b4.tar.bz2 | |
Issue #9; absurl util for constructing absolute urls directly in code
Diffstat (limited to 'brevisurl/utils.py')
| -rw-r--r-- | brevisurl/utils.py | 31 | 
1 files changed, 30 insertions, 1 deletions
| diff --git a/brevisurl/utils.py b/brevisurl/utils.py index e0c3d52..b023554 100644 --- a/brevisurl/utils.py +++ b/brevisurl/utils.py @@ -1,4 +1,7 @@  from django.utils import importlib +from django.contrib.sites.models import Site + +import brevisurl.settings  def load_object(import_path): @@ -15,4 +18,30 @@ def load_object(import_path):          raise ValueError('There must be at least one dot in import path: "%s"', import_path)      module_name, object_name = import_path.rsplit('.', 1)      module = importlib.import_module(module_name) -    return getattr(module, object_name)
\ No newline at end of file +    return getattr(module, object_name) + + +def absurl(protocol=brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL, +           domain=None, site=None, path='/'): +    """Util for constructing absolute urls from relative urls. + +    Keyword argument domain has higher priority over site. If site not set +    domain is used. If both are not set, current site is used. + +    :param protocol: domain protocol +    :type protocol: string +    :param domain: URI domain +    :type domain: string +    :param site: Site instance +    :type site: django.contrib.sites.models.Site +    :param path: URI path +    :type path: string +    :returns: absolute URI +    :rtype: string + +    """ +    if domain is None and site is None: +        domain = Site.objects.get_current().domain +    elif domain is None and site is not None: +        domain = site.domain +    return '{0}://{1}{2}'.format(protocol, domain, path)
\ No newline at end of file | 
