]> git.ipfire.org Git - ipfire.org.git/commitdiff
blog: Render and cache HTML content
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 1 Sep 2018 16:04:28 +0000 (17:04 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 1 Sep 2018 16:04:28 +0000 (17:04 +0100)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/blog.py

index 2fe13c248c52ae96f79fb2f4f0a2c8b32a085de1..3184b0f167dcd47636b34253772cfa473fcac0e5 100644 (file)
@@ -93,13 +93,16 @@ class Blog(misc.Object):
                                ORDER BY ts_rank(search_index.document, to_tsquery('english', %s)) DESC \
                        LIMIT %s", query, query, limit)
 
-       def create_post(self, title, text, author, tags=[]):
+       def create_post(self, title, text, author, tags=[], lang="markdown"):
                """
                        Creates a new post and returns the resulting Post object
                """
-               return self._get_post("INSERT INTO blog(title, slug, text, author_uid, tags) \
-                       VALUES(%s, %s, %s, %s, %s) RETURNING *", title, self._make_slug(title),
-                       text, author.uid, list(tags))
+               # Pre-render HTML
+               html = self._render_text(text, lang=lang)
+
+               return self._get_post("INSERT INTO blog(title, slug, text, html, lang, author_uid, tags) \
+                       VALUES(%s, %s, %s, %s, %s, %s, %s) RETURNING *", title, self._make_slug(title), text,
+                       html, lang, author.uid, list(tags))
 
        def _make_slug(self, s):
                # Remove any non-ASCII characters
@@ -314,11 +317,12 @@ class Post(misc.Object):
                slug = self.backend.blog._make_slug(title) \
                        if not self.is_published() and not self.title == title else self.slug
 
-               # XXX render HTML
+               # Render and cache HTML
+               html = self.backend.blog._render_text(text, lang=self.lang)
 
-               self.db.execute("UPDATE blog SET title = %s, slug = %s, text = %s, \
+               self.db.execute("UPDATE blog SET title = %s, slug = %s, text = %s, html = %s, \
                        tags = %s, updated_at = CURRENT_TIMESTAMP WHERE id = %s",
-                       title, slug, text, list(tags), self.id)
+                       title, slug, text, html, list(tags), self.id)
 
                # Update cache
                self.data.update({