Python django.http.request.split_domain_port() Examples

The following are 6 code examples of django.http.request.split_domain_port(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module django.http.request , or try the search function .
Example #1
Source File: model_utils.py    From janeway with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_by_request(cls, request):
        domain = request.get_host()
        # Lookup by domain with/without port
        try:
            obj = cls.objects.get(domain=domain)
        except cls.DoesNotExist:
            # Lookup without port
            domain, _port = split_domain_port(domain)
            obj = cls.objects.get(domain=domain)
        return obj 
Example #2
Source File: models.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _find_for_request(request):
        hostname = split_domain_port(request.get_host())[0]
        port = request.get_port()
        site = None
        try:
            site = get_site_for_hostname(hostname, port)
        except Site.DoesNotExist:
            pass
            # copy old SiteMiddleware behavior
        return site 
Example #3
Source File: models.py    From openhgsenti with Apache License 2.0 5 votes vote down vote up
def _get_site_by_request(self, request):
        host = request.get_host()
        try:
            # First attempt to look up the site by host with or without port.
            if host not in SITE_CACHE:
                SITE_CACHE[host] = self.get(domain__iexact=host)
            return SITE_CACHE[host]
        except Site.DoesNotExist:
            # Fallback to looking up site after stripping port from the host.
            domain, port = split_domain_port(host)
            if not port:
                raise
            if domain not in SITE_CACHE:
                SITE_CACHE[domain] = self.get(domain__iexact=domain)
            return SITE_CACHE[domain] 
Example #4
Source File: views.py    From django-cc with MIT License 5 votes vote down vote up
def cc_validate_host(func):
    def validate(request):
        domain, port = split_domain_port(request.META['HTTP_HOST'])
        if not validate_host(domain, settings.CC_ALLOWED_HOSTS):
            return HttpResponseForbidden('forbiden')

        return func(request)

    return validate 
Example #5
Source File: tests.py    From djongo with GNU Affero General Public License v3.0 5 votes vote down vote up
def test_split_domain_port_removes_trailing_dot(self):
        domain, port = split_domain_port('example.com.:8080')
        self.assertEqual(domain, 'example.com')
        self.assertEqual(port, '8080') 
Example #6
Source File: site.py    From timestrap with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __call__(self, request):
        domain, port = split_domain_port(request.get_host())

        try:
            current_site = Site.objects.get(domain=domain)
        except Site.DoesNotExist:
            current_site = Site.objects.get(id=settings.SITE_ID)

        request.site = current_site
        _thread_local.request = request

        return self.get_response(request)