Commits

Anonymous committed 484d034

Ported [704] to stable

Comments (0)

Files changed (1)

         if id == -1:
             # If no particular report was requested, display
             # a list of available reports instead
-            cursor.execute("SELECT id AS report, title "
-                           "FROM report "
-                           "ORDER BY report")
             title = 'Available reports'
+            sql = 'SELECT id AS report, title FROM report ORDER BY report'
             description = 'This is a list of reports available.'
         else:
             cursor.execute('SELECT title, sql, description from report '
                            ' WHERE id=%s', id)
             row = cursor.fetchone()
-            try:
-                if not row:
-                    raise TracError('Report %d does not exist.' % id,
+            if not row:
+                raise TracError('Report %d does not exist.' % id,
                                 'Invalid Report Number')
-                title = row[0] or ''
-                sql   = self.sql_sub_vars(row[1], args)
-                if not sql:
-                    raise TracError('Report %s has no SQL query.' % id)
-                description = row[2] or ''
-                cursor.execute(sql)
+            title = row[0] or ''
+            sql = row[1]
+            description = row[2] or ''
 
-                if sql.find('__group__') == -1:
-                    self.req.hdf.setValue('report.sorting.enabled', '1')
-
-            except Exception, e:
-                self.error = e
-                self.req.hdf.setValue('report.message',
-                                      'report failed: %s' % e)
-                return None
-
-        # FIXME: fetchall should probably not be used.
-        info = cursor.fetchall()
-        cols = cursor.rs.col_defs
-        # Escape the values so that they are safe to have as html parameters
-#        info = map(lambda row: map(lambda x: escape(x), row), info)
-        return [cols, info, title, description]
+        return [title, description, sql]
         
     def create_report(self, title, description, sql):
         self.perm.assert_permission(perm.REPORT_CREATE)
         self.db.commit()
         self.req.redirect(self.env.href.report())
 
+    def execute_report(self, sql, args):
+        cursor = self.db.cursor()
+        sql = self.sql_sub_vars(sql, args)
+        if not sql:
+            raise TracError('Report %s has no SQL query.' % id)
+        cursor.execute(sql)
+
+        if sql.find('__group__') == -1:
+            self.req.hdf.setValue('report.sorting.enabled', '1')
+
+        # FIXME: fetchall should probably not be used.
+        info = cursor.fetchall()
+        cols = cursor.rs.col_defs
+        # Escape the values so that they are safe to have as html parameters
+        #info = map(lambda row: map(lambda x: escape(x), row), info)
+
+        return [cols, info]
+
     def commit_changes(self, id):
         """
         saves report changes to the database
         info = self.get_info(id, args)
         if not info:
             return
-        [self.cols, self.rows, title, description] = info
+        [title, description, sql] = info
         self.error = None
         
         self.req.hdf.setValue('title', title + ' (report)')
         descr_html = wiki_to_html(description, self.req.hdf, self.env)
         self.req.hdf.setValue('report.description', descr_html)
 
+        if self.args.get('format') == 'sql':
+            return
+
+        try:
+            [self.cols, self.rows] = self.execute_report(sql, args)
+        except Exception, e:
+            self.error = e
+            self.req.hdf.setValue('report.message',
+                                  'Report failed: %s' % e)
+            return None
+
         # Convert the header info to HDF-format
         idx = 0
         for col in self.cols:
         if self.args.has_key('sort'):
             sortCol = self.args.get('sort')
             colIndex = None
-            x = 0
-            while colIndex == None and x < len(self.cols):
-                if self.cols[x][0] == sortCol:
+            hiddenCols = 0
+            for x in range(len(self.cols)):
+                colName = self.cols[x][0]
+                if colName == sortCol:
                     colIndex = x
-                x = x + 1
+                if colName[:2] == '__' and colName[-2:] == '__':
+                    hiddenCols += 1
             if colIndex != None:
-                k = 'report.headers.%d.asc' % (colIndex-1)
+                k = 'report.headers.%d.asc' % (colIndex - hiddenCols)
                 asc = self.args.get('asc', None)
                 if asc:
                     sorter = ColumnSorter(colIndex, int(asc))