From 94db91fceff1edcd65b39f1f9bbfb1cc1bd25982 Mon Sep 17 00:00:00 2001 From: John Hess Date: Mon, 4 Mar 2013 18:19:24 -0500 Subject: [PATCH] Cast cursor responses as lists. MySQL returns them as tuples. Removed print statemetns Moved import call to top of file --- lms/djangoapps/dashboard/views.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/lms/djangoapps/dashboard/views.py b/lms/djangoapps/dashboard/views.py index e74d462432..266e769db5 100644 --- a/lms/djangoapps/dashboard/views.py +++ b/lms/djangoapps/dashboard/views.py @@ -3,6 +3,7 @@ import json from datetime import datetime from django.http import Http404 from mitxmako.shortcuts import render_to_response +from django.db import connection from student.models import CourseEnrollment, CourseEnrollmentAllowed from django.contrib.auth.models import User @@ -12,16 +13,18 @@ def dictfetchall(cursor): '''Returns a list of all rows from a cursor as a column: result dict. Borrowed from Django documentation''' desc = cursor.description - table=[] + table = [] table.append([col[0] for col in desc]) - table = table + cursor.fetchall() - print "Table: " + str(table) + + # ensure response from db is a list, not a tuple (which is returned + # by MySQL backed django instances) + rows_from_cursor=cursor.fetchall() + table = table + [list(row) for row in rows_from_cursor] return table def SQL_query_to_list(cursor, query_string): cursor.execute(query_string) raw_result=dictfetchall(cursor) - print raw_result return raw_result def dashboard(request): @@ -50,7 +53,6 @@ def dashboard(request): results["scalars"]["Total Enrollments Across All Courses"]=CourseEnrollment.objects.count() # establish a direct connection to the database (for executing raw SQL) - from django.db import connection cursor = connection.cursor() # define the queries that will generate our user-facing tables