src/ldt/ldt/utils/url.py
author grandjoncl
Wed, 28 Nov 2012 12:31:27 +0100
changeset 1003 c2d9d1c6c228
parent 922 cba34a867804
child 1007 a6d47caa7fc0
permissions -rw-r--r--
Merge with 8251c6a02d6fc48c19f241f8055ceac0017ac457

from django.contrib.staticfiles.storage import staticfiles_storage
from django.contrib.sites.models import Site
from ldt import settings
import httplib2
import re
import urlparse


def absstatic(request, path):
    if request.is_secure():
        domain = "https://%s" % Site.objects.get_current().domain
    else:
        domain = "http://%s" % Site.objects.get_current().domain
    new_path = staticfiles_storage.url(path)
    return urlparse.urljoin(domain, new_path)

def request_with_auth(url, method='GET'):
    h = httplib2.Http()
    web_auth = settings.WEB_AUTH if settings.WEB_AUTH else []
    for entry in web_auth:
        if not isinstance(entry, dict):
            continue
        regex = entry.get('REGEX', None)
        if regex and re.search(regex, url, re.IGNORECASE):
            h.add_credentials(entry.get('NAME', ''), entry.get('PASSWORD', ''), entry.get('DOMAIN', ''))
            break
            
    return h.request(url, method)


def is_absolute(url):
    return bool(urlparse.urlparse(url).scheme)