Add almost everything necessary for UI support for including test attributes in table view.

This was almost straightforward.  The one roadblock came with grouping support.  I didn't bother including that for iteration results, but I did try to make it work here.  Grouping is fine; unfortunately, grouping implies drilldown, and drilldown implies arbitrary filtering on test attributes, which requires a bit more sophisticated handling of which attribute fields we're requesting in each query (it's not just the ones we're viewing, but also any fields used in filtering).  This isn't a terribly hard problem to solve, but doing it right will require some design changes, notably separating the fields in the view from the fields used in actual query, which will probably require making HeaderFields immutable to be done properly.  I need to verify that this feature is high-enough priority to justify getting into all that, but I'd like to check in what I've got so far so I don't lose it.

To be clear, this doesn't actually add any user-visible changes yet.

Signed-off-by: Steve Howard <showard@google.com>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@3866 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/new_tko/tko/models.py b/new_tko/tko/models.py
index 8d0cfb1..0884f8f 100644
--- a/new_tko/tko/models.py
+++ b/new_tko/tko/models.py
@@ -15,7 +15,7 @@
         field_names = []
         for field in fields:
             if field in extra_select_fields:
-                field_names.append(field)
+                field_names.append(extra_select_fields[field][0])
             else:
                 field_names.append(self._get_key_unless_is_function(field))
         return field_names
@@ -74,16 +74,21 @@
 
 
     def _get_num_groups_sql(self, query, group_by):
-        group_fields = self._get_field_names(group_by)
+        group_fields = self._get_field_names(group_by, query.query.extra_select)
         query = query.order_by() # this can mess up the query and isn't needed
 
         sql, params = query.query.as_sql()
         from_ = sql[sql.find(' FROM'):]
-        return ('SELECT COUNT(DISTINCT %s) %s' % (','.join(group_fields),
+        return ('SELECT DISTINCT %s %s' % (','.join(group_fields),
                                                   from_),
                 params)
 
 
+    def _cursor_rowcount(self, cursor):
+        """To be stubbed by tests"""
+        return cursor.rowcount
+
+
     def get_num_groups(self, query, group_by):
         """
         Returns the number of distinct groups for the given query grouped by the
@@ -92,7 +97,7 @@
         sql, params = self._get_num_groups_sql(query, group_by)
         cursor = readonly_connection.connection().cursor()
         cursor.execute(sql, params)
-        return cursor.fetchone()[0]
+        return self._cursor_rowcount(cursor)
 
 
 class Machine(dbmodels.Model):