def test_default_storage_without_tenant(settings): set_current_tenant(None) s = TenantFileSystemStorage() file = SimpleUploadedFile('test.txt', b'any content') s.save('test.txt', file) assert s.exists('test.txt') assert s.path('test.txt') == f'{settings.MEDIA_ROOT}/test.txt' assert s.url('test.txt') == f'{settings.MEDIA_URL}test.txt'
def process_view(self, request, view_func, view_args, view_kwargs): if not request.user.is_authenticated: return None request.tenant = _get_tenant(request) set_current_tenant(request.tenant) if not request.tenant and not _tenant_not_required(view_func): return redirect(settings.EASY_TENANTS_REDIRECT_URL)
def test_tenant_context(db): tenant1 = StoreTenant.objects.create() tenant2 = StoreTenant.objects.create() set_current_tenant(tenant1) with tenant_context(tenant2): assert tenant2 == get_current_tenant() assert tenant1 == get_current_tenant()
def test_with_tenant_in_context(self, logged_client): tenant = StoreTenant.objects.create() set_current_tenant(tenant) session = logged_client.session session['tenant_id'] = str(tenant.id) session.save() response = logged_client.get('/') assert response.status_code == 200 assert hasattr(response, 'request')
def test_default_middleware_with_tenant_in_context(settings, logged_client): settings.MIDDLEWARE = [ 'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'easy_tenants.middleware.DefaultTenantMiddleware', ] settings.EASY_TENANTS_REDIRECT_URL = 'store-list' tenant = StoreTenant.objects.create() set_current_tenant(tenant) session = logged_client.session session['tenant_id'] = tenant.id session.save() response = logged_client.get('/') assert response.status_code == 200 assert hasattr(response, 'request')
def __call__(self, request): assert hasattr(request, 'session'), ( "The Django authentication middleware requires session middleware " "to be installed. Edit your MIDDLEWARE setting to insert " "'django.contrib.sessions.middleware.SessionMiddleware' before " "'django.contrib.auth.middleware.AuthenticationMiddleware'.") assert hasattr(request, 'user'), ( "The easy_tenants middleware requires authentication middleware " "to be installed. Edit your MIDDLEWARE setting to insert " "'django.contrib.auth.middleware.AuthenticationMiddleware' before " "'easy_tenants.middleware.DefaultTenantMiddleware'.") response = None request.tenant = self.get_tenant(request) set_current_tenant(request.tenant) if not request.tenant or not request.user.is_authenticated: response = response or self.redirect_to(request) response = response or self.get_response(request) return response
def post(self, request, *args, **kwargs): tenant = get_object_or_404(Tenant, pk=kwargs['pk']) set_current_tenant(tenant) request.session[settings.EASY_TENANTS_SESSION_KEY] = kwargs['pk'] return super().post(request, *args, **kwargs)
def test_get_and_set_current_tenant_thread_local(db): tenant = StoreTenant.objects.create() set_current_tenant(tenant) assert tenant == get_current_tenant()