From 694c4f0809fda72a5b7e355ef3e2e92cb3c4d9f1 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 1 Sep 2018 17:04:28 +0100 Subject: [PATCH] blog: Render and cache HTML content Signed-off-by: Michael Tremer --- src/backend/blog.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/backend/blog.py b/src/backend/blog.py index 2fe13c24..3184b0f1 100644 --- a/src/backend/blog.py +++ b/src/backend/blog.py @@ -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({ -- 2.47.2