feat(tenancy): resolve active club from request subdomain
Add row-based multi-tenancy plumbing keyed on Club as the tenant root: - ClubTenantMiddleware maps the request's subdomain to a Club by slug, storing it on request.club and in a contextvar so service-layer code and management commands can read it via get_current_club(). Resolution honours CLUBMANAGER_BASE_DOMAIN (e.g. ajax-united.clubmanager.app), falling back to generic slug.example.com hosts, and ignores the bare base domain, www, and unknown slugs. - Club gains a unique slug (auto-derived from name on save) plus a ClubManager.current() accessor for the active tenant. - ClubScopedModel gets a TenantQuerySet (.for_club()/.current()) and auto-fills club from the active context on save. Contextvar helpers live in club.tenancy; Club is imported lazily there and in clubmanager.base to avoid an import cycle with club.models. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,12 +1,27 @@
|
||||
import datetime
|
||||
|
||||
from django.db import models
|
||||
from django.utils import timezone
|
||||
from django.utils.text import slugify
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from clubmanager.base import UUIDModel
|
||||
from clubmanager.base import ClubScopedModel, UUIDModel
|
||||
from members.models import Member
|
||||
|
||||
|
||||
class ClubManager(models.Manager):
|
||||
def current(self):
|
||||
"""Return the club for the active tenant context, if any."""
|
||||
from .tenancy import get_current_club
|
||||
|
||||
return get_current_club()
|
||||
|
||||
|
||||
class Club(UUIDModel):
|
||||
name = models.CharField(_("name"), max_length=255)
|
||||
slug = models.SlugField(_("slug"), max_length=255, unique=True, blank=True, help_text=_("Drives subdomain / path resolution (e.g. ajax-united.clubmanager.app)."))
|
||||
|
||||
objects = ClubManager()
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("club")
|
||||
@@ -16,6 +31,21 @@ class Club(UUIDModel):
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
if not self.slug:
|
||||
self.slug = self._unique_slug()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
def _unique_slug(self):
|
||||
base = slugify(self.name) or "club"
|
||||
slug = base
|
||||
suffix = 2
|
||||
existing = Club.objects.exclude(pk=self.pk)
|
||||
while existing.filter(slug=slug).exists():
|
||||
slug = f"{base}-{suffix}"
|
||||
suffix += 1
|
||||
return slug
|
||||
|
||||
|
||||
class ClubMembership(UUIDModel):
|
||||
member = models.ForeignKey(Member, on_delete=models.CASCADE, related_name="member_of", verbose_name=_("member"))
|
||||
@@ -31,3 +61,22 @@ class ClubMembership(UUIDModel):
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.club} - {self.member}"
|
||||
|
||||
|
||||
class Season(ClubScopedModel):
|
||||
start_date = models.DateField(_("start date"))
|
||||
end_date = models.DateField(_("end date"))
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.start_date} - {self.end_date}"
|
||||
|
||||
class Meta:
|
||||
verbose_name = _("season")
|
||||
verbose_name_plural = _("seasons")
|
||||
|
||||
@classmethod
|
||||
def get_current(cls, date: datetime.date | None = None):
|
||||
if date is None:
|
||||
date = timezone.now().date()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user