Skip to content

Commit b931a55

Browse files
committed
Used TextChoices for blog.Entry.content_format
1 parent 913bc5d commit b931a55

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

blog/models.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,24 @@ def active(self):
2828
return self.filter(is_active=True)
2929

3030

31-
CONTENT_FORMAT_CHOICES = (
32-
("reST", "reStructuredText"),
33-
("html", "Raw HTML"),
34-
)
31+
class ContentFormat(models.TextChoices):
32+
REST = "reST", "reStructuredText"
33+
HTML = "html", "Raw HTML"
34+
35+
@classmethod
36+
def to_html(cls, fmt, source):
37+
"""
38+
Convert the given source from the given format to HTML
39+
"""
40+
if not fmt or fmt == cls.HTML:
41+
return source
42+
if fmt == cls.REST:
43+
return publish_parts(
44+
source=source,
45+
writer_name="html",
46+
settings_overrides=BLOG_DOCUTILS_SETTINGS,
47+
)["fragment"]
48+
raise ValueError(f"Unsupported format {fmt}")
3549

3650

3751
class Entry(models.Model):
@@ -52,7 +66,7 @@ class Entry(models.Model):
5266
"publication date must be in the past."
5367
),
5468
)
55-
content_format = models.CharField(choices=CONTENT_FORMAT_CHOICES, max_length=50)
69+
content_format = models.CharField(choices=ContentFormat.choices, max_length=50)
5670
summary = models.TextField()
5771
summary_html = models.TextField()
5872
body = models.TextField()
@@ -88,20 +102,8 @@ def is_published(self):
88102
is_published.boolean = True
89103

90104
def save(self, *args, **kwargs):
91-
if self.content_format == "html":
92-
self.summary_html = self.summary
93-
self.body_html = self.body
94-
elif self.content_format == "reST":
95-
self.summary_html = publish_parts(
96-
source=self.summary,
97-
writer_name="html",
98-
settings_overrides=BLOG_DOCUTILS_SETTINGS,
99-
)["fragment"]
100-
self.body_html = publish_parts(
101-
source=self.body,
102-
writer_name="html",
103-
settings_overrides=BLOG_DOCUTILS_SETTINGS,
104-
)["fragment"]
105+
self.summary_html = ContentFormat.to_html(self.content_format, self.summary)
106+
self.body_html = ContentFormat.to_html(self.content_format, self.body)
105107
super().save(*args, **kwargs)
106108
self.invalidate_cached_entry()
107109

blog/tests.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.urls import reverse
66
from django.utils import timezone
77

8-
from .models import Entry, Event
8+
from .models import ContentFormat, Entry, Event
99
from .sitemaps import WeblogSitemap
1010

1111

@@ -76,6 +76,24 @@ def test_docutils_safe(self):
7676
self.assertIn("<p>&quot;raw&quot; directive disabled.</p>", entry.body_html)
7777
self.assertIn(".. raw:: html\n :file: somefile", entry.body_html)
7878

79+
def test_content_format_html(self):
80+
entry = Entry.objects.create(
81+
pub_date=self.now,
82+
slug="a",
83+
body="<strong>test</strong>",
84+
content_format=ContentFormat.HTML,
85+
)
86+
self.assertHTMLEqual(entry.body_html, "<strong>test</strong>")
87+
88+
def test_content_format_reST(self):
89+
entry = Entry.objects.create(
90+
pub_date=self.now,
91+
slug="a",
92+
body="**test**",
93+
content_format=ContentFormat.REST,
94+
)
95+
self.assertHTMLEqual(entry.body_html, "<p><strong>test</strong></p>")
96+
7997

8098
class EventTestCase(DateTimeMixin, TestCase):
8199
def test_manager_past_future(self):

0 commit comments

Comments
 (0)