From: Michael Tremer Date: Sun, 18 Nov 2012 17:59:51 +0000 (+0100) Subject: Rework Text UI module. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c764ce436f70d4598c5266bef5099046016c267a;p=pbs.git Rework Text UI module. Renders texts much faster and does not make so many database queries any more. --- diff --git a/backend/constants.py b/backend/constants.py index 84d76de9..54b549ab 100644 --- a/backend/constants.py +++ b/backend/constants.py @@ -16,6 +16,9 @@ UPLOADS_DIR = "/var/tmp/pakfire/uploads" BUFFER_SIZE = 1024 * 100 # 100kb +BUGZILLA_PATTERN = r"(bug\s?|#)(\d+)" +CVE_PATTERN = r"(CVE)(\s|\-)(\d{4}\-\d{4})" + N_ = lambda x: x MSG_BUILD_FAILED_SUBJECT = N_("[%(build_name)s] Build job failed.") diff --git a/web/ui_modules.py b/web/ui_modules.py index 98a3358e..9e373855 100644 --- a/web/ui_modules.py +++ b/web/ui_modules.py @@ -20,43 +20,63 @@ class UIModule(tornado.web.UIModule): class TextModule(UIModule): - def render(self, text, pre=False, remove_linebreaks=True): - link = """%s""" + __cache = {} - bz_url = self.settings.get("bugzilla_url", "") - bz_pattern = re.compile(r"(bug\s?|#)(\d+)") - bz_repl = link % (bz_url % { "bugid" : r"\2" }, r"\1\2") + LINK = """%s""" - cve_url = self.settings.get("cve_url", "") - cve_pattern = re.compile(r"(CVE)(\s|\-)(\d{4}\-\d{4})") - cve_repl = link % (cve_url % r"\3", r"\1\2\3") + @property + def bugzilla_url(self): + return self.settings.get("bugzilla_url", "") - if remove_linebreaks: - text = text.replace("\n", " ") + @property + def bugzilla_pattern(self): + if not self.__cache.has_key("bugzilla_pattern"): + self.__cache["bugzilla_pattern"] = re.compile(BUGZILLA_PATTERN) + + return self.__cache["bugzilla_pattern"] + + @property + def bugzilla_repl(self): + return self.LINK % (self.bugzilla_url % { "bugid" : r"\2" }, r"\1\2") + + @property + def cve_url(self): + return self.settings.get("cve_url", "") + + @property + def cve_pattern(self): + if not self.__cache.has_key("cve_pattern"): + self.__cache["cve_pattern"] = re.compile(CVE_PATTERN) + + return self.__cache["cve_pattern"] + + @property + def cve_repl(self): + return self.LINK % (self.cve_url % r"\3", r"\1\2\3") - o = [] - for p in text.splitlines(): - # Escape the text and create make urls clickable. - p = tornado.escape.xhtml_escape(p) - p = tornado.escape.linkify(p, shorten=True, - extra_params='target="_blank"') + def render(self, text, pre=False, remove_linebreaks=True): + link = """%s""" - # Search for bug ids that need to be linked to bugzilla. - if bz_url: - p = re.sub(bz_pattern, bz_repl, p, re.I|re.U) + if remove_linebreaks: + text = text.replace("\n", " ") - # Search for CVE numbers and create hyperlinks. - if cve_url: - p = re.sub(cve_pattern, cve_repl, p, re.I|re.U) + # Escape the text and create make urls clickable. + text = tornado.escape.xhtml_escape(text) + text = tornado.escape.linkify(text, shorten=True, + extra_params='target="_blank"') - o.append(p) + # Search for bug ids that need to be linked to bugzilla. + if self.bugzilla_url: + text = re.sub(self.bugzilla_pattern, self.bugzilla_repl, text, re.I|re.U) - o = "\n".join(o) + # Search for CVE numbers and create hyperlinks. + if self.cve_url: + text = re.sub(self.cve_pattern, self.cve_repl, text, re.I|re.U) if pre: - return "
%s
" % o + return "
%s
" % text - return textile.textile(o) + return textile.textile(text) class ModalModule(UIModule):