Add django-scopes

This commit is contained in:
Tobias Kunze 2025-03-20 10:32:03 +01:00
parent 2dcc5650a9
commit 6e6f2d0993
4 changed files with 22 additions and 2 deletions

View file

@ -1,5 +1,6 @@
from django.shortcuts import get_object_or_404
from django.urls import resolve
from django_scopes import scope
from servala.core.models import Organization
@ -16,7 +17,8 @@ class OrganizationMiddleware:
if organization_slug:
pk = organization_slug.rsplit("-", maxsplit=1)[-1]
request.organization = get_object_or_404(Organization, pk=pk)
else:
request.organization = None
with scope(organization=request.organization):
return self.get_response(request)
request.organization = None
return self.get_response(request)

View file

@ -4,6 +4,7 @@ from django.urls import reverse
from django.utils.functional import cached_property
from django.utils.text import slugify
from django.utils.translation import gettext_lazy as _
from django_scopes import ScopedManager
from .mixins import ServalaModelMixin
@ -136,6 +137,8 @@ class OrganizationMembership(ServalaModelMixin, models.Model):
verbose_name=_("Role"),
)
objects = ScopedManager(organization="organization")
class Meta:
verbose_name = _("Organization membership")
verbose_name_plural = _("Organization memberships")