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

import logging
6
import json
7 8

from django.http import HttpResponse
9
from opaque_keys.edx.locations import SlashSeparatedCourseKey
10 11 12 13

from courseware.courses import get_course_with_access
from courseware.access import has_access
from class_dashboard import dashboard_data
14

15 16 17 18 19 20 21 22 23

log = logging.getLogger(__name__)


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

24 25
    course = get_course_with_access(user, 'staff', course_id, depth=None)
    return has_access(user, 'staff', course)
26 27 28 29 30 31 32 33 34 35 36 37 38


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
    """

39
    data = {}
40 41

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

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


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
    """
65
    data = {}
66 67

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

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


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.
    """
96
    data = {}
97 98

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

109
    return HttpResponse(json.dumps(data), mimetype="application/json")