Project

General

Profile

Download (2.66 KB) Statistics
| Branch: | Tag: | Revision:

root / entrouvert / djommon / multitenant / README @ dc463f1d

1 e62d56fc Benjamin Dauvergne
Multitenant
2
-----------
3
4
An application for making a Django application multitenant for Entr'ouvert
5 c59fb59b Benjamin Dauvergne
customers.
6 e62d56fc Benjamin Dauvergne
7 2ba6f6bf Thomas NOEL
Based on https://django-tenant-schemas.readthedocs.org/
8 e62d56fc Benjamin Dauvergne
9 2ba6f6bf Thomas NOEL
It is developed, tested and supported on Django 1.7, but it should work with
10
Django 1.6 + south.
11 e62d56fc Benjamin Dauvergne
12
13 2ba6f6bf Thomas NOEL
Install
14
-------
15 e62d56fc Benjamin Dauvergne
16 2ba6f6bf Thomas NOEL
See also : https://django-tenant-schemas.readthedocs.org/
17 e62d56fc Benjamin Dauvergne
18 2ba6f6bf Thomas NOEL
Set the tenant model:
19 e62d56fc Benjamin Dauvergne
20 2ba6f6bf Thomas NOEL
    TENANT_MODEL = 'multitenant.Tenant'
21 e62d56fc Benjamin Dauvergne
22 2ba6f6bf Thomas NOEL
Where are tenants:
23 e62d56fc Benjamin Dauvergne
24 2ba6f6bf Thomas NOEL
    TENANT_BASE = '/var/lib/<project>/tenants'
25 e62d56fc Benjamin Dauvergne
26 2ba6f6bf Thomas NOEL
Add the middlewares for multitenant, they must be first:
27 e62d56fc Benjamin Dauvergne
28 2ba6f6bf Thomas NOEL
    MIDDLEWARE_CLASSES = (
29
        'entrouvert.djommon.multitenant.middleware.TenantMiddleware',
30
        'entrouvert.djommon.multitenant.middleware.JSONSettingsMiddleware',
31
        'entrouvert.djommon.multitenant.middleware.PythonSettingsMiddleware',
32
    ) + MIDDLEWARE_CLASSES
33 e62d56fc Benjamin Dauvergne
34 2ba6f6bf Thomas NOEL
Define the shared applications:
35 e62d56fc Benjamin Dauvergne
36
    SHARED_APPS = (
37
        'tenant_schemas',
38
        'entrouvert.djommon.multitenant',
39 c59fb59b Benjamin Dauvergne
        # those are needed for the public apps to work
40
        # add also any application needed by the public app
41
        'django.contrib.auth',
42
        'django.contrib.contenttypes',
43
        'django.contrib.sessions',
44
        'django.contrib.messages',
45
        'django.contrib.staticfiles',
46 e62d56fc Benjamin Dauvergne
    )
47
48 c59fb59b Benjamin Dauvergne
    TENANT_APPS = INSTALLED_APPS
49
50 2ba6f6bf Thomas NOEL
    INSTALLED_APPS = ('entrouvert.djommon.multitenant',
51
        'tenant_schemas') + INSTALLED_APPS
52
53
    # or, with Django 1.6 or older:
54
    # INSTALLED_APPS += ('tenant_schemas', 'entrouvert.djommon.multitenant')
55 c59fb59b Benjamin Dauvergne
56 e62d56fc Benjamin Dauvergne
Use multitenant database engine:
57
58
    DATABASES = {
59
        'default': {
60
            'ENGINE': 'tenant_schemas.postgresql_backend',
61
            'NAME': '<db_name>',
62
        },
63
    }
64 2ba6f6bf Thomas NOEL
    DATABASE_ROUTERS = (
65
        'tenant_schemas.routers.TenantSyncRouter',
66
    )
67 e62d56fc Benjamin Dauvergne
68 2ba6f6bf Thomas NOEL
    # With Django 1.6 or older, use multitenant south adapter:
69
    # SOUTH_DATABASE_ADAPTERS = {'default': 'south.db.postgresql_psycopg2'}
70 e62d56fc Benjamin Dauvergne
71 2ba6f6bf Thomas NOEL
Add the multitenant filesystem template loader and configure where the
72
multitenant templates are located:
73
74
    TEMPLATE_LOADERS = (
75
        'entrouvert.djommon.multitenant.template_loader.FilesystemLoader',
76
    ) + TEMPLATE_LOADERS
77
    TENANT_TEMPLATE_DIRS = (TENANT_BASE,)
78
79
    TEMPLATE_CONTEXT_PROCESSORS = (
80
        'django.core.context_processors.request',
81
    ) + TEMPLATE_CONTEXT_PROCESSORS
82
83
84
Usage
85
-----
86
87
Create a tenant:
88 dc463f1d Frédéric Péters
   manage.py create_tenant www.example.net
89 2ba6f6bf Thomas NOEL
Migration of all tenants:
90 dc463f1d Frédéric Péters
   manage.py migrate_schemas
91 2ba6f6bf Thomas NOEL
92
93
Tenants are created in TENANT_BASE directory, for example :
94
    /var/lib/project/tenants/www.example.net/
95
               templates/  <-- override project templates
96
               static/     <-- to be handled by HTTP server
97
               media/
98
Each tenant is a PostgreSQL schema, named www_example_net