def tenant_ctx(db):
    """Create a tenant a set context"""
    from easy_tenants import tenant_context, get_tenant_model

    tenant = get_tenant_model().objects.create()
    with tenant_context(tenant):
        yield tenant
Beispiel #2
0
def _get_tenant(request):
    """Get tenant saved in session request"""
    tenant = None
    tenant_id = request.session.get(settings.EASY_TENANTS_SESSION_KEY, None)

    if tenant_id:
        tenant = get_tenant_model().objects.filter(id=tenant_id).first()

    return tenant
Beispiel #3
0
def set_tenant(request, pk):
    """
    Save tenant in session
    """
    Tenant = get_tenant_model()
    tenant = get_object_or_404(Tenant, pk=pk)
    request.session["tenant_id"] = str(tenant.id)

    return redirect("home")
    def get_tenant(self, request):
        """Get tenant saved in session request"""
        SESSION_KEY = "tenant_id"
        tenant = None
        tenant_id = request.session.get(SESSION_KEY, None)

        if tenant_id:
            tenant = get_tenant_model().objects.filter(id=tenant_id).first()

        return tenant
Beispiel #5
0
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404, resolve_url
from django.views.generic import RedirectView

from easy_tenants import set_current_tenant, get_tenant_model
from easy_tenants.conf import settings

Tenant = get_tenant_model()


class SetTenantView(LoginRequiredMixin, RedirectView):
    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 get_redirect_url(self, *args, **kwargs):
        redirect_url = settings.EASY_TENANTS_REDIRECT_URL
        if redirect_url:
            return resolve_url(redirect_url)

        return super().get_redirect_url(*args, **kwargs)


set_tenant = SetTenantView.as_view()
Beispiel #6
0
def test_get_tenant_model():
    assert StoreTenant == get_tenant_model()