Organization tenancy and frontend #16

Merged
rixx merged 26 commits from 6-organizations into main 2025-03-21 12:32:53 +00:00
5 changed files with 39 additions and 14 deletions
Showing only changes of commit 2dcc5650a9 - Show all commits

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

View file

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

View file

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