コード例 #1
0
    def process_request(self, request):
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(
            request.get_host().split(':')[0])

        try:
            domain = get_tenant_domain_model().objects.select_related(
                'tenant').get(domain=hostname_without_port)
            request.tenant = domain.tenant
        except utils.DatabaseError:
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
            return
        except get_tenant_domain_model().DoesNotExist:
            if hostname_without_port in ("127.0.0.1", "localhost"):
                request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
                return
            else:
                raise Http404

        connection.set_tenant(request.tenant)
        ContentType.objects.clear_cache()

        if hasattr(
                settings, 'PUBLIC_SCHEMA_URLCONF'
        ) and request.tenant.schema_name == get_public_schema_name():
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
コード例 #2
0
 def no_tenant_found(self, request, hostname):
     hostname_without_port = remove_www_and_dev(request.get_host().split(':')[0])
     if hostname_without_port in ("127.0.0.1", "localhost"):
         request.urlconf = get_public_schema_urlconf()
         return
     else:
         raise Http404
コード例 #3
0
ファイル: middleware.py プロジェクト: humblesami/tenants
    def process_request(self, request):
        try:
            connection.set_schema_to_public()
            hostname_without_port = remove_www_and_dev(
                request.get_host().split(':')[0])
            tenant_model = get_tenant_model()
            request.main_url = MAIN_URL
            request.login_url = LOGIN_URL
            request.protocol = PROTOCOL
            if hostname_without_port.startswith('login.'):
                hostname_without_port = hostname_without_port.replace(
                    'login.', '')

            tenant = tenant_model.objects.filter(
                domain_name=hostname_without_port)
            if tenant:
                tenant = tenant[0]
                request.tenant = tenant
                selected_schema_name = tenant.schema_name
            else:
                res = {
                    'error':
                    'Subdomain ' + hostname_without_port + ' does not exist',
                    'status': 'Invalid Client'
                }
                not_found = {
                    'error': 'This subdomain does not exist => ' +
                    hostname_without_port,
                    'error_code': 404
                }
                if hostname_without_port != server_domain:
                    return render(request, 'error.html', not_found)
                tenant = tenant_model.objects.filter(schema_name='public')
                if not tenant:
                    tenant = create_public_tenant(tenant_model)
                    if type(tenant) is not str:
                        request.tenant = tenant
                        selected_schema_name = tenant.schema_name
                    else:
                        return render(request, 'error.html', not_found)
                else:
                    tenant = tenant[0]
                    request.tenant = tenant
                    selected_schema_name = tenant.schema_name
        except utils.DatabaseError:
            request.urlconf = PUBLIC_SCHEMA_URLCONF
            return
        connection.set_tenant(request.tenant, False)
        ContentType.objects.clear_cache()
        if selected_schema_name == 'public':
            request.home_url = PROTOCOL + "://" + server_domain + SERVER_PORT_STR
            request.urlconf = PUBLIC_SCHEMA_URLCONF
        else:
            request.home_url = PROTOCOL + "://" + selected_schema_name + '.' + server_domain + SERVER_PORT_STR
コード例 #4
0
    def process_request(self, request):
        request.META['SMS-CONTEXT-EXIST'] = False
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(
            request.get_host().split(':')[0])
        subdomain = hostname_without_port.split('.')[0]
        sms_context_processor = "sms.context_processors.school_setting_processor"
        context_processors = settings.TEMPLATES[0]['OPTIONS'][
            'context_processors']

        if subdomain == 'admin':
            request.urlconf = settings.ADMIN_URLCONF
            return

        elif subdomain == "www":
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
            return

        else:
            try:
                domain = get_tenant_domain_model().objects.select_related(
                    'tenant').get(domain=hostname_without_port)
                request.tenant = domain.tenant
                if sms_context_processor not in context_processors:
                    context_processors.append(sms_context_processor)
                request.META['SMS-CONTEXT-EXIST'] = True
            except utils.DatabaseError:
                request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
                return
            except get_tenant_domain_model().DoesNotExist:
                request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
                return

            connection.set_tenant(request.tenant)
            ContentType.objects.clear_cache()

            if hasattr(
                    settings, 'PUBLIC_SCHEMA_URLCONF'
            ) and request.tenant.schema_name == get_public_schema_name():
                request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
コード例 #5
0
    def process_request(self, request):
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(request.get_host().split(':')[0])

        try:
            domain = get_tenant_domain_model().objects.select_related('tenant').get(domain=hostname_without_port)
            request.tenant = domain.tenant
        except utils.DatabaseError:
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
            return
        except get_tenant_domain_model().DoesNotExist:
            if hostname_without_port in ("127.0.0.1", "localhost"):
                request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
                return
            else:
                raise Http404

        connection.set_tenant(request.tenant)
        ContentType.objects.clear_cache()

        if hasattr(settings, 'PUBLIC_SCHEMA_URLCONF') and request.tenant.schema_name == get_public_schema_name():
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF