def get_all_tenant_specific_table_permissions(self, user_obj, obj=None): if not user_obj.is_active or user_obj.is_anonymous( ) or obj is not None: return set() tenant = get_current_tenant() if not tenant: return set() self.has_module_perms if (not hasattr(user_obj, '_tenant_specific_tables_perm_cache') or not getattr(user_obj, '_tenant_specific_tables_perm_cache').get( tenant.pk)): user_obj._tenant_specific_tables_perm_cache = { tenant.pk: self.get_user_tenant_specific_tables_permissions( user_obj, obj) } user_obj._tenant_specific_tables_perm_cache[tenant.pk] = ( user_obj._tenant_specific_tables_perm_cache[tenant.pk].union( self.get_group_tenant_specific_tables_permissions( user_obj, obj))) return user_obj._tenant_specific_tables_perm_cache[tenant.pk]
def create(self, validated_data): tenant = get_current_tenant() domain = validated_data['domain'] with transaction.atomic(): site = Site.objects.create(name=domain, domain=domain) return TenantSite.objects.create(tenant=tenant, site=site)
def _get_tenant_specific_tables_permissions(self, user_obj, obj, from_name): if not user_obj.is_active or user_obj.is_anonymous( ) or obj is not None: return set() tenant = get_current_tenant() if not tenant: return set() perm_cache_name = '_tenant_specific_tables_%s_perm_cache' % from_name if (not hasattr(user_obj, perm_cache_name) or not getattr(user_obj, perm_cache_name).get(tenant.pk)): if user_obj.is_superuser: relationship_perms = TenantSpecificTablesPermission.objects.all( ) relationship_perms = relationship_perms.values_list( 'codename', flat=True).order_by() else: try: relationship = TenantSpecificTablesRelationship.objects.get( user=user_obj, tenant=tenant) except TenantSpecificTablesRelationship.DoesNotExist: relationship_perms = set() else: relationship_perms = getattr( self, '_get_%s_tenant_specific_tables_permissions' % from_name)(relationship) relationship_perms = relationship_perms.values_list( 'codename', flat=True).order_by() setattr(user_obj, perm_cache_name, {tenant.pk: {name for name in relationship_perms}}) return getattr(user_obj, perm_cache_name).get(tenant.pk)
def save(self, *args, **kwargs): table_id = getattr( self, 'table_id', getattr(getattr(self, 'table', object()), 'id', None)) force_hit_db = False if hasattr(self, 'definitions') and not self.definitions.exists(): force_hit_db = True definitions = self.get_definitions(table_id=table_id, force_hit_db=force_hit_db) tenant_specific_fields_data = {} for definition in definitions: if hasattr(self, definition.name): tenant_specific_fields_data[definition.name] = getattr( self, definition.name) delattr(self, definition.name) else: tenant_specific_fields_data[ definition.name] = definition.default_value if not self.pk: if not hasattr(self, 'tenant'): self.tenant = get_current_tenant() super(TenantSpecificFieldsModelMixin, self).save(*args, **kwargs) self.create_tenant_specific_fields(tenant_specific_fields_data) else: super(TenantSpecificFieldsModelMixin, self).save(*args, **kwargs) self.update_tenant_specific_fields(tenant_specific_fields_data) self.tenant_specific_fields_data = {}
def __init__(self, instance_field_name, instance=None, tenant_fields={}, tenant_default_fields_values={}): if not instance: instance = get_current_tenant() self.instance = instance self.instance_field_name = instance_field_name self.tenant = get_current_tenant() self.tenant_fields = tenant_fields self.tenant_default_fields_values = tenant_default_fields_values if sys.version_info < (3, 4): self.TYPES_TO_INTERNAL_MAP['string'].append(type(u''))
def save(self, *args, **kwargs): if not hasattr(self, 'tenant'): self.tenant = get_current_tenant() if getattr(self, 'tenant', False): return super(SingleTenantModelMixin, self).save(*args, **kwargs) else: raise TenantNotFoundError()
def save(self, *args, **kwargs): tenant = get_current_tenant() if tenant: instance = super(MultipleTenantsModelMixin, self).save(*args, **kwargs) self.tenants.add(tenant) return instance else: raise TenantNotFoundError()
def get_queryset(self, tenant=None): if not tenant: tenant = get_current_tenant() if tenant: return super(MultipleTenantModelManager, self).get_queryset().filter(tenants=tenant) else: return super(MultipleTenantModelManager, self).get_queryset().none() else: return super(MultipleTenantModelManager, self).get_queryset().filter(tenants=tenant)
def post(self, request, *args, **kwargs): serializer = self.get_serializer_class()( data=self.request.data, context={'request': request, 'view': self}) if serializer.is_valid(): serializer.save() tenant = get_current_tenant() return_serializer = self.get_serializer_class()(tenant) return response.Response(return_serializer.data, status=status.HTTP_200_OK) return response.Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, tenant=None, *args, **kwargs): if not tenant: tenant = get_current_tenant() if tenant: kwargs['tenant'] = tenant return super(SingleTenantModelManager, self).create(*args, **kwargs) else: raise TenantNotFoundError() else: return super(SingleTenantModelManager, self).create(tenant=tenant, *args, **kwargs)
def get_queryset(self, tenant=None, *args, **kwargs): if not tenant: tenant = get_current_tenant() if tenant: return super(SingleTenantModelManager, self).get_queryset(*args, **kwargs).filter(tenant=tenant) else: return super(SingleTenantModelManager, self).get_queryset(*args, **kwargs).none() else: return super(SingleTenantModelManager, self).get_queryset(*args, **kwargs).filter(tenant=tenant)
def create(self, tenant=None, *args, **kwargs): if not tenant: tenant = get_current_tenant() if tenant: with transaction.atomic(): try: model_instance = self.get_original_queryset().get( **kwargs) except ObjectDoesNotExist: model_instance = super(MultipleTenantModelManager, self).create(*args, **kwargs) model_instance.tenants.add(tenant) return model_instance else: raise TenantNotFoundError() else: model_instance = super(MultipleTenantModelManager, self).create(*args, **kwargs) model_instance.tenants.add(tenant) return model_instance
def _get_tenant_permissions(self, user_obj, obj, from_name): if not user_obj.is_active or user_obj.is_anonymous( ) or obj is not None: return set() tenant = get_current_tenant() if not tenant: return set() perm_cache_name = '_tenant_%s_perm_cache' % from_name if (not hasattr(user_obj, perm_cache_name) or not getattr(user_obj, perm_cache_name).get(tenant.pk)): if user_obj.is_superuser: relationship_perms = Permission.objects.all() relationship_perms = relationship_perms.values_list( 'content_type__app_label', 'codename').order_by() else: try: relationship = TenantRelationship.objects.get( user=user_obj, tenant=tenant) except TenantRelationship.DoesNotExist: relationship_perms = set() else: relationship_perms = getattr( self, '_get_%s_tenant_permissions' % from_name)(relationship) relationship_perms = relationship_perms.values_list( 'content_type__app_label', 'codename').order_by() setattr( user_obj, perm_cache_name, { tenant.pk: {"%s.%s" % (ct, name) for ct, name in relationship_perms} }) return getattr(user_obj, perm_cache_name).get(tenant.pk)
def get_serializer(self, *args, **kwargs): if self.request.method == 'POST': data = kwargs.get('data', {}) data['tenant'] = get_current_tenant() kwargs['data'] = data return super(TenantSiteListView, self).get_serializer(*args, **kwargs)
def get_object(self): return get_current_tenant()
def current_tenant(request): return { 'tenant': get_current_tenant(), }