"""
Domain middleware: enables multi-tenancy in a single process
"""
from anaf.core.domains import setup_domain, setup_domain_database
from anaf.core.db import DatabaseNotFound
from anaf.core.conf import settings
from django.http import HttpResponseRedirect
from django.db.utils import DatabaseError
from django.core.urlresolvers import reverse
from pandora import box


class DomainMiddleware(object):
    """Handles multiple domains within the same Django process"""

    def process_request(self, request):
        """Identify the current domain and database, set up appropriate variables in the pandora box"""

        domain = request.get_host().split('.')[0]
        try:
            setup_domain(domain)
        except DatabaseNotFound:
            evergreen_url = getattr(
                settings, 'EVERGREEN_BASE_URL', 'http://tree.io/')
            return HttpResponseRedirect(evergreen_url)
        except DatabaseError:
            from django.db import router
            from anaf.core.models import ConfigSetting
            setup_domain_database(router.db_for_read(ConfigSetting))
            return HttpResponseRedirect(reverse('database_setup'))
        box['request'] = request

    def process_exception(self, request, exception):
        if isinstance(exception, DatabaseNotFound):
            evergreen_url = getattr(
                settings, 'EVERGREEN_BASE_URL', 'http://tree.io/')
            return HttpResponseRedirect(evergreen_url)

"""
Domain middleware: enables multi-tenancy in a single process
"""
from anaf.core.domains import setup_domain, setup_domain_database
from anaf.core.db import DatabaseNotFound
from anaf.core.conf import settings
from django.http import HttpResponseRedirect
from django.db.utils import DatabaseError
from django.core.urlresolvers import reverse
from pandora import box


class DomainMiddleware(object):
    """Handles multiple domains within the same Django process"""

    def process_request(self, request):
        """Identify the current domain and database, set up appropriate variables in the pandora box"""

        domain = request.get_host().split('.')[0]
        try:
            setup_domain(domain)
        except DatabaseNotFound:
            evergreen_url = getattr(
                settings, 'EVERGREEN_BASE_URL', 'http://tree.io/')
            return HttpResponseRedirect(evergreen_url)
        except DatabaseError:
            from django.db import router
            from anaf.core.models import ConfigSetting
            setup_domain_database(router.db_for_read(ConfigSetting))
            return HttpResponseRedirect(reverse('database_setup'))
        box['request'] = request

    def process_exception(self, request, exception):
        if isinstance(exception, DatabaseNotFound):
            evergreen_url = getattr(
                settings, 'EVERGREEN_BASE_URL', 'http://tree.io/')
            return HttpResponseRedirect(evergreen_url)

Domain middleware: enables multi-tenancy in a single process Handles multiple domains within the same Django process Identify the current domain and database, set up appropriate variables in the pandora box