From 016b40f99d679f0787f7c8a5f61f4a411b6c3632 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Sun, 23 Apr 2017 12:46:48 +0200 Subject: Render comments when storing them in the database Instead of converting package comments from plain text to HTML code when they are displayed, do the conversion when the comment is posted and store the rendered result in the database. The conversion itself is done by a Python script which uses Bleach for sanitizing the text. Signed-off-by: Lukas Fleischer --- aurweb/scripts/rendercomment.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 aurweb/scripts/rendercomment.py (limited to 'aurweb') diff --git a/aurweb/scripts/rendercomment.py b/aurweb/scripts/rendercomment.py new file mode 100755 index 00000000..593cd36a --- /dev/null +++ b/aurweb/scripts/rendercomment.py @@ -0,0 +1,35 @@ +#!/usr/bin/python3 + +import sys +import bleach + +import aurweb.db + + +def get_comment(conn, commentid): + cur = conn.execute('SELECT Comments FROM PackageComments WHERE ID = ?', + [commentid]) + return cur.fetchone()[0] + + +def save_rendered_comment(conn, commentid, html): + conn.execute('UPDATE PackageComments SET RenderedComment = ? WHERE ID = ?', + [html, commentid]) + + +def main(): + commentid = int(sys.argv[1]) + + conn = aurweb.db.Connection() + + html = get_comment(conn, commentid) + html = html.replace('\n', '
') + html = bleach.clean(html, tags=['br']) + save_rendered_comment(conn, commentid, html) + + conn.commit() + conn.close() + + +if __name__ == '__main__': + main() -- cgit v1.2.3-24-g4f1b