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 _clean_tenant_specific_fields(self): for name, field in self.fields.items(): if name in tenant_specific_fields_names: definition = tenant_specific_fields_definitions.get(name=name) value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) try: value = field.clean(value) validators = [] for validator_instance in definition.validators.all(): validator_function = import_from_string(validator_instance.module_path) validators.append(validator_function) validate_method = compose_list(validators) self.cleaned_data[name] = validate_method(value) if hasattr(self, 'clean_%s' % name): value = getattr(self, 'clean_%s' % name)() self.cleaned_data[name] = value except ValidationError as e: self.add_error(name, e)
def to_internal_value(self, data): ret = OrderedDict() errors = OrderedDict() for field in tenant_specific_fields_definitions: validators = [] for validator_instance in field.validators.all(): validator_function = import_from_string( validator_instance.module_path) validators.append(validator_function) validate_method = compose_list(validators) primitive_value = self.fields.get(field.name).get_value(data) try: validated_value = self.fields.get( field.name).run_validation(primitive_value) if validate_method is not None: validated_value = validate_method(validated_value) except serializers.ValidationError as exc: errors[field.name] = exc.detail except DjangoValidationError as exc: errors[field.name] = get_error_detail(exc) except SkipField: pass else: set_value(ret, self.fields.get(field.name).source_attrs, validated_value) if errors: raise serializers.ValidationError(errors) data.update(dict(ret)) ret = super(TenantSpecificTableRowSerializer, self).to_internal_value(data) return ret
def get_permissions(self): return [ import_from_string(permission)() for permission in get_setting( 'CUSTOMIZABLE_MODELS_LIST_CREATE_PERMISSIONS') ]
def get_permissions(self): return [ import_from_string(permission)() for permission in get_setting( 'CUSTOMIZABLE_MODELS_RETRIEVE_UTPADE_DESTROY_PERMISSIONS') ]
def get_serializer_class(self): return import_from_string(get_setting('TENANT_SITE_SERIALIZER'))
def test_class_is_really_imported(self): TenantSettingsHelper = import_from_string( 'shared_schema_tenants.helpers.tenant_settings.TenantSettingsHelper' ) self.assertEqual(TenantSettingsHelper.__name__, 'TenantSettingsHelper')