views.py 1.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
# Create your views here.
import json
from datetime import datetime
from django.http import HttpResponse, Http404

def dictfetchall(cursor):
    '''Returns all rows from a cursor as a dict.
    Borrowed from Django documentation'''
    desc = cursor.description
    return [
        dict(zip([col[0] for col in desc], row))
        for row in cursor.fetchall()
    ]

def dashboard(request):
    """
Piotr Mitros committed
17 18 19
    Quick hack to show staff enrollment numbers.  This should be
    replaced with a real dashboard later. This version is a short-term
    bandaid for the next couple weeks. 
20 21 22 23
    """
    if not request.user.is_staff:
        raise Http404

24 25 26
    queries=[]
    queries.append("select count(user_id) as students, course_id from student_courseenrollment group by course_id order by students desc;")
    queries.append("select count(distinct user_id) as unique_students from student_courseenrollment;")
27 28
    queries.append("select registrations, count(registrations) from (select count(user_id) as registrations from student_courseenrollment group by user_id) as registrations_per_user group by registrations;")
    
29 30
    from django.db import connection
    cursor = connection.cursor()
31 32 33 34 35
    results =[]

    for query in queries:
        cursor.execute(query)
        results.append(dictfetchall(cursor))
36 37

    return HttpResponse(json.dumps(results, indent=4))