Add urlman for easier url access
With a variable slug, reverse()/{% url %} is rough to use.
This commit is contained in:
parent
8be1c86deb
commit
8a98f1ac33
6 changed files with 22 additions and 8 deletions
|
@ -15,6 +15,7 @@ dependencies = [
|
|||
"psycopg2-binary>=2.9.10",
|
||||
"pyjwt>=2.10.1",
|
||||
"requests>=2.32.3",
|
||||
"urlman>=2.0.2",
|
||||
]
|
||||
|
||||
[dependency-groups]
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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>
|
||||
|
|
|
@ -12,7 +12,7 @@ urlpatterns = [
|
|||
name="organization.create",
|
||||
),
|
||||
path(
|
||||
"<slug:organization>/",
|
||||
"org/<slug:organization>/",
|
||||
include(
|
||||
[
|
||||
path(
|
||||
|
|
|
@ -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):
|
||||
|
|
11
uv.lock
generated
11
uv.lock
generated
|
@ -676,6 +676,7 @@ dependencies = [
|
|||
{ name = "psycopg2-binary" },
|
||||
{ name = "pyjwt" },
|
||||
{ name = "requests" },
|
||||
{ name = "urlman" },
|
||||
]
|
||||
|
||||
[package.dev-dependencies]
|
||||
|
@ -702,6 +703,7 @@ requires-dist = [
|
|||
{ name = "psycopg2-binary", specifier = ">=2.9.10" },
|
||||
{ name = "pyjwt", specifier = ">=2.10.1" },
|
||||
{ name = "requests", specifier = ">=2.32.3" },
|
||||
{ name = "urlman", specifier = ">=2.0.2" },
|
||||
]
|
||||
|
||||
[package.metadata.requires-dev]
|
||||
|
@ -763,3 +765,12 @@ sdist = { url = "https://files.pythonhosted.org/packages/aa/63/e53da845320b757bf
|
|||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/c8/19/4ec628951a74043532ca2cf5d97b7b14863931476d117c471e8e2b1eb39f/urllib3-2.3.0-py3-none-any.whl", hash = "sha256:1cee9ad369867bfdbbb48b7dd50374c0967a0bb7710050facf0dd6911440e3df", size = 128369 },
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "urlman"
|
||||
version = "2.0.2"
|
||||
source = { registry = "https://pypi.org/simple" }
|
||||
sdist = { url = "https://files.pythonhosted.org/packages/65/c3/cc163cadf40a03d23d522d050ffa147c0589ccd7992a2cc4dd2b02aa9886/urlman-2.0.2.tar.gz", hash = "sha256:231afe89d0d0db358fe7a2626eb39310e5bf5911f3796318955cbe77e1b39601", size = 7684 }
|
||||
wheels = [
|
||||
{ url = "https://files.pythonhosted.org/packages/f4/0c/e8a418c9bc9349e7869e88a5b439cf39c4f6f8942da858000944c94a8f01/urlman-2.0.2-py2.py3-none-any.whl", hash = "sha256:2505bf310be424ffa6f4965a6f643ce32dc6194f61a3c5989f2f56453c614814", size = 8028 },
|
||||
]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue