# 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
            data = request.GET.copy()
            data.pop('__schema')

            if request.method == "GET":
                # redirect so we strip the schema out of the querystring.
                if data:
                    return redirect(request.path + '?' + data.urlencode())
                return redirect(request.path)

            # method == 'POST' or other
            request.GET = data

        # 3. Header "X-Change-Schema: <name>"
        elif 'HTTP_X_CHANGE_SCHEMA' in request.META:
            schema = request.META['HTTP_X_CHANGE_SCHEMA']
            try:
                change_schema(request, schema)
            except Forbidden:
                return FORBIDDEN

        elif 'schema' not in request.session and len(request.user.visible_schemata) == 1:
            change_schema(request, request.user.visible_schemata[0].schema)

        if 'schema' in request.session:
            try:
                activate_schema(request.session['schema'])
            except SchemaNotFound:
                deactivate_schema()
                request.session.pop('schema')
        else:

# 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

            data = request.GET.copy()
            data.pop('__schema')

            if request.method == "GET":
                # redirect so we strip the schema out of the querystring.
                if data:
                    return redirect(request.path + '?' + data.urlencode())
                return redirect(request.path)

            # method == 'POST' or other
            request.GET = data

        # 3. Header "X-Change-Schema: <name>"
        elif 'HTTP_X_CHANGE_SCHEMA' in request.META:
            schema = request.META['HTTP_X_CHANGE_SCHEMA']
            try:
                change_schema(request, schema)
            except Forbidden:
                return FORBIDDEN

        elif 'schema' not in request.session and len(request.user.visible_schemata) == 1:
            change_schema(request, request.user.visible_schemata[0].schema)

        if 'schema' in request.session:
            try:
                activate_schema(request.session['schema'])
            except SchemaNotFound:
                deactivate_schema()
                request.session.pop('schema')
        else:

            deactivate_schema()