aboutsummaryrefslogtreecommitdiffstats
path: root/brevisurl/utils.py
blob: b0235542af8f655302dfb71fc0dd4b7ec94e528e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django.utils import importlib
from django.contrib.sites.models import Site

import brevisurl.settings


def load_object(import_path):
    """Util for importing objects from import path.

    :param import_path: import path of object to be imported e.g. module.submodule.Class
    :type import_path: string
    :returns: imported object
    :rtype: object
    :raises: ValueError, ImportError, AttributeError

    """
    if not (isinstance(import_path, basestring) and '.' in 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)


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)