コード例 #1
0
def update_cache_from_env(cache_dict):
    from django.db import connection

    cache_prefix = os.environ.get("TENANT_CACHE_PREFIX")
    if cache_prefix is not None:
        cache_dict["KEY_PREFIX"] = cache_prefix
        connection.get_threadlocal().set_cache_prefix(cache_prefix)
コード例 #2
0
 def process_response(self, request, response):
     """Clears the database name and cache prefix on response.
     This is a precaution against the connection being reused without
     first calling set_dbname or set_tenant_name.
     """
     connection.get_threadlocal().reset()
     return response
コード例 #3
0
def update_database_from_env(db_dict):
    from django.db import connection

    dbname = os.environ.get("TENANT_DATABASE_NAME")
    if dbname:
        db_dict["NAME"] = dbname
        connection.get_threadlocal().set_dbname(dbname)
コード例 #4
0
    def process_response(self, request, response):
        """Clears the database name and cache prefix on response.

        This is a precaution against the connection being reused without
        first calling set_dbname.
        """
        connection.get_threadlocal().reset()
        return response
コード例 #5
0
    def process_request(self, request):
        mapper = utils.get_mapper()
        threadlocal = connection.get_threadlocal()
        threadlocal.set_tenant_name(mapper.get_tenant_name(request))
        threadlocal.set_dbname(mapper.get_dbname(request))
        threadlocal.set_cache_prefix(mapper.get_cache_prefix(request))

        if 'django.contrib.sites' in settings.INSTALLED_APPS:
            # Clear the sites framework cache.
            from django.contrib.sites.models import Site
            Site.objects.clear_cache()
コード例 #6
0
    def process_request(self, request):
        mapper = utils.get_mapper()

        threadlocal = connection.get_threadlocal()
        threadlocal.set_tenant_name(mapper.get_tenant_name(request))
        threadlocal.set_dbname(mapper.get_dbname(request))
        threadlocal.set_cache_prefix(mapper.get_cache_prefix(request))

        if 'django.contrib.sites' in settings.INSTALLED_APPS:
            # Clear the sites framework cache.
            from django.contrib.sites.models import Site
            Site.objects.clear_cache()
コード例 #7
0
    def process_request(self, request):
        mapper = utils.get_mapper()

        threadlocal = connection.get_threadlocal()
        tenant_name = mapper.get_tenant_name(request)
        threadlocal.set_tenant_name(tenant_name)
        db_name = mapper.get_db_name(request, tenant_name)
        # Fixes ValueError when trying to inject SQL, setting empty string as default
        try:
            threadlocal.set_db_name(db_name)
        except ValueError as ve:
            threadlocal.set_db_name('')
        threadlocal.set_cache_prefix(mapper.get_cache_prefix(request, tenant_name, db_name))

        if 'django.contrib.sites' in settings.INSTALLED_APPS:
            # Clear the sites framework cache.
            from django.contrib.sites.models import Site
            Site.objects.clear_cache()
コード例 #8
0
def update_tenant_name_from_env():
    from django.db import connection
    tenant_name = os.environ.get('TENANT_NAME')
    if tenant_name:
        connection.get_threadlocal().set_tenant_name(tenant_name)
コード例 #9
0
def update_cache_from_env(cache_settings):
    from django.db import connection
    cache_prefix = os.environ.get('TENANT_CACHE_PREFIX')
    if cache_prefix is not None and cache_settings is not None:
        connection.get_threadlocal().set_cache_prefix(cache_prefix)
コード例 #10
0
def update_database_from_env(database_settings):
    from django.db import connection
    db_name = os.environ.get('TENANT_DATABASE_NAME')
    if db_name and database_settings is not None:
        database_settings['NAME'] = db_name
        connection.get_threadlocal().set_db_name(db_name)
コード例 #11
0
ファイル: helper.py プロジェクト: mik3y/django-db-multitenant
def multitenant_key_func(key, key_prefix, version):
    tenant_prefix = connection.get_threadlocal().get_cache_prefix()
    if tenant_prefix is None:
        raise ImproperlyConfigured('Multi-tenant cache prefix not available')
    return '%s:%s:%s:%s' % (tenant_prefix, key_prefix, version, key)
コード例 #12
0
def update_database_from_env(db_dict):
    from django.db import connection
    dbname = os.environ.get('TENANT_DATABASE_NAME')
    if dbname:
        db_dict['NAME'] = dbname
        connection.get_threadlocal().set_dbname(dbname)
コード例 #13
0
def multitenant_key_func(key, key_prefix, version):
    tenant_prefix = connection.get_threadlocal().get_cache_prefix()
    if tenant_prefix is None:
        raise ImproperlyConfigured('Multi-tenant cache prefix not available')
    return '%s:%s:%s:%s' % (tenant_prefix, key_prefix, version, key)
コード例 #14
0
ファイル: utils.py プロジェクト: mik3y/django-db-multitenant
def update_tenant_name_from_env():
    from django.db import connection
    tenant_name = os.environ.get('TENANT_NAME')
    if tenant_name:
        connection.get_threadlocal().set_tenant_name(tenant_name)
コード例 #15
0
ファイル: utils.py プロジェクト: mik3y/django-db-multitenant
def update_cache_from_env(cache_settings):
    from django.db import connection
    cache_prefix = os.environ.get('TENANT_CACHE_PREFIX')
    if cache_prefix is not None and cache_settings is not None:
        cache_settings['KEY_PREFIX'] = cache_prefix
        connection.get_threadlocal().set_cache_prefix(cache_prefix)
コード例 #16
0
ファイル: utils.py プロジェクト: mik3y/django-db-multitenant
def update_database_from_env(database_settings):
    from django.db import connection
    db_name = os.environ.get('TENANT_DATABASE_NAME')
    if db_name and database_settings is not None:
        database_settings['NAME'] = db_name
        connection.get_threadlocal().set_db_name(db_name)
コード例 #17
0
def update_cache_from_env(cache_dict):
    from django.db import connection
    cache_prefix = os.environ.get('TENANT_CACHE_PREFIX')
    if cache_prefix is not None:
        cache_dict['KEY_PREFIX'] = cache_prefix
        connection.get_threadlocal().set_cache_prefix(cache_prefix)
コード例 #18
0
def get_threadlocal():
    if get_mode() == "SCHEMA":
        return connection.get_threadlocal()
    else: # mode == "DATABASE":
        return my_local_global