views.py 3.17 KB
Newer Older
1 2 3
# Create your views here.
import json
from datetime import datetime
John Hess committed
4 5
from django.http import Http404
from mitxmako.shortcuts import render_to_response
6
from django.db import connection
John Hess committed
7 8 9

from student.models import CourseEnrollment, CourseEnrollmentAllowed
from django.contrib.auth.models import User
10

Calen Pennington committed
11

12
def dictfetchall(cursor):
John Hess committed
13
    '''Returns a list of all rows from a cursor as a column: result dict.
14 15
    Borrowed from Django documentation'''
    desc = cursor.description
16
    table = []
John Hess committed
17
    table.append([col[0] for col in desc])
18 19 20 21 22
    
    # 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]
John Hess committed
23 24 25 26 27 28
    return table

def SQL_query_to_list(cursor, query_string):
    cursor.execute(query_string)
    raw_result=dictfetchall(cursor)
    return raw_result
29 30 31

def dashboard(request):
    """
John Hess committed
32 33 34 35 36 37
    Slightly less hackish hack to show staff enrollment numbers and other
    simple queries.  

    All queries here should be indexed and simple.  Mostly, this means don't
    touch courseware_studentmodule, as tempting as it may be.

38 39 40 41
    """
    if not request.user.is_staff:
        raise Http404

John Hess committed
42 43 44 45 46
    # results are passed to the template.  The template knows how to render
    # two types of results: scalars and tables.  Scalars should be represented
    # as "Visible Title": Value and tables should be lists of lists where each
    # inner list represents a single row of the table
    results = {"scalars":{},"tables":{}}
Calen Pennington committed
47

John Hess committed
48 49 50 51 52 53 54 55
    # count how many users we have
    results["scalars"]["Unique Usernames"]=User.objects.filter().count()
    results["scalars"]["Activated Usernames"]=User.objects.filter(is_active=1).count()
    
    # count how many enrollments we have
    results["scalars"]["Total Enrollments Across All Courses"]=CourseEnrollment.objects.count()

    # establish a direct connection to the database (for executing raw SQL)
56
    cursor = connection.cursor()
57

John Hess committed
58 59 60 61
    # define the queries that will generate our user-facing tables
    # table queries need not take the form of raw SQL, but do in this case since
    # the MySQL backend for django isn't very friendly with group by or distinct
    table_queries = {}
62 63 64 65 66 67 68 69 70 71 72 73 74 75
    table_queries["course enrollments"]= \
        "select "+ \
        "course_id as Course, "+ \
        "count(user_id) as Students " + \
        "from student_courseenrollment "+ \
        "group by course_id "+ \
        "order by students desc;"
    table_queries["number of students in each number of classes"]= \
        "select registrations as 'Registered for __ Classes' , "+ \
        "count(registrations) as Users "+ \
        "from (select count(user_id) as registrations "+ \
               "from student_courseenrollment "+ \
               "group by user_id) as registrations_per_user "+ \
        "group by registrations;"
John Hess committed
76 77 78 79 80 81 82

    # add the result for each of the table_queries to the results object
    for query in table_queries.keys():
        cursor.execute(table_queries[query])
        results["tables"][query] = SQL_query_to_list(cursor, table_queries[query])

    context={"results":results}
83

John Hess committed
84
    return render_to_response("admin_dashboard.html",context)