# 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,
)


"""
These middlewares should be placed at the very top of the middleware stack.
Selects the proper database schema using request information. Can fail in
various ways which is better than corrupting or revealing data.

Extend BaseTenantMiddleware for a custom tenant selection strategy,
such as inspecting the header, or extracting it from some OAuth token.
"""


class BaseTenantMiddleware(django.utils.deprecation.MiddlewareMixin):
    TENANT_NOT_FOUND_EXCEPTION = Http404

    """
    Subclass and override  this to achieve desired behaviour. Given a
    request, return the tenant to use. Tenant should be an instance
    of TENANT_MODEL. We have three parameters for backwards compatibility
    (the request would be enough).
    """

    def get_tenant(self, model, hostname, request):
        raise NotImplementedError

    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.
        """

# 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,
)



"""
These middlewares should be placed at the very top of the middleware stack.
Selects the proper database schema using request information. Can fail in
various ways which is better than corrupting or revealing data.

Extend BaseTenantMiddleware for a custom tenant selection strategy,
such as inspecting the header, or extracting it from some OAuth token.
"""


class BaseTenantMiddleware(django.utils.deprecation.MiddlewareMixin):
    TENANT_NOT_FOUND_EXCEPTION = Http404

    """
    Subclass and override  this to achieve desired behaviour. Given a
    request, return the tenant to use. Tenant should be an instance
    of TENANT_MODEL. We have three parameters for backwards compatibility
    (the request would be enough).
    """

    def get_tenant(self, model, hostname, request):
        raise NotImplementedError

    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(request.get_host().split(":")[0]).lower()