Revision 4cea64a7
Added by Benjamin Dauvergne over 10 years ago
entrouvert/djommon/multitenant/management/commands/create-tenant.py | ||
---|---|---|
1 |
import re |
|
2 |
import json |
|
3 |
import sys |
|
4 |
|
|
5 |
from optparse import make_option |
|
6 |
|
|
7 |
from django.core.management.base import BaseCommand, CommandError |
|
8 |
from django.template.defaultfilters import slugify |
|
9 |
from django.db import transaction |
|
10 |
|
|
11 |
|
|
12 |
from ...models import ClientSetting |
|
13 |
|
|
14 |
|
|
15 |
class Command(BaseCommand): |
|
16 |
requires_model_validation = True |
|
17 |
can_import_settings = True |
|
18 |
option_list = BaseCommand.option_list + ( |
|
19 |
make_option('--list-settings', |
|
20 |
action='store_true', |
|
21 |
default=False, |
|
22 |
help='List settings'), |
|
23 |
make_option('--verbose', |
|
24 |
action='store_true', |
|
25 |
default=False, |
|
26 |
help='List settings'), |
|
27 |
make_option('--set-setting', |
|
28 |
action='append', |
|
29 |
default=[], |
|
30 |
help='Set setting, use key=value format'), |
|
31 |
) |
|
32 |
|
|
33 |
@transaction.commit_on_success |
|
34 |
def handle(self, domain_url, schema_name=None, **options): |
|
35 |
import tenant_schemas.utils |
|
36 |
|
|
37 |
tenant_model = tenant_schemas.utils.get_tenant_model() |
|
38 |
if schema_name is None: |
|
39 |
schema_name = slugify(domain_url) |
|
40 |
tenant, created = tenant_model.objects.get_or_create(domain_url=domain_url, |
|
41 |
defaults={'schema_name': schema_name}) |
|
42 |
if created: |
|
43 |
print 'Created new tenant for', domain_url, 'and schema name', schema_name |
|
44 |
tenant.create_schema(True) |
|
45 |
if options['list_settings']: |
|
46 |
if tenant.clientsetting_set.exists(): |
|
47 |
for client_setting in tenant.clientsetting_set.all(): |
|
48 |
print '{0.name}: {0.value}'.format(client_setting) |
|
49 |
for key_value in options['set_setting']: |
|
50 |
if '=' not in key_value: |
|
51 |
raise CommandError('invalid --set-settings %r', key_value) |
|
52 |
key, value = key_value.split('=', 1) |
|
53 |
if not re.match(ClientSetting.NAME_RE, key): |
|
54 |
raise CommandError('invalid --set-settings key %r', key) |
|
55 |
if value: |
|
56 |
try: |
|
57 |
json.loads(value) |
|
58 |
except ValueError: |
|
59 |
raise CommandError('invalid --set-settings value JSON %r', value) |
|
60 |
try: |
|
61 |
client_setting = tenant.clientsetting_set.get(name=key) |
|
62 |
client_setting.value = value |
|
63 |
client_setting.save() |
|
64 |
if options['verbose']: |
|
65 |
print >>sys.stderr, 'Modified setting' |
|
66 |
except ClientSetting.DoesNotExist: |
|
67 |
tenant.clientsetting_set.create(name=key, value=value) |
|
68 |
if options['verbose']: |
|
69 |
print >>sys.stderr, 'Created setting' |
|
70 |
else: |
|
71 |
qs = tenant.clientsetting_set.filter(name=key) |
|
72 |
count = qs.count() |
|
73 |
qs.delete() |
|
74 |
if options['verbose']: |
|
75 |
if count: |
|
76 |
print >>sys.stderr, 'Deleted settings' |
|
77 |
else: |
|
78 |
print >>sys.stderr, 'No setting found' |
Also available in: Unified diff
Remove create-tenant command
refs #5106