Add urlman for easier url access

With a variable slug, reverse()/{% url %} is rough to use.
This commit is contained in:
Tobias Kunze 2025-03-20 11:32:46 +01:00
parent 8be1c86deb
commit 8a98f1ac33
6 changed files with 22 additions and 8 deletions

View file

@ -1,6 +1,6 @@
import urlman
from django.conf import settings
from django.db import models
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 _
@ -33,14 +33,16 @@ class Organization(ServalaModelMixin, models.Model):
verbose_name=_("Members"),
)
class urls(urlman.Urls):
base = "/org/{self.slug}/"
details = "{base}details/"
@cached_property
def slug(self):
return f"{slugify(self.name)}-{self.id}"
def get_absolute_url(self):
return reverse(
"frontend:organization.dashboard", kwargs={"organization": self.slug}
)
return self.urls.base
def set_owner(self, user):
OrganizationMembership.objects.filter(user=user, organization=self).delete()

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 request.organization %}{{ request.organization.get_absolute_url }}{% else %}/{% endif %}">
<a href="{% if request.organization %}{{ request.organization.urls.base }}{% else %}/{% endif %}">
<img src="" alt="{% translate 'Logo' %}" srcset="">
</a>
</div>
@ -73,7 +73,7 @@
id="organization-dropdown">
{% for organization in user_organizations %}
<a class="dropdown-item{% if organization == request.organization %} active{% endif %}"
href="{{ organization.get_absolute_url }}">
href="{{ organization.urls.base }}">
<i class="bi bi-building-fill me-1"></i>
{{ organization.name }}
</a>

View file

@ -12,7 +12,7 @@ urlpatterns = [
name="organization.create",
),
path(
"<slug:organization>/",
"org/<slug:organization>/",
include(
[
path(

View file

@ -12,7 +12,7 @@ class OrganizationCreateView(FormView):
instance = form.instance.create_organization(
form.instance, owner=self.request.user
)
return redirect(instance.get_absolute_url())
return redirect(instance.urls.base)
class OrganizationDashboardView(TemplateView):