feat: auto-populate slug fields on save
Add clubmanager.base.unique_slugify(instance, value, scope=...): slugify a source value, truncate to the field's max_length, and append -2/-3/... to stay unique within a scope. ClubScopedModel gains a slug_source hook that fills a blank slug (unique per club) on save. Wire it up so every SlugField auto-populates from its natural source when left blank (explicit values are always kept): - shop.Product.slug <- name (per club) - formbuilder.Form.slug <- title (per club) - formbuilder.Field.key <- label (per form) Club.slug already auto-populated; refactor it onto the shared helper. Also fix shop.Product.slug's multi-tenancy bug: it was globally unique (unique=True); make it unique per club like the others. Migrations added. Full suite at 100% coverage. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ import uuid
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from django.db import models
|
||||
from django.utils.text import slugify
|
||||
|
||||
from club.tenancy import require_current_club
|
||||
|
||||
@@ -9,6 +10,30 @@ if TYPE_CHECKING:
|
||||
from club.models import Club
|
||||
|
||||
|
||||
def unique_slugify(instance, value, *, slug_field="slug", scope=None):
|
||||
"""Return a slug derived from ``value``, unique within ``scope``.
|
||||
|
||||
Truncates to the slug field's ``max_length`` and appends ``-2``, ``-3``, …
|
||||
on collision. ``scope`` is a dict of field lookups the uniqueness is
|
||||
checked within (e.g. ``{"club": club}`` for per-club, ``{}``/``None`` for
|
||||
global).
|
||||
"""
|
||||
max_length = instance._meta.get_field(slug_field).max_length
|
||||
base = slugify(value)[:max_length] or "item"
|
||||
|
||||
queryset = type(instance)._default_manager.exclude(pk=instance.pk)
|
||||
if scope:
|
||||
queryset = queryset.filter(**scope)
|
||||
|
||||
slug = base
|
||||
suffix = 2
|
||||
while queryset.filter(**{slug_field: slug}).exists():
|
||||
tail = f"-{suffix}"
|
||||
slug = f"{base[: max_length - len(tail)]}{tail}"
|
||||
suffix += 1
|
||||
return slug
|
||||
|
||||
|
||||
class TenantQuerySet(models.QuerySet):
|
||||
def for_club(self, club: Club):
|
||||
return self.filter(club=club)
|
||||
@@ -32,6 +57,10 @@ class ClubScopedModel(UUIDModel):
|
||||
club = models.ForeignKey("club.Club", on_delete=models.CASCADE, related_name="%(class)ss")
|
||||
objects = TenantQuerySet.as_manager()
|
||||
|
||||
# Subclasses with a ``slug`` field set this to the source field name (e.g.
|
||||
# "name"/"title") to auto-populate the slug — unique per club — on save.
|
||||
slug_source = None
|
||||
|
||||
class Meta:
|
||||
abstract = True
|
||||
|
||||
@@ -39,4 +68,7 @@ class ClubScopedModel(UUIDModel):
|
||||
if self.club_id is None:
|
||||
self.club = require_current_club()
|
||||
|
||||
if self.slug_source and not self.slug:
|
||||
self.slug = unique_slugify(self, getattr(self, self.slug_source), scope={"club": self.club})
|
||||
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user