]> git.ipfire.org Git - people/jschlag/pbs.git/commitdiff
Rework Text UI module.
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 18 Nov 2012 17:59:51 +0000 (18:59 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 18 Nov 2012 17:59:51 +0000 (18:59 +0100)
Renders texts much faster and does not make so
many database queries any more.

backend/constants.py
web/ui_modules.py

index 84d76de9da41f3fc7a988cd4b9554c0f184a188e..54b549ab2ff51e058c3c2eee3bac8a1305988190 100644 (file)
@@ -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.")
index 98a3358e3a7d117a501e072bf2bdcd0ccf3216a3..9e3738555312122d25993154737efe227eeb0c23 100644 (file)
@@ -20,43 +20,63 @@ class UIModule(tornado.web.UIModule):
 
 
 class TextModule(UIModule):
-       def render(self, text, pre=False, remove_linebreaks=True):
-               link = """<a href="%s" target="_blank">%s</a>"""
+       __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 = """<a href="%s" target="_blank">%s</a>"""
 
-               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 = """<a href="%s" target="_blank">%s</a>"""
 
-                       # 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 "<pre>%s</pre>" % o
+                       return "<pre>%s</pre>" % text
 
-               return textile.textile(o)
+               return textile.textile(text)
 
 
 class ModalModule(UIModule):