Exemplo n.º 1
0
    def process_request(self, request):
        """
        Resets to public schema

        Some nasty weird bugs happened at the production environment without this call.
        connection.pg_thread.schema_name would already be set and then terrible errors
        would occur. Any idea why? My theory is django implements connection as some sort
        of threading local variable.
        """
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(request.get_host().split(':')[0])

        TenantModel = get_tenant_model()
        request.tenant = get_object_or_404(TenantModel, domain_url=hostname_without_port)
        connection.set_tenant(request.tenant)

        # content type can no longer be cached as public and tenant schemas have different
        # models. if someone wants to change this, the cache needs to be separated between
        # public and shared schemas. if this cache isn't cleared, this can cause permission
        # problems. for example, on public, a particular model has id 14, but on the tenants
        # it has the id 15. if 14 is cached instead of 15, the permissions for the wrong
        # model will be fetched.
        ContentType.objects.clear_cache()

        # do we have a public-specific token?
        if hasattr(settings, 'PUBLIC_SCHEMA_URL_TOKEN') and request.tenant.schema_name == get_public_schema_name():
            request.path_info = settings.PUBLIC_SCHEMA_URL_TOKEN + request.path_info
Exemplo n.º 2
0
 def get_tenant(self, host):
     hostname_without_port = remove_www_and_dev(host.split(":")[0])
     print hostname_without_port
     try:
         return self.TenantModel.objects.get(domain_url=hostname_without_port)
     except Exception as e:
         return self.TenantModel.objects.get(pk=1)
Exemplo n.º 3
0
    def process_request(self, request):
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(
            request.get_host().split(':')[0])

        TenantModel = get_tenant_model()

        try:
            request.tenant = TenantModel.objects.get(
                domain_url=hostname_without_port)

        except utils.DatabaseError:
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
            return
        except TenantModel.DoesNotExist:
            if hostname_without_port in ("127.0.0.1", "localhost", "multidemo.com"):
                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

        if hasattr(settings, "API_URLCONF") and request.tenant.name == 'API':
            self.urlconf = settings.API_URLCONF
Exemplo n.º 4
0
    def process_request(self, request):
        """
        Resets to public schema

        Some nasty weird bugs happened at the production environment without this call.
        connection.pg_thread.schema_name would already be set and then terrible errors
        would occur. Any idea why? My theory is django implements connection as some sort
        of threading local variable.
        """
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(
            request.get_host().split(':')[0])

        TenantModel = get_tenant_model()
        request.tenant = get_object_or_404(TenantModel,
                                           domain_url=hostname_without_port)
        connection.set_tenant(request.tenant)

        # do we have tenant-specific URLs?
        if hasattr(
                settings, 'PUBLIC_SCHEMA_URL_TOKEN'
        ) and request.tenant.schema_name == "public" and request.path_info[
                -1] == '/':
            # we are not at the public schema, manually alter routing to schema-dependent urls
            request.path_info = settings.PUBLIC_SCHEMA_URL_TOKEN + request.path_info
Exemplo n.º 5
0
    def process_request(self, request):
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(
            request.get_host().split(':')[0])

        TenantModel = get_tenant_model()

        try:
            request.tenant = TenantModel.objects.get(
                domain_url=hostname_without_port)
        except utils.DatabaseError:
            request.urlconf = settings.ROOT_URLCONF
            return
        except TenantModel.DoesNotExist:
            if hostname_without_port in ("127.0.0.1", "localhost"):
                request.urlconf = settings.ROOT_URLCONF
                return
            else:
                raise Http404

        connection.set_tenant(request.tenant)
        # ContentType.objects.clear_cache()
        # Session.objects.all().delete()

        if hasattr(
                settings, 'ROOT_URLCONF'
        ) and request.tenant.schema_name == get_public_schema_name():
            request.urlconf = settings.ROOT_URLCONF
Exemplo n.º 6
0
def auth_login(request):
    # Documentação de autenticação na url abaixo:
    # https://docs.djangoproject.com/en/dev/topics/auth/default/
    context = {'hostname': remove_www_and_dev(request.get_host().split('.')[0])}

    if request.method == 'POST':
        username = request.POST['email']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/')
        return render(request, 'customers/login.html', context)
    else:
        if request.user.is_authenticated():
            return HttpResponseRedirect('/')
        else:
            return render(request, 'customers/login.html', context)
Exemplo n.º 7
0
    def process_request(self, request):
        """
        Resets to public schema

        Some nasty weird bugs happened at the production environment without this call.
        connection.pg_thread.schema_name would already be set and then terrible errors
        would occur. Any idea why? My theory is django implements connection as some sort
        of threading local variable.
        """
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(request.get_host().split(':')[0])

        TenantModel = get_tenant_model()
        request.tenant = get_object_or_404(TenantModel, domain_url=hostname_without_port)
        connection.set_tenant(request.tenant)

        # do we have tenant-specific URLs?
        if settings.TENANT_URL_TOKEN and request.tenant.schema_name != "public" and request.path_info[-1] == '/':
            # we are not at the public schema, manually alter routing to schema-dependent urls
            request.path_info = settings.TENANT_URL_TOKEN + request.path_info
