1 |
|
import re
|
2 |
|
import json
|
3 |
|
|
4 |
|
from django.utils.translation import ugettext as _
|
5 |
|
from django.db import models
|
6 |
|
from django.core.exceptions import ImproperlyConfigured, ValidationError
|
7 |
|
|
8 |
1 |
from tenant_schemas.models import TenantMixin
|
9 |
2 |
|
10 |
|
from . import app_settings
|
11 |
|
|
12 |
3 |
class Tenant(TenantMixin):
|
13 |
|
name = models.CharField(max_length=100, verbose_name=_('name'))
|
14 |
|
created_on = models.DateField(auto_now_add=True, verbose_name=_('created on'))
|
15 |
|
is_active = models.BooleanField(default=True, blank=True, verbose_name=_('active'))
|
16 |
|
|
17 |
4 |
# default true, schema will be automatically created and synced when it is saved
|
18 |
5 |
auto_create_schema = False
|
19 |
6 |
|
|
7 |
def save(self):
|
|
8 |
pass
|
|
9 |
|
20 |
10 |
def __unicode__(self):
|
21 |
11 |
return u'%s' % self.schema_name
|
22 |
|
|
23 |
|
|
24 |
|
class ClientSetting(models.Model):
|
25 |
|
NAME_RE = re.compile(r'^[_A-Z][_A-Z0-9]*$')
|
26 |
|
|
27 |
|
tenant = models.ForeignKey('Tenant', verbose_name=_('tenant'))
|
28 |
|
name = models.CharField(max_length=100, verbose_name=_('name'))
|
29 |
|
value = models.TextField(verbose_name=_('value'), help_text=_('JSON'))
|
30 |
|
|
31 |
|
def get_json(self):
|
32 |
|
return json.loads(self.value)
|
33 |
|
|
34 |
|
def set_json(self, json):
|
35 |
|
self.value = json.dumps(json)
|
36 |
|
|
37 |
|
json = property(get_json, set_json)
|
38 |
|
|
39 |
|
def clean(self):
|
40 |
|
if not re.match(self.NAME_RE, self.name):
|
41 |
|
raise ValidationError('name must be an uppercase variable '
|
42 |
|
'name')
|
43 |
|
try:
|
44 |
|
json.loads(self.value)
|
45 |
|
except ValueError:
|
46 |
|
raise ValidationError('invalid JSON document')
|
47 |
|
if self.name in dir(app_settings):
|
48 |
|
raise ImproperlyConfigured('The setting %r cannot be overridden by tenants' % self.name)
|
49 |
|
|
50 |
|
-
|