Skip to content

Commit

Permalink
Merge branch 'master' of github.com:bernardopires/django-tenant-schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardopires committed Jul 30, 2015
2 parents ce73fa9 + 68323c5 commit 4654fc3
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 2 deletions.
19 changes: 19 additions & 0 deletions docs/install.rst
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,24 @@ If you put this in the same Django project, you can make a new ``settings_public

Or you can create a completely separate project for the main website.

Caching
-------

To enable tenant aware caching you can set the `KEY_FUNCTION <https://docs.djangoproject.com/en/1.8/ref/settings/#std:setting-CACHES-KEY_FUNCTION>`_ setting to use the provided ``make_key`` helper function which
adds the tenants ``schema_name`` as the first key prefix.

.. code-block:: python
CACHES = {
"default": {
...
'KEY_FUNCTION': 'tenant_schemas.cache.make_key',
'REVERSE_KEY_FUNCTION': 'tenant_schemas.cache.reverse_key',
},
}
The ``REVERSE_KEY_FUNCTION`` setting is only required if you are using the `django-redis <https://github.com/niwinz/django-redis>`_ cache backend.

Configuring your Apache Server (optional)
=========================================
Here's how you can configure your Apache server to route all subdomains to your django project so you don't have to setup any subdomains manually.
Expand All @@ -238,3 +256,4 @@ formats using `Sphinx <http://pypi.python.org/pypi/Sphinx>`_. To get started
make html
This creates the documentation in HTML format at ``docs/_build/html``.

27 changes: 26 additions & 1 deletion docs/use.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,32 @@ Any call to the methods ``filter``, ``get``, ``save``, ``delete`` or any other f

Management commands
-------------------
Every command except tenant_command runs by default on all tenants. You can also create your own commands that run on every tenant by inheriting ``BaseTenantCommand``. To run only a particular schema, there is an optional argument called ``--schema``.
Every command except tenant_command runs by default on all tenants. You can also create your own commands that run on every tenant by inheriting ``BaseTenantCommand``.

For example, if you have the following ```do_foo``` command in the ```foo``` app:

```foo/management/commands/do_foo.py```

.. code-block:: python
from django.core.management.base import BaseCommand
class Command(BaseCommand):
def handle(self, *args, **options):
do_foo()
You could create a wrapper command ```tenant_do_foo``` by using ```BaseTenantCommand``` like so:

```foo/management/commands/tenant_do_foo.py```

.. code-block:: python
from tenant_schemas.management.commands import BaseTenantCommand
class Command(BaseTenantCommand):
COMMAND_NAME = 'do_foo'
To run only a particular schema, there is an optional argument called ``--schema``.

.. code-block:: bash
Expand Down
20 changes: 20 additions & 0 deletions tenant_schemas/cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from django.db import connection


def make_key(key, key_prefix, version):
"""
Tenant aware function to generate a cache key.
Constructs the key used by all other methods. Prepends the tenant
`schema_name` and `key_prefix'.
"""
return '%s:%s:%s:%s' % (connection.schema_name, key_prefix, version, key)


def reverse_key(key):
"""
Tenant aware function to reverse a cache key.
Required for django-redis REVERSE_KEY_FUNCTION setting.
"""
return key.split(':', 3)[3]
3 changes: 2 additions & 1 deletion tenant_schemas/template_loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from django.utils.encoding import force_bytes
from django.utils._os import safe_join
from django.db import connection
from tenant_schemas.postgresql_backend.base import FakeTenant


class CachedLoader(BaseLoader):
Expand Down Expand Up @@ -85,7 +86,7 @@ def get_template_sources(self, template_name, template_dirs=None):
directory in "template_dirs". Any paths that don't lie inside one of the
template dirs are excluded from the result set, for security reasons.
"""
if not connection.tenant:
if not connection.tenant or isinstance(connection.tenant, FakeTenant):
return
if not template_dirs:
try:
Expand Down
1 change: 1 addition & 0 deletions tenant_schemas/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from test_routes import *
from test_tenants import *
from test_cache import *
13 changes: 13 additions & 0 deletions tenant_schemas/tests/test_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from tenant_schemas.cache import make_key, reverse_key
from tenant_schemas.test.cases import TenantTestCase


class CacheHelperTestCase(TenantTestCase):
def test_make_key(self):
key = make_key(key='foo', key_prefix='', version=1)
tenant_prefix = key.split(':')[0]
self.assertEqual(self.tenant.schema_name, tenant_prefix)

def test_reverse_key(self):
key = 'foo'
self.assertEqual(key, reverse_key(make_key(key=key, key_prefix='', version=1)))

0 comments on commit 4654fc3

Please sign in to comment.