Add organization context to every request

This commit is contained in:
Tobias Kunze 2025-03-20 10:04:48 +01:00
parent 7f389434a4
commit 2dcc5650a9
5 changed files with 39 additions and 14 deletions

View file

@ -0,0 +1,22 @@
from django.shortcuts import get_object_or_404
from django.urls import resolve
from servala.core.models import Organization
class OrganizationMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
url = resolve(request.path_info)
organization_slug = url.kwargs.get("organization")
if organization_slug:
pk = organization_slug.rsplit("-", maxsplit=1)[-1]
request.organization = get_object_or_404(Organization, pk=pk)
else:
request.organization = None
return self.get_response(request)

View file

@ -2,4 +2,4 @@ def add_organizations(request):
if not request.user.is_authenticated:
return {"user_organizations": []}
return {"user_organizations": request.user.organizations.all()}
return {"user_organizations": request.user.organizations.all().order_by("name")}

View file

@ -5,7 +5,7 @@
<div class="sidebar-header position-relative">
<div class="d-flex justify-content-between align-items-center">
<div class="logo">
<a href="{% if current_organization %}{{ current_organization.get_absolute_url }}{% else %}/{% endif %}">
<a href="{% if request.organization %}{{ request.organization.get_absolute_url }}{% else %}/{% endif %}">
<img src="" alt="{% translate 'Logo' %}" srcset="">
</a>
</div>
@ -67,21 +67,23 @@
{# request.user.is_authenticated #}
<li class="sidebar-item">
{% if user_organizations.count %}
<button class="btn btn-primary dropdown-toggle me-1"
<button class="btn btn-outline-primary dropdown-toggle w-100"
type="button"
id="organizationDropdown"
data-bs-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">
{% if current_organization %}
{{ current_organization.name }}
{% if request.organization %}
{{ request.organization.name }}
{% else %}
{% translate "Organizations" %}
{% endif %}
</button>
<div class="dropdown-menu" aria-labelledby="organizationDropdown">
<div class="dropdown-menu shadow"
aria-labelledby="organizationDropdown"
id="organization-dropdown">
{% for organization in user_organizations %}
<a class="dropdown-item{% if organization == current_organization %} active{% endif %}"
<a class="dropdown-item{% if organization == request.organization %} active{% endif %}"
href="{{ organization.get_absolute_url }}">
<i class="bi bi-building-fill me-1"></i>
{{ organization.name }}
@ -99,9 +101,9 @@
</a>
{% endif %}
</li>
{% if current_organization %}
{% if request.organization %}
<li class="sidebar-item">
<a href="{{ current_organization.get_absolute_url }}"
<a href="{{ request.organization.get_absolute_url }}"
class='sidebar-link'>
<i class="bi bi-grid-fill"></i>
<span>{% translate 'Dashboard' %}</span>

View file

@ -1,3 +1,4 @@
from django.shortcuts import redirect
from django.views.generic import FormView, TemplateView
from servala.frontend.forms import OrganizationCreateForm
@ -8,11 +9,10 @@ class OrganizationCreateView(FormView):
template_name = "frontend/organizations/create.html"
def form_valid(self, form):
form.instance.create_organization(form.instance, owner=self.request.user)
return super().form_valid(form)
def get_success_url(self):
return "/"
instance = form.instance.create_organization(
form.instance, owner=self.request.user
)
return redirect(instance.get_absolute_url())
class OrganizationDashboardView(TemplateView):

View file

@ -116,6 +116,7 @@ MIDDLEWARE = [
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"allauth.account.middleware.AccountMiddleware",
"django.contrib.auth.middleware.LoginRequiredMiddleware",
"servala.core.middleware.OrganizationMiddleware",
]
LOGIN_URL = "account_login"