def get_tenant(request): if not hasattr(request, '_cached_tenant'): tenant_retrievers = get_setting('TENANT_RETRIEVERS') for tenant_retriever in tenant_retrievers: tenant = import_from_string(tenant_retriever)(request) if tenant: request._cached_tenant = tenant break if not getattr(request, '_cached_tenant', False): lazy_tenant = TenantMiddleware.get_current_tenant() if not lazy_tenant: return None lazy_tenant._setup() request._cached_tenant = lazy_tenant._wrapped elif get_setting('ADD_TENANT_TO_SESSION'): try: request.session['tenant_slug'] = request._cached_tenant.slug except AttributeError: pass return request._cached_tenant
def get_tenant(request): if not hasattr(request, '_cached_tenant'): try: request._cached_tenant = get_current_site( request).tenant_site.tenant return request._cached_tenant except (TenantSite.DoesNotExist, Site.DoesNotExist): pass try: tenant_http_header = 'HTTP_' + get_setting( 'TENANT_HTTP_HEADER').replace('-', '_').upper() request._cached_tenant = Tenant.objects.get( slug=request.META[tenant_http_header]) except LookupError: lazy_tenant = TenantMiddleware.get_current_tenant() if not lazy_tenant: return None lazy_tenant._setup() request._cached_tenant = lazy_tenant._wrapped except Tenant.DoesNotExist: return None return request._cached_tenant
class Tenant(TimeStampedModel): name = models.CharField(max_length=255) slug = models.CharField(max_length=255, primary_key=True) if 'postgresql' in django_settings.DATABASES['default']['ENGINE']: from django.contrib.postgres.fields import JSONField extra_data = JSONField( blank=True, null=True, default=get_setting('DEFAULT_TENANT_EXTRA_DATA')) settings = JSONField(blank=True, null=True, default=get_setting('DEFAULT_TENANT_SETTINGS')) else: _extra_data = models.TextField( blank=True, null=True, validators=[validate_json], default=json.dumps(get_setting('DEFAULT_TENANT_EXTRA_DATA'))) _settings = models.TextField( blank=True, null=True, validators=[validate_json], default=json.dumps(get_setting('DEFAULT_TENANT_SETTINGS'))) @property def extra_data(self): import json return json.loads(self._extra_data) @extra_data.setter def extra_data(self, value): import json self._extra_data = json.dumps(value) @property def settings(self): import json return json.loads(self._settings) @settings.setter def settings(self, value): import json self._settings = json.dumps(value) def __str__(self): return self.name
def retrieve_by_http_header(request): try: tenant_http_header = 'HTTP_' + get_setting( 'TENANT_HTTP_HEADER').replace('-', '_').upper() return Tenant.objects.get(slug=request.META[tenant_http_header]) except LookupError: return None except Tenant.DoesNotExist: raise TenantNotFoundError()
def create_default_tenant_groups(): with transaction.atomic(): group, created = Group.objects.get_or_create(name='tenant_owner') if created: for perm in get_setting('DEFAULT_TENANT_OWNER_PERMISSIONS'): try: group.permissions.add(Permission.objects.get( content_type__app_label=perm.split('.')[0], codename=perm.split('.')[1])) except Permission.DoesNotExist: pass return [group]
def get_serializer_class(self): return import_from_string(get_setting('TENANT_SITE_SERIALIZER'))
def __init__(self, instance=None): super(TenantExtraDataHelper, self).__init__( instance_field_name='extra_data', instance=instance, tenant_fields=get_setting('DEFAULT_TENANT_EXTRA_DATA_FIELDS'), tenant_default_fields_values=get_setting('DEFAULT_TENANT_EXTRA_DATA'))
def get_default_tenant(): from shared_schema_tenants.models import Tenant return Tenant.objects.filter(slug=get_setting('DEFAULT_TENANT_SLUG')).first()
def __init__(self, instance=None): super(TenantSettingsHelper, self).__init__( instance_field_name='settings', instance=instance, tenant_fields=get_setting('DEFAULT_TENANT_SETTINGS_FIELDS'), tenant_default_fields_values=get_setting('DEFAULT_TENANT_SETTINGS'))
def get_default_tenant(): return Tenant.objects.filter( slug=get_setting('DEFAULT_TENANT_SLUG')).first()
def get_serializer_class(self): return import_class(get_setting('TENANT_SETTINGS_SERIALIZER'))