diff options
| author | Baptiste Mispelon <bmispelon@gmail.com> | 2024-10-17 16:15:10 +0200 |
|---|---|---|
| committer | Baptiste Mispelon <bmispelon@gmail.com> | 2024-10-25 18:10:08 +0200 |
| commit | b931a5530003d53628599132077696acd5862386 (patch) | |
| tree | ab2aefc5d8ce16edb43ed1bf744b8927adc510e8 /blog | |
| parent | 913bc5db278995d3d2ce5ebcd2abe6390aa0a78a (diff) | |
Used TextChoices for blog.Entry.content_format
Diffstat (limited to 'blog')
| -rw-r--r-- | blog/models.py | 40 | ||||
| -rw-r--r-- | blog/tests.py | 20 |
2 files changed, 40 insertions, 20 deletions
diff --git a/blog/models.py b/blog/models.py index d33a487f..34025f49 100644 --- a/blog/models.py +++ b/blog/models.py @@ -28,10 +28,24 @@ class EntryQuerySet(models.QuerySet): return self.filter(is_active=True) -CONTENT_FORMAT_CHOICES = ( - ("reST", "reStructuredText"), - ("html", "Raw HTML"), -) +class ContentFormat(models.TextChoices): + REST = "reST", "reStructuredText" + HTML = "html", "Raw HTML" + + @classmethod + def to_html(cls, fmt, source): + """ + Convert the given source from the given format to HTML + """ + if not fmt or fmt == cls.HTML: + return source + if fmt == cls.REST: + return publish_parts( + source=source, + writer_name="html", + settings_overrides=BLOG_DOCUTILS_SETTINGS, + )["fragment"] + raise ValueError(f"Unsupported format {fmt}") class Entry(models.Model): @@ -52,7 +66,7 @@ class Entry(models.Model): "publication date must be in the past." ), ) - content_format = models.CharField(choices=CONTENT_FORMAT_CHOICES, max_length=50) + content_format = models.CharField(choices=ContentFormat.choices, max_length=50) summary = models.TextField() summary_html = models.TextField() body = models.TextField() @@ -88,20 +102,8 @@ class Entry(models.Model): is_published.boolean = True def save(self, *args, **kwargs): - if self.content_format == "html": - self.summary_html = self.summary - self.body_html = self.body - elif self.content_format == "reST": - self.summary_html = publish_parts( - source=self.summary, - writer_name="html", - settings_overrides=BLOG_DOCUTILS_SETTINGS, - )["fragment"] - self.body_html = publish_parts( - source=self.body, - writer_name="html", - settings_overrides=BLOG_DOCUTILS_SETTINGS, - )["fragment"] + self.summary_html = ContentFormat.to_html(self.content_format, self.summary) + self.body_html = ContentFormat.to_html(self.content_format, self.body) super().save(*args, **kwargs) self.invalidate_cached_entry() diff --git a/blog/tests.py b/blog/tests.py index 31ae7090..53f0d2f1 100644 --- a/blog/tests.py +++ b/blog/tests.py @@ -5,7 +5,7 @@ from django.test import TestCase from django.urls import reverse from django.utils import timezone -from .models import Entry, Event +from .models import ContentFormat, Entry, Event from .sitemaps import WeblogSitemap @@ -76,6 +76,24 @@ class EntryTestCase(DateTimeMixin, TestCase): self.assertIn("<p>"raw" directive disabled.</p>", entry.body_html) self.assertIn(".. raw:: html\n :file: somefile", entry.body_html) + def test_content_format_html(self): + entry = Entry.objects.create( + pub_date=self.now, + slug="a", + body="<strong>test</strong>", + content_format=ContentFormat.HTML, + ) + self.assertHTMLEqual(entry.body_html, "<strong>test</strong>") + + def test_content_format_reST(self): + entry = Entry.objects.create( + pub_date=self.now, + slug="a", + body="**test**", + content_format=ContentFormat.REST, + ) + self.assertHTMLEqual(entry.body_html, "<p><strong>test</strong></p>") + class EventTestCase(DateTimeMixin, TestCase): def test_manager_past_future(self): |
