Commits

Tetsuya Morimoto committed e092ed8

fixed to enable the ticket referenct link in the preview page
refactoring ugly code

Comments (0)

Files changed (1)

ticketref/web_ui.py

 _, add_domain = domain_functions("ticketref", ("_", "add_domain"))
 
 TEMPLATE_FILES = [
-    "report_view.html", "query_results.html", "ticket.html", "query.html",
+    "query.html",
+    "query_results.html",
+    "report_view.html",
+    "ticket.html",
+    "ticket_preview.html",
 ]
 
 COPY_TICKET_FIELDS = [
-    "type", "priority", "milestone", "component", "version", "keywords",
-    "owner", "cc",
+    "cc",
+    "component",
+    "keywords",
+    "milestone",
+    "owner",
+    "priority",
+    "type",
+    "version",
 ]
 
 
     """ Extend template for ticket cross-reference """
 
     implements(IRequestFilter, ITemplateProvider, ITemplateStreamFilter)
+    _empty_list = []
 
     def __init__(self):
         add_domain(self.env.path, resource_filename(__name__, "locale"))
 
     # ITemplateStreamFilter methods
     def filter_stream(self, req, method, filename, stream, data):
-        if not data or (not filename in TEMPLATE_FILES):
+        if not (data and filename in TEMPLATE_FILES):
             return stream
 
-        # For ticket.html
-        if "fields" in data and isinstance(data["fields"], list):
+        if filename.startswith("ticket"):
             self._filter_fields(req, data)
 
-        # For query_results.html and query.html
-        if "groups" in data and isinstance(data["groups"], list):
+        if filename.startswith("query"):
             self._filter_groups(req, data)
 
-        # For report_view.html
-        if "row_groups" in data and isinstance(data["row_groups"], list):
+        if filename == "report_view.html":
             self._filter_row_groups(req, data)
 
         return stream
 
     def _filter_fields(self, req, data):
-        for field in data["fields"]:
+        for field in data.get("fields", self._empty_list):
             if field["name"] == TREF:
                 field["label"] = _("Relationships")
                 ticket = data["ticket"]
                     field["rendered"] = tag([new])
 
     def _filter_groups(self, req, data):
-        fields_tref = data["fields"].get(TREF)
+        fields_tref = data.get("fields", {}).get(TREF)
         if fields_tref:  # column checkbox/select option
             fields_tref["label"] = _("Relationships")
             if fields_tref["type"] == u"textarea":
                 if isinstance(data.get("all_columns"), list):
                     data["all_columns"].append(TREF)
 
-        for header in data["headers"]:  # list view header
+        # list view header
+        for header in data.get("headers", self._empty_list):
             if header["name"] == TREF:
                 header["label"] = _("Relationships")
 
-        for group, tickets in data["groups"]:
+        for group, tickets in data.get("groups", self._empty_list):
             for ticket in tickets:
                 if TREF in ticket:
-                    if TREF in data["row"]:
+                    if TREF in data.get("row"):
                         ticket[TREF] = self._link_textarea(req, ticket[TREF])
                     else:  # expect TREF in data["col"]
                         ticket[TREF] = self._link_refs(req, ticket[TREF])
 
     def _filter_row_groups(self, req, data):
-        for headers in data["header_groups"]:
+        for headers in data.get("header_groups", self._empty_list):
             for header in headers:
                 if header["col"] == TREF:
                     header["title"] = _("Relationships")
-        for group, rows in data["row_groups"]:
+
+        for group, rows in data.get("row_groups", self._empty_list):
             for row in rows:
                 _is_list = isinstance(row["cell_groups"], list)
                 if "cell_groups" in row and _is_list:
 
     def _link_new(self, req, ticket, field):
         param = {TREF: ticket.id}
-        for field in COPY_TICKET_FIELDS:
-            if ticket[field]:
-                param[field] = ticket[field]
+        param.update(dict([(i, ticket[i]) for i in COPY_TICKET_FIELDS
+                                          if ticket[i]]))
         attr = {
             "class_": "tref-link",
             "target": "_blank",