diff --git a/app/modules/hosting/services/hosted_site_service.py b/app/modules/hosting/services/hosted_site_service.py index 722e9aeb..b404a193 100644 --- a/app/modules/hosting/services/hosted_site_service.py +++ b/app/modules/hosting/services/hosted_site_service.py @@ -34,20 +34,30 @@ ALLOWED_TRANSITIONS: dict[HostedSiteStatus, list[HostedSiteStatus]] = { } -def _slugify(name: str) -> str: - """Generate a short URL-safe slug from a business name or domain.""" +def _slugify(name: str, max_length: int = 30) -> str: + """Generate a short URL-safe slug from a domain or business name. + + Priority: domain name (clean) > first 3 words of business name > full slug truncated. + """ slug = name.lower().strip() - # Remove protocol/www if domain was passed + # If it looks like a domain, extract the hostname part for prefix in ["https://", "http://", "www."]: if slug.startswith(prefix): slug = slug[len(prefix):] slug = slug.rstrip("/") - # Remove TLD if it looks like a domain if "." in slug and " " not in slug: + # Domain: remove TLD → batirenovation-strasbourg.fr → batirenovation-strasbourg slug = slug.rsplit(".", 1)[0] + else: + # Business name: take first 3 meaningful words for brevity + words = re.sub(r"[^a-z0-9\s]", "", slug).split() + # Skip filler words + filler = {"the", "le", "la", "les", "de", "du", "des", "et", "and", "und", "die", "der", "das"} + words = [w for w in words if w not in filler][:3] + slug = " ".join(words) slug = re.sub(r"[^a-z0-9\s-]", "", slug) slug = re.sub(r"[\s-]+", "-", slug) - return slug.strip("-")[:30] + return slug.strip("-")[:max_length] class HostedSiteService: