# Path: boardinghouse/exceptions.py
# class Forbidden(Exception):
#     """
#     An exception that will be raised when an attempt to activate a non-valid
#     schema is made.
#     """
# 
# class TemplateSchemaActivation(Forbidden):
#     """
#     An exception that will be raised when a user attempts to activate
#     the settings.TEMPLATE_SCHEMA schema.
#     """
#     def __init__(self, *args, **kwargs):
#         super(TemplateSchemaActivation, self).__init__(
#             'Activating template schema forbidden.', *args, **kwargs
#         )
# 
# class SchemaNotFound(Exception):
#     """
#     An exception that is raised when an attempt to activate a schema that
#     is not in the database is made.
#     """
# 
# Path: boardinghouse/schema.py
# def activate_schema(schema_name):
#     """
#     Activate the current schema: this will execute, in the database
#     connection, something like:
# 
#     .. code:: sql
# 
#         SET search_path TO "foo",public;
# 
#     It sends signals before and after that the schema will be, and was
#     activated.
# 
#     Must be passed a string: the internal name of the schema to activate.
#     """
#     from .signals import schema_pre_activate, schema_post_activate
# 
#     if schema_name == settings.TEMPLATE_SCHEMA:
#         raise TemplateSchemaActivation()
# 
#     schema_pre_activate.send(sender=None, schema_name=schema_name)
#     _set_search_path(schema_name)
#     found_schema = _get_search_path()
#     if found_schema != schema_name:
#         raise SchemaNotFound('Schema activation failed. Expected "{0}", saw "{1}"'.format(
#             schema_name, found_schema,
#         ))
#     schema_post_activate.send(sender=None, schema_name=schema_name)
#     _thread_locals.schema = schema_name
# 
# def deactivate_schema(schema=None):
#     """
#     Deactivate the provided (or current) schema.
#     """
#     from .signals import schema_pre_activate, schema_post_activate
# 
#     cursor = connection.cursor()
#     schema_pre_activate.send(sender=None, schema_name=None)
#     cursor.execute('SET search_path TO "$user",{0}'.format(settings.PUBLIC_SCHEMA))
#     schema_post_activate.send(sender=None, schema_name=None)
#     _thread_locals.schema = None
#     cursor.close()
# 
# Path: boardinghouse/signals.py



import logging
import re
from django.conf import settings
from django.contrib import messages
from django.db import ProgrammingError
from django.http import (
    HttpResponse, HttpResponseForbidden, HttpResponseRedirect,
)
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _
from .exceptions import Forbidden, TemplateSchemaActivation, SchemaNotFound
from .schema import activate_schema, deactivate_schema
from .signals import session_requesting_schema_change, session_schema_changed

      This is designed to be used from AJAX requests, or as part of
      an API call, as it returns a status code (and a short message)
      about the schema change request. If you were storing local data,
      and did one of these, you are probably going to have to invalidate
      much of that.

    You could also come up with other methods.

    """
    def __init__(self, get_response=None):
        # We should remove ourself if... when?
        self.get_response = get_response

    def __call__(self, request):
        try:
            return self.process_request(request) or self.get_response(request)
        except (TemplateSchemaActivation, ProgrammingError) as exception:
            return self.process_exception(request, exception)

    def process_request(self, request):
        FORBIDDEN = HttpResponseForbidden(_('You may not select that schema'))
        # Ways of changing the schema.
        # 1. URL /__change_schema__/<name>/
        # This will return a whole page.
        # We don't need to activate, that happens on the next request.
        if request.path.startswith('/__change_schema__/'):
            schema = request.path.split('/')[2]
            try:
                change_schema(request, schema)

# Path: boardinghouse/exceptions.py
# class Forbidden(Exception):
#     """
#     An exception that will be raised when an attempt to activate a non-valid
#     schema is made.
#     """
# 
# class TemplateSchemaActivation(Forbidden):
#     """
#     An exception that will be raised when a user attempts to activate
#     the settings.TEMPLATE_SCHEMA schema.
#     """
#     def __init__(self, *args, **kwargs):
#         super(TemplateSchemaActivation, self).__init__(
#             'Activating template schema forbidden.', *args, **kwargs
#         )
# 
# class SchemaNotFound(Exception):
#     """
#     An exception that is raised when an attempt to activate a schema that
#     is not in the database is made.
#     """
# 
# Path: boardinghouse/schema.py
# def activate_schema(schema_name):
#     """
#     Activate the current schema: this will execute, in the database
#     connection, something like:
# 
#     .. code:: sql
# 
#         SET search_path TO "foo",public;
# 
#     It sends signals before and after that the schema will be, and was
#     activated.
# 
#     Must be passed a string: the internal name of the schema to activate.
#     """
#     from .signals import schema_pre_activate, schema_post_activate
# 
#     if schema_name == settings.TEMPLATE_SCHEMA:
#         raise TemplateSchemaActivation()
# 
#     schema_pre_activate.send(sender=None, schema_name=schema_name)
#     _set_search_path(schema_name)
#     found_schema = _get_search_path()
#     if found_schema != schema_name:
#         raise SchemaNotFound('Schema activation failed. Expected "{0}", saw "{1}"'.format(
#             schema_name, found_schema,
#         ))
#     schema_post_activate.send(sender=None, schema_name=schema_name)
#     _thread_locals.schema = schema_name
# 
# def deactivate_schema(schema=None):
#     """
#     Deactivate the provided (or current) schema.
#     """
#     from .signals import schema_pre_activate, schema_post_activate
# 
#     cursor = connection.cursor()
#     schema_pre_activate.send(sender=None, schema_name=None)
#     cursor.execute('SET search_path TO "$user",{0}'.format(settings.PUBLIC_SCHEMA))
#     schema_post_activate.send(sender=None, schema_name=None)
#     _thread_locals.schema = None
#     cursor.close()
# 
# Path: boardinghouse/signals.py



# Path: boardinghouse/middleware.py
import logging
import re
from django.conf import settings
from django.contrib import messages
from django.db import ProgrammingError
from django.http import (
    HttpResponse, HttpResponseForbidden, HttpResponseRedirect,
)
from django.shortcuts import redirect
from django.utils.translation import ugettext_lazy as _
from .exceptions import Forbidden, TemplateSchemaActivation, SchemaNotFound
from .schema import activate_schema, deactivate_schema
from .signals import session_requesting_schema_change, session_schema_changed


      This is designed to be used from AJAX requests, or as part of
      an API call, as it returns a status code (and a short message)
      about the schema change request. If you were storing local data,
      and did one of these, you are probably going to have to invalidate
      much of that.

    You could also come up with other methods.

    """
    def __init__(self, get_response=None):
        # We should remove ourself if... when?
        self.get_response = get_response

    def __call__(self, request):
        try:
            return self.process_request(request) or self.get_response(request)
        except (TemplateSchemaActivation, ProgrammingError) as exception:
            return self.process_exception(request, exception)

    def process_request(self, request):
        FORBIDDEN = HttpResponseForbidden(_('You may not select that schema'))
        # Ways of changing the schema.
        # 1. URL /__change_schema__/<name>/
        # This will return a whole page.
        # We don't need to activate, that happens on the next request.
        if request.path.startswith('/__change_schema__/'):
            schema = request.path.split('/')[2]
            try:
                change_schema(request, schema)

            except Forbidden: