From 7e104037c9522a408138c79c1fa8be3f98fc5894 Mon Sep 17 00:00:00 2001 From: viatrak Date: Thu, 7 Sep 2017 12:43:40 -0500 Subject: [PATCH] Content type cache should be cleared not just at the beginning of a request, but *anytime* the schema is changed (which happens in middle of requests). set_tenant() will also now result in a set_schema() call so it will claer_cache() at the beginning of a request, and on subsequent set_schema calls --- tenant_schemas/middleware.py | 9 --------- tenant_schemas/postgresql_backend/base.py | 9 +++++++++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/tenant_schemas/middleware.py b/tenant_schemas/middleware.py index 639e686..c70c053 100644 --- a/tenant_schemas/middleware.py +++ b/tenant_schemas/middleware.py @@ -62,15 +62,6 @@ class BaseTenantMiddleware(MIDDLEWARE_MIXIN): request.tenant = tenant connection.set_tenant(request.tenant) - # Content type can no longer be cached as public and tenant schemas - # have different models. If someone wants to change this, the cache - # needs to be separated between public and shared schemas. If this - # cache isn't cleared, this can cause permission problems. For example, - # on public, a particular model has id 14, but on the tenants it has - # the id 15. if 14 is cached instead of 15, the permissions for the - # wrong model will be fetched. - ContentType.objects.clear_cache() - # Do we have a public-specific urlconf? if hasattr(settings, 'PUBLIC_SCHEMA_URLCONF') and request.tenant.schema_name == get_public_schema_name(): request.urlconf = settings.PUBLIC_SCHEMA_URLCONF diff --git a/tenant_schemas/postgresql_backend/base.py b/tenant_schemas/postgresql_backend/base.py index ad61f5b..212d3ea 100644 --- a/tenant_schemas/postgresql_backend/base.py +++ b/tenant_schemas/postgresql_backend/base.py @@ -3,6 +3,7 @@ import warnings import psycopg2 from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.core.exceptions import ImproperlyConfigured, ValidationError import django.db.utils @@ -80,6 +81,14 @@ class DatabaseWrapper(original_backend.DatabaseWrapper): self.include_public_schema = include_public self.set_settings_schema(schema_name) self.search_path_set = False + # Content type can no longer be cached as public and tenant schemas + # have different models. If someone wants to change this, the cache + # needs to be separated between public and shared schemas. If this + # cache isn't cleared, this can cause permission problems. For example, + # on public, a particular model has id 14, but on the tenants it has + # the id 15. if 14 is cached instead of 15, the permissions for the + # wrong model will be fetched. + ContentType.objects.clear_cache() def set_schema_to_public(self): """