# Path: boardinghouse/models.py
# class Schema(AbstractSchema):
#     """
#     The default schema model.
# 
#     Unless you set `settings.BOARDINGHOUSE_SCHEMA_MODEL`, this model will
#     be used for storing the schema objects.
#     """
#     users = models.ManyToManyField(
#         settings.AUTH_USER_MODEL,
#         blank=True, related_name='schemata',
#         help_text=_(u'Which users may access data from this schema.')
#     )
# 
#     class Meta:
#         app_label = 'boardinghouse'
#         verbose_name_plural = 'schemata'
#         swappable = 'BOARDINGHOUSE_SCHEMA_MODEL'
# 
# Path: boardinghouse/schema.py
# def get_active_schema_name():
#     """
#     Get the currently active schema.
# 
#     This requires a database query to ask it what the current `search_path` is.
#     """
#     active_schema = getattr(_thread_locals, 'schema', None)
# 
#     if not active_schema:
#         reported_schema = _get_search_path()[0]
# 
#         if _get_schema(reported_schema):
#             active_schema = reported_schema
#         else:
#             active_schema = None
# 
#         _thread_locals.schema = active_schema
# 
#     return active_schema
# 
# def get_schema_model():
#     """
#     Return the class that is currently set as the schema model.
#     """
#     try:
#         return apps.get_model(settings.BOARDINGHOUSE_SCHEMA_MODEL)
#     except AttributeError:
#         raise ImproperlyConfigured("BOARDINGHOUSE_SCHEMA_MODEL is not set: is 'boardinghouse' in your INSTALLED_APPS?")
#     except ValueError:
#         raise ImproperlyConfigured("BOARDINGHOUSE_SCHEMA_MODEL must be of the form 'app_label.model_name'")
#     except LookupError:
#         raise ImproperlyConfigured(
#             "BOARDINGHOUSE_SCHEMA_MODEL refers to model '{0!s}' that has not been installed".format(
#                 settings.BOARDINGHOUSE_SCHEMA_MODEL))
# 
# def is_shared_model(model):
#     """
#     Is the model (or instance of a model) one that should be in the
#     public/shared schema?
#     """
#     if model._is_shared_model:
#         return True
# 
#     app_model = '{m.app_label}.{m.model_name}'.format(m=model._meta).lower()
# 
#     # These should be case insensitive!
#     if app_model in REQUIRED_SHARED_MODELS:
#         return True
# 
#     if app_model in [x.lower() for x in settings.SHARED_MODELS]:
#         return True
# 
#     # Sometimes, we want a join table to be private.
#     if app_model in [x.lower() for x in settings.PRIVATE_MODELS]:
#         return False
# 
#     # if all fields are auto or fk, then we are a join model,
#     # and if all related objects are shared, then we must
#     # also be shared, unless we were explicitly marked as private
#     # above.
#     if _is_join_model(model):
#         return all(
#             is_shared_model(field.remote_field.model)
#             for field in model._meta.fields
#             if field.remote_field
#         )
# 
#     return False
# 
# Path: boardinghouse/signals.py



from django.contrib import admin
from django.contrib.admin.models import LogEntry, LogEntryManager
from django.db import models
from django.db.models import expressions, Q
from django.dispatch import receiver
from .models import Schema
from .schema import get_active_schema_name, get_schema_model, is_shared_model
from .signals import find_schema


def get_inline_instances(self, request, obj=None):
    """
    Prevent the display of non-shared inline objects associated
    with private models if no schema is currently selected.

    If we don't patch this, then a ``DatabaseError`` will occur because
    the tables could not be found.
    """
    schema = get_active_schema_name()
    return [
        inline(self.model, self.admin_site) for inline in self.inlines
        if schema or is_shared_model(inline.model)
    ]


admin.ModelAdmin.get_inline_instances = get_inline_instances

if not getattr(LogEntry, 'object_schema', None):
    # We can't use a proper foreign key, as it plays havoc with migrations.
    LogEntry.add_to_class(
        'object_schema_id',
        models.TextField(blank=True, null=True)
    )

    @receiver(models.signals.pre_save, sender=LogEntry)
    def update_object_schema(sender, instance, **kwargs):
        obj = instance.get_edited_object()


# Path: boardinghouse/models.py
# class Schema(AbstractSchema):
#     """
#     The default schema model.
# 
#     Unless you set `settings.BOARDINGHOUSE_SCHEMA_MODEL`, this model will
#     be used for storing the schema objects.
#     """
#     users = models.ManyToManyField(
#         settings.AUTH_USER_MODEL,
#         blank=True, related_name='schemata',
#         help_text=_(u'Which users may access data from this schema.')
#     )
# 
#     class Meta:
#         app_label = 'boardinghouse'
#         verbose_name_plural = 'schemata'
#         swappable = 'BOARDINGHOUSE_SCHEMA_MODEL'
# 
# Path: boardinghouse/schema.py
# def get_active_schema_name():
#     """
#     Get the currently active schema.
# 
#     This requires a database query to ask it what the current `search_path` is.
#     """
#     active_schema = getattr(_thread_locals, 'schema', None)
# 
#     if not active_schema:
#         reported_schema = _get_search_path()[0]
# 
#         if _get_schema(reported_schema):
#             active_schema = reported_schema
#         else:
#             active_schema = None
# 
#         _thread_locals.schema = active_schema
# 
#     return active_schema
# 
# def get_schema_model():
#     """
#     Return the class that is currently set as the schema model.
#     """
#     try:
#         return apps.get_model(settings.BOARDINGHOUSE_SCHEMA_MODEL)
#     except AttributeError:
#         raise ImproperlyConfigured("BOARDINGHOUSE_SCHEMA_MODEL is not set: is 'boardinghouse' in your INSTALLED_APPS?")
#     except ValueError:
#         raise ImproperlyConfigured("BOARDINGHOUSE_SCHEMA_MODEL must be of the form 'app_label.model_name'")
#     except LookupError:
#         raise ImproperlyConfigured(
#             "BOARDINGHOUSE_SCHEMA_MODEL refers to model '{0!s}' that has not been installed".format(
#                 settings.BOARDINGHOUSE_SCHEMA_MODEL))
# 
# def is_shared_model(model):
#     """
#     Is the model (or instance of a model) one that should be in the
#     public/shared schema?
#     """
#     if model._is_shared_model:
#         return True
# 
#     app_model = '{m.app_label}.{m.model_name}'.format(m=model._meta).lower()
# 
#     # These should be case insensitive!
#     if app_model in REQUIRED_SHARED_MODELS:
#         return True
# 
#     if app_model in [x.lower() for x in settings.SHARED_MODELS]:
#         return True
# 
#     # Sometimes, we want a join table to be private.
#     if app_model in [x.lower() for x in settings.PRIVATE_MODELS]:
#         return False
# 
#     # if all fields are auto or fk, then we are a join model,
#     # and if all related objects are shared, then we must
#     # also be shared, unless we were explicitly marked as private
#     # above.
#     if _is_join_model(model):
#         return all(
#             is_shared_model(field.remote_field.model)
#             for field in model._meta.fields
#             if field.remote_field
#         )
# 
#     return False
# 
# Path: boardinghouse/signals.py



# Path: boardinghouse/admin.py
from django.contrib import admin
from django.contrib.admin.models import LogEntry, LogEntryManager
from django.db import models
from django.db.models import expressions, Q
from django.dispatch import receiver
from .models import Schema
from .schema import get_active_schema_name, get_schema_model, is_shared_model
from .signals import find_schema



def get_inline_instances(self, request, obj=None):
    """
    Prevent the display of non-shared inline objects associated
    with private models if no schema is currently selected.

    If we don't patch this, then a ``DatabaseError`` will occur because
    the tables could not be found.
    """
    schema = get_active_schema_name()
    return [
        inline(self.model, self.admin_site) for inline in self.inlines
        if schema or is_shared_model(inline.model)
    ]


admin.ModelAdmin.get_inline_instances = get_inline_instances

if not getattr(LogEntry, 'object_schema', None):
    # We can't use a proper foreign key, as it plays havoc with migrations.
    LogEntry.add_to_class(
        'object_schema_id',
        models.TextField(blank=True, null=True)
    )

    @receiver(models.signals.pre_save, sender=LogEntry)
    def update_object_schema(sender, instance, **kwargs):
        obj = instance.get_edited_object()


        if not is_shared_model(obj):