# Path: tenant_schemas/utils.py
# def get_public_schema_name():
#     return getattr(settings, 'PUBLIC_SCHEMA_NAME', 'public')
# 
# def get_tenant_model():
#     return get_model(*settings.TENANT_MODEL.split("."))
# 
# def remove_www(hostname):
#     """
#     Removes www. from the beginning of the address. Only for
#     routing purposes. www.test.com/login/ and test.com/login/ should
#     find the same tenant.
#     """
#     if hostname.startswith("www."):
#         return hostname[4:]
# 
#     return hostname


import django
from django.conf import settings
from django.core.exceptions import DisallowedHost
from django.db import connection
from django.http import Http404
from tenant_schemas.utils import (
    get_public_schema_name,
    get_tenant_model,
    remove_www,
)
    See https://github.com/bernardopires/django-tenant-schemas/pull/269 for
    discussion on this middleware.
    """

    TENANT_NOT_FOUND_EXCEPTION = DisallowedHost


class DefaultTenantMiddleware(SuspiciousTenantMiddleware):
    """
    Extend the SuspiciousTenantMiddleware in scenario where you want to
    configure a tenant to be served if the hostname does not match any of the
    existing tenants.

    Subclass and override DEFAULT_SCHEMA_NAME to use a schema other than the
    public schema.

        class MyTenantMiddleware(DefaultTenantMiddleware):
            DEFAULT_SCHEMA_NAME = 'default'
    """

    DEFAULT_SCHEMA_NAME = None

    def get_tenant(self, model, hostname, request):
        try:
            return super(DefaultTenantMiddleware, self).get_tenant(
                model, hostname, request
            )
        except model.DoesNotExist:
            schema_name = self.DEFAULT_SCHEMA_NAME
            if not schema_name:

# Path: tenant_schemas/utils.py
# def get_public_schema_name():
#     return getattr(settings, 'PUBLIC_SCHEMA_NAME', 'public')
# 
# def get_tenant_model():
#     return get_model(*settings.TENANT_MODEL.split("."))
# 
# def remove_www(hostname):
#     """
#     Removes www. from the beginning of the address. Only for
#     routing purposes. www.test.com/login/ and test.com/login/ should
#     find the same tenant.
#     """
#     if hostname.startswith("www."):
#         return hostname[4:]
# 
#     return hostname


# Path: tenant_schemas/middleware.py
import django
from django.conf import settings
from django.core.exceptions import DisallowedHost
from django.db import connection
from django.http import Http404
from tenant_schemas.utils import (
    get_public_schema_name,
    get_tenant_model,
    remove_www,
)

    See https://github.com/bernardopires/django-tenant-schemas/pull/269 for
    discussion on this middleware.
    """

    TENANT_NOT_FOUND_EXCEPTION = DisallowedHost


class DefaultTenantMiddleware(SuspiciousTenantMiddleware):
    """
    Extend the SuspiciousTenantMiddleware in scenario where you want to
    configure a tenant to be served if the hostname does not match any of the
    existing tenants.

    Subclass and override DEFAULT_SCHEMA_NAME to use a schema other than the
    public schema.

        class MyTenantMiddleware(DefaultTenantMiddleware):
            DEFAULT_SCHEMA_NAME = 'default'
    """

    DEFAULT_SCHEMA_NAME = None

    def get_tenant(self, model, hostname, request):
        try:
            return super(DefaultTenantMiddleware, self).get_tenant(
                model, hostname, request
            )
        except model.DoesNotExist:
            schema_name = self.DEFAULT_SCHEMA_NAME
            if not schema_name:

                schema_name = get_public_schema_name()