From a1977f3946d138848ad0018697bf1c818297a4d7 Mon Sep 17 00:00:00 2001 From: dendencat <33782903+dendencat@users.noreply.github.com> Date: Tue, 16 Sep 2025 20:23:42 +0900 Subject: [PATCH] Fix linkify import and truncate generated slugs --- techblog_cms/models.py | 20 ++++++++++++++++++-- techblog_cms/templatetags/markdown_filter.py | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/techblog_cms/models.py b/techblog_cms/models.py index 1f65f32..06602a6 100644 --- a/techblog_cms/models.py +++ b/techblog_cms/models.py @@ -53,10 +53,26 @@ class Article(models.Model): image = models.ImageField(upload_to='articles/', blank=True, null=True) def _generate_unique_slug(self): + slug_field = self._meta.get_field('slug') + max_length = slug_field.max_length or 50 + suffix_length = 8 base_slug = slugify(self.title) or 'article' + + if max_length > suffix_length + 1: + base_max_length = max_length - (suffix_length + 1) + base_slug = base_slug[:base_max_length].rstrip('-') + if not base_slug: + base_slug = 'article' + base_slug = base_slug[:base_max_length] + else: + base_slug = '' + while True: - hash_fragment = uuid.uuid4().hex[:8] - candidate = f"{base_slug}-{hash_fragment}" + if base_slug: + hash_fragment = uuid.uuid4().hex[:suffix_length] + candidate = f"{base_slug}-{hash_fragment}" + else: + candidate = uuid.uuid4().hex[:max_length] if not Article.objects.filter(slug=candidate).exclude(pk=self.pk).exists(): return candidate diff --git a/techblog_cms/templatetags/markdown_filter.py b/techblog_cms/templatetags/markdown_filter.py index 830d2e9..1bce420 100644 --- a/techblog_cms/templatetags/markdown_filter.py +++ b/techblog_cms/templatetags/markdown_filter.py @@ -19,7 +19,7 @@ def markdown_to_html(text): 'toc', # Table of contents 'fenced_code', # Fenced code blocks 'nl2br', # Convert newlines to
- 'linkify', # Auto-link plain URLs + 'markdown.extensions.linkify', # Auto-link plain URLs ], extension_configs={ 'codehilite': { 'linenums': False, # Disable line numbers