Révision 2ba6f6bf
Ajouté par Thomas Noël il y a environ 9 ans
entrouvert/djommon/multitenant/README | ||
---|---|---|
4 | 4 |
An application for making a Django application multitenant for Entr'ouvert |
5 | 5 |
customers. |
6 | 6 |
|
7 |
Install |
|
8 |
------- |
|
7 |
Based on https://django-tenant-schemas.readthedocs.org/ |
|
9 | 8 |
|
10 |
Set the tenant model: |
|
9 |
It is developed, tested and supported on Django 1.7, but it should work with |
|
10 |
Django 1.6 + south. |
|
11 | 11 |
|
12 |
TENANT_MODEL = 'multitenant.Tenant' |
|
13 | 12 |
|
14 |
Add the middlewares for multitenant, they must be first: |
|
13 |
Install |
|
14 |
------- |
|
15 | 15 |
|
16 |
MIDDLEWARE_CLASSES = ( |
|
17 |
'tenant_schemas.middleware.TenantMiddleware', |
|
18 |
'entrouvert.djommon.multitenant.middleware.EOTenantMiddleware', |
|
19 |
... |
|
20 |
) |
|
16 |
See also : https://django-tenant-schemas.readthedocs.org/ |
|
21 | 17 |
|
22 |
Add the multitenant filesystem template loader:
|
|
18 |
Set the tenant model:
|
|
23 | 19 |
|
24 |
TEMPLATE_LOADERS = ( |
|
25 |
'entrouvert.djommon.multitenant.template_loader.FilesystemLoader' |
|
26 |
.... |
|
27 |
) |
|
20 |
TENANT_MODEL = 'multitenant.Tenant' |
|
28 | 21 |
|
29 |
Configure where the multitenant templates are located:
|
|
22 |
Where are tenants:
|
|
30 | 23 |
|
24 |
TENANT_BASE = '/var/lib/<project>/tenants' |
|
31 | 25 |
|
32 |
MULTITENANT_TEMPLATE_DIRS = ('/var/lib/my_project/templates/',)
|
|
26 |
Add the middlewares for multitenant, they must be first:
|
|
33 | 27 |
|
34 |
Define the shared application: |
|
28 |
MIDDLEWARE_CLASSES = ( |
|
29 |
'entrouvert.djommon.multitenant.middleware.TenantMiddleware', |
|
30 |
'entrouvert.djommon.multitenant.middleware.JSONSettingsMiddleware', |
|
31 |
'entrouvert.djommon.multitenant.middleware.PythonSettingsMiddleware', |
|
32 |
) + MIDDLEWARE_CLASSES |
|
35 | 33 |
|
34 |
Define the shared applications: |
|
36 | 35 |
|
37 | 36 |
SHARED_APPS = ( |
38 | 37 |
'tenant_schemas', |
... | ... | |
44 | 43 |
'django.contrib.sessions', |
45 | 44 |
'django.contrib.messages', |
46 | 45 |
'django.contrib.staticfiles', |
47 |
'django.contrib.sites', |
|
48 | 46 |
) |
49 | 47 |
|
50 | 48 |
TENANT_APPS = INSTALLED_APPS |
51 | 49 |
|
52 |
INSTALLED_APPS += ('tenant_schemas', 'entrouvert.djommon.multitenant') |
|
50 |
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') |
|
53 | 55 |
|
54 | 56 |
Use multitenant database engine: |
55 | 57 |
|
... | ... | |
59 | 61 |
'NAME': '<db_name>', |
60 | 62 |
}, |
61 | 63 |
} |
64 |
DATABASE_ROUTERS = ( |
|
65 |
'tenant_schemas.routers.TenantSyncRouter', |
|
66 |
) |
|
62 | 67 |
|
63 |
Use multitenant south adapter: |
|
68 |
# With Django 1.6 or older, use multitenant south adapter: |
|
69 |
# SOUTH_DATABASE_ADAPTERS = {'default': 'south.db.postgresql_psycopg2'} |
|
64 | 70 |
|
65 |
SOUTH_DATABASE_ADAPTERS = { |
|
66 |
'default': 'south.db.postgresql_psycopg2', |
|
67 |
} |
|
71 |
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 |
manange.py create_tenant www.example.net |
|
89 |
Migration of all tenants: |
|
90 |
manange.py migrate_schemas |
|
91 |
|
|
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 |
Formats disponibles : Unified diff
multitenant: update README