views.py 3.68 KB
Newer Older
1 2 3 4
"""
Handles requests for data, returning a json
"""

5
import json
6
import logging
7 8

from django.http import HttpResponse
9
from opaque_keys.edx.keys import CourseKey
10 11

from class_dashboard import dashboard_data
12 13
from courseware.access import has_access
from courseware.courses import get_course_overview_with_access
14 15 16 17 18 19 20 21 22

log = logging.getLogger(__name__)


def has_instructor_access_for_class(user, course_id):
    """
    Returns true if the `user` is an instructor for the course.
    """

23
    course = get_course_overview_with_access(user, 'staff', course_id)
24
    return bool(has_access(user, 'staff', course))
25 26 27 28 29 30 31 32 33 34 35 36 37


def all_sequential_open_distrib(request, course_id):
    """
    Creates a json with the open distribution for all the subsections in the course.

    `request` django request

    `course_id` the course ID for the course interested in

    Returns the format in dashboard_data.get_d3_sequential_open_distrib
    """

38
    data = {}
39 40

    # Only instructor for this particular course can request this information
41
    course_key = CourseKey.from_string(course_id)
42
    if has_instructor_access_for_class(request.user, course_key):
43
        try:
44
            data = dashboard_data.get_d3_sequential_open_distrib(course_key)
45 46
        except Exception as ex:  # pylint: disable=broad-except
            log.error('Generating metrics failed with exception: %s', ex)
47
            data = {'error': "error"}
48
    else:
49
        data = {'error': "Access Denied: User does not have access to this course's data"}
50

51
    return HttpResponse(json.dumps(data), content_type="application/json")
52 53 54 55 56 57 58 59 60 61 62 63


def all_problem_grade_distribution(request, course_id):
    """
    Creates a json with the grade distribution for all the problems in the course.

    `Request` django request

    `course_id` the course ID for the course interested in

    Returns the format in dashboard_data.get_d3_problem_grade_distrib
    """
64
    data = {}
65 66

    # Only instructor for this particular course can request this information
67
    course_key = CourseKey.from_string(course_id)
68
    if has_instructor_access_for_class(request.user, course_key):
69
        try:
70
            data = dashboard_data.get_d3_problem_grade_distrib(course_key)
71 72
        except Exception as ex:  # pylint: disable=broad-except
            log.error('Generating metrics failed with exception: %s', ex)
73
            data = {'error': "error"}
74
    else:
75
        data = {'error': "Access Denied: User does not have access to this course's data"}
76

77
    return HttpResponse(json.dumps(data), content_type="application/json")
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94


def section_problem_grade_distrib(request, course_id, section):
    """
    Creates a json with the grade distribution for the problems in the specified section.

    `request` django request

    `course_id` the course ID for the course interested in

    `section` The zero-based index of the section for the course

    Returns the format in dashboard_data.get_d3_section_grade_distrib

    If this is requested multiple times quickly for the same course, it is better to call all_problem_grade_distribution
    and pick out the sections of interest.
    """
95
    data = {}
96 97

    # Only instructor for this particular course can request this information
98
    course_key = CourseKey.from_string(course_id)
99
    if has_instructor_access_for_class(request.user, course_key):
100
        try:
101
            data = dashboard_data.get_d3_section_grade_distrib(course_key, section)
102 103
        except Exception as ex:  # pylint: disable=broad-except
            log.error('Generating metrics failed with exception: %s', ex)
104
            data = {'error': "error"}
105
    else:
106
        data = {'error': "Access Denied: User does not have access to this course's data"}
107

108
    return HttpResponse(json.dumps(data), content_type="application/json")