Exemplo n.º 8
0
    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)

        hostname_without_port = remove_www_and_dev(self.request.get_host().split(":")[0])
        try:
            Client.objects.get(schema_name="public")
        except utils.DatabaseError:
            context["need_sync"] = True
            context["shared_apps"] = settings.SHARED_APPS
            context["tenants_list"] = []
            return context
        except Client.DoesNotExist:
            context["no_public_tenant"] = True
            context["hostname"] = hostname_without_port

        if Client.objects.count() == 1:
            context["only_public_tenant"] = True

        context["tenants_list"] = Client.objects.all()
        return context
Exemplo n.º 9
0
    def process_request(self, request):
        # connection needs first to be at the public schema, as this is where the
        # tenant informations are saved
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(request.get_host().split(':')[0])

        request.tenant = get_object_or_404(get_tenant_model(), domain_url=hostname_without_port)
        connection.set_tenant(request.tenant)

        # content type can no longer be cached as public and tenant schemas have different
        # models. if someone wants to change this, the cache needs to be separated between
        # public and shared schemas. if this cache isn't cleared, this can cause permission
        # problems. for example, on public, a particular model has id 14, but on the tenants
        # it has the id 15. if 14 is cached instead of 15, the permissions for the wrong
        # model will be fetched.
        ContentType.objects.clear_cache()

        # do we have a public-specific token?
        if hasattr(settings, 'PUBLIC_SCHEMA_URLCONF') and request.tenant.schema_name == get_public_schema_name():
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
Exemplo n.º 10
0
    def get_context_data(self, **kwargs):
        context = super(HomeView, self).get_context_data(**kwargs)

        hostname_without_port = remove_www_and_dev(self.request.get_host().split(':')[0])
        try:
            Client.objects.get(schema_name='public')
        except utils.DatabaseError:
            context['need_sync'] = True
            context['shared_apps'] = settings.SHARED_APPS
            context['tenants_list'] = []
            return context
        except Client.DoesNotExist:
            context['no_public_tenant'] = True
            context['hostname'] = hostname_without_port

        if Client.objects.count() == 1:
            context['only_public_tenant'] = True

        context['tenants_list'] = Client.objects.all()
        return context
    def process_request(self, request):
        """
        Resets to public schema

        Some nasty weird bugs happened at the production environment without this call.
        connection.pg_thread.schema_name would already be set and then terrible errors
        would occur. Any idea why? My theory is django implements connection as some sort
        of threading local variable.
        """
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(
            request.get_host().split(':')[0])

        TenantModel = get_tenant_model()
        request.tenant = get_object_or_404(TenantModel,
                                           domain_url=hostname_without_port)
        connection.set_tenant(request.tenant)

        # content type can no longer be cached as public and tenant schemas have different
        # models. if someone wants to change this, the cache needs to be separated between
        # public and shared schemas. if this cache isn't cleared, this can cause permission
        # problems. for example, on public, a particular model has id 14, but on the tenants
        # it has the id 15. if 14 is cached instead of 15, the permissions for the wrong
        # model will be fetched.
        ContentType.objects.clear_cache()

        # do we have a public-specific token?
        if hasattr(
                settings, 'PUBLIC_SCHEMA_URL_TOKEN'
        ) and request.tenant.schema_name == get_public_schema_name():
            request.path_info = settings.PUBLIC_SCHEMA_URL_TOKEN + request.path_info
            warnings.warn(
                "PUBLIC_SCHEMA_URL_TOKEN is deprecated. Use PUBLIC_SCHEMA_URLCONF instead.",
                category=DeprecationWarning)

        if hasattr(
                settings, 'PUBLIC_SCHEMA_URLCONF'
        ) and request.tenant.schema_name == get_public_schema_name():
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
Exemplo n.º 12
0
    def process_request(self, request):
        connection.set_schema_to_public()
        hostname_without_port = remove_www_and_dev(request.get_host().split(':')[0])

        TenantModel = get_tenant_model()

        try:
            request.tenant = TenantModel.objects.get(domain_url=hostname_without_port)
        except utils.DatabaseError:
            request.urlconf = settings.PUBLIC_SCHEMA_URLCONF
            return
        except TenantModel.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
Exemplo n.º 13
0
 def hostname_from_request(self, request):
     """ Extracts hostname from request. Used for custom requests filtering.
         By default removes the request's port and common prefixes.
     """
     return remove_www_and_dev(request.get_host().split(':')[0])
Exemplo n.º 14
0
def main(request):
    context = {'hostname': remove_www_and_dev(request.get_host().split('.')[0])}
    return render(request, 'customers/dashboard.html', context)