tasks.py 11.8 KB
Newer Older
1
"""
2
This file contains tasks that are designed to perform background operations on the
3 4
running state of a course.

Brian Wilson committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
At present, these tasks all operate on StudentModule objects in one way or another,
so they share a visitor architecture.  Each task defines an "update function" that
takes a module_descriptor, a particular StudentModule object, and xmodule_instance_args.

A task may optionally specify a "filter function" that takes a query for StudentModule
objects, and adds additional filter clauses.

A task also passes through "xmodule_instance_args", that are used to provide
information to our code that instantiates xmodule instances.

The task definition then calls the traversal function, passing in the three arguments
above, along with the id value for an InstructorTask object.  The InstructorTask
object contains a 'task_input' row which is a JSON-encoded dict containing
a problem URL and optionally a student.  These are used to set up the initial value
of the query for traversing StudentModule objects.

21
"""
22 23 24
import logging
from functools import partial

25
from django.conf import settings
26
from django.utils.translation import ugettext_noop
27

28
from celery import task
29
from bulk_email.tasks import perform_delegate_email_batches
30 31 32 33 34 35 36
from instructor_task.tasks_helper import (
    run_main_task,
    BaseInstructorTask,
    perform_module_state_update,
    rescore_problem_module_state,
    reset_attempts_module_state,
    delete_problem_module_state,
37
    upload_grades_csv,
38
    upload_problem_grade_report,
39
    upload_students_csv,
40
    cohort_students_and_upload,
41 42
    upload_enrollment_report,
    upload_may_enroll_csv,
43 44
    upload_exec_summary_report,
    generate_students_certificates,
45
    upload_proctored_exam_results_report
46
)
47 48 49


TASK_LOG = logging.getLogger('edx.celery.task')
50 51


52
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
53
def rescore_problem(entry_id, xmodule_instance_args):
54
    """Rescores a problem in a course, for all students or one specific student.
55 56

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
57 58 59 60
    The entry contains the `course_id` that identifies the course, as well as the
    `task_input`, which contains task-specific input.

    The task_input should be a dict with the following entries:
61 62

      'problem_url': the full URL to the problem to be rescored.  (required)
63

64 65
      'student': the identifier (username or email) of a particular user whose
          problem submission should be rescored.  If not specified, all problem
66
          submissions for the problem will be rescored.
67 68 69 70

    `xmodule_instance_args` provides information needed by _get_module_instance_for_task()
    to instantiate an xmodule instance.
    """
71 72
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('rescored')
73
    update_fcn = partial(rescore_problem_module_state, xmodule_instance_args)
74 75

    def filter_fcn(modules_to_update):
76
        """Filter that matches problems which are marked as being done"""
77 78
        return modules_to_update.filter(state__contains='"done": true')

79 80
    visit_fcn = partial(perform_module_state_update, update_fcn, filter_fcn)
    return run_main_task(entry_id, visit_fcn, action_name)
81 82


83
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
84
def reset_problem_attempts(entry_id, xmodule_instance_args):
85
    """Resets problem attempts to zero for a particular problem for all students in a course.
86 87

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
88 89 90 91
    The entry contains the `course_id` that identifies the course, as well as the
    `task_input`, which contains task-specific input.

    The task_input should be a dict with the following entries:
92 93 94 95 96 97

      'problem_url': the full URL to the problem to be rescored.  (required)

    `xmodule_instance_args` provides information needed by _get_module_instance_for_task()
    to instantiate an xmodule instance.
    """
98 99
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('reset')
100 101 102
    update_fcn = partial(reset_attempts_module_state, xmodule_instance_args)
    visit_fcn = partial(perform_module_state_update, update_fcn, None)
    return run_main_task(entry_id, visit_fcn, action_name)
103 104


105
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
106
def delete_problem_state(entry_id, xmodule_instance_args):
107
    """Deletes problem state entirely for all students on a particular problem in a course.
108 109

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
110 111 112 113
    The entry contains the `course_id` that identifies the course, as well as the
    `task_input`, which contains task-specific input.

    The task_input should be a dict with the following entries:
114 115 116 117 118 119

      'problem_url': the full URL to the problem to be rescored.  (required)

    `xmodule_instance_args` provides information needed by _get_module_instance_for_task()
    to instantiate an xmodule instance.
    """
120 121
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('deleted')
122 123 124 125 126
    update_fcn = partial(delete_problem_module_state, xmodule_instance_args)
    visit_fcn = partial(perform_module_state_update, update_fcn, None)
    return run_main_task(entry_id, visit_fcn, action_name)


127
@task(base=BaseInstructorTask)  # pylint: disable=not-callable
128 129
def send_bulk_course_email(entry_id, _xmodule_instance_args):
    """Sends emails to recipients enrolled in a course.
130 131 132 133 134

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
    The entry contains the `course_id` that identifies the course, as well as the
    `task_input`, which contains task-specific input.

135
    The task_input should be a dict with the following entries:
136

137 138 139 140
      'email_id': the full URL to the problem to be rescored.  (required)

    `_xmodule_instance_args` provides information needed by _get_module_instance_for_task()
    to instantiate an xmodule instance.  This is unused here.
141
    """
142 143
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('emailed')
144
    visit_fcn = perform_delegate_email_batches
145
    return run_main_task(entry_id, visit_fcn, action_name)
146 147


148
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
149 150 151 152
def calculate_grades_csv(entry_id, xmodule_instance_args):
    """
    Grade a course and push the results to an S3 bucket for download.
    """
153
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
154
    action_name = ugettext_noop('graded')
155 156 157 158 159
    TASK_LOG.info(
        u"Task: %s, InstructorTask ID: %s, Task type: %s, Preparing for task execution",
        xmodule_instance_args.get('task_id'), entry_id, action_name
    )

160
    task_fn = partial(upload_grades_csv, xmodule_instance_args)
161
    return run_main_task(entry_id, task_fn, action_name)
162 163


164 165 166
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
def calculate_problem_grade_report(entry_id, xmodule_instance_args):
    """
Daniel Friedman committed
167
    Generate a CSV for a course containing all students' problem
168 169
    grades and push the results to an S3 bucket for download.
    """
170 171
    # Translators: This is a past-tense phrase that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('problem distribution graded')
172 173 174 175 176 177 178 179 180
    TASK_LOG.info(
        u"Task: %s, InstructorTask ID: %s, Task type: %s, Preparing for task execution",
        xmodule_instance_args.get('task_id'), entry_id, action_name
    )

    task_fn = partial(upload_problem_grade_report, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


181
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
182 183 184 185 186 187 188
def calculate_students_features_csv(entry_id, xmodule_instance_args):
    """
    Compute student profile information for a course and upload the
    CSV to an S3 bucket for download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('generated')
189
    task_fn = partial(upload_students_csv, xmodule_instance_args)
190
    return run_main_task(entry_id, task_fn, action_name)
191 192


193 194 195 196 197 198 199 200 201 202 203 204
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
def enrollment_report_features_csv(entry_id, xmodule_instance_args):
    """
    Compute student profile information for a course and upload the
    CSV to an S3 bucket for download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('generating_enrollment_report')
    task_fn = partial(upload_enrollment_report, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


205
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
Afzal Wali committed
206 207 208 209 210 211 212 213 214 215 216 217
def exec_summary_report_csv(entry_id, xmodule_instance_args):
    """
    Compute executive summary report for a course and upload the
    Html generated report to an S3 bucket for download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = 'generating_exec_summary_report'
    task_fn = partial(upload_exec_summary_report, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
218 219 220 221 222 223 224 225 226 227 228
def proctored_exam_results_csv(entry_id, xmodule_instance_args):
    """
    Compute proctored exam results report for a course and upload the
    CSV for download.
    """
    action_name = 'generating_proctored_exam_results_report'
    task_fn = partial(upload_proctored_exam_results_report, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
229 230 231 232 233 234 235 236 237 238 239 240
def calculate_may_enroll_csv(entry_id, xmodule_instance_args):
    """
    Compute information about invited students who have not enrolled
    in a given course yet and upload the CSV to an S3 bucket for
    download.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('generated')
    task_fn = partial(upload_may_enroll_csv, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=not-callable
def generate_certificates(entry_id, xmodule_instance_args):
    """
    Grade students and generate certificates.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('certificates generated')
    TASK_LOG.info(
        u"Task: %s, InstructorTask ID: %s, Task type: %s, Preparing for task execution",
        xmodule_instance_args.get('task_id'), entry_id, action_name
    )

    task_fn = partial(generate_students_certificates, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)


257 258 259 260 261 262 263 264 265 266
@task(base=BaseInstructorTask)  # pylint: disable=E1102
def cohort_students(entry_id, xmodule_instance_args):
    """
    Cohort students in bulk, and upload the results.
    """
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    # An example of such a message is: "Progress: {action} {succeeded} of {attempted} so far"
    action_name = ugettext_noop('cohorted')
    task_fn = partial(cohort_students_and_upload, xmodule_instance_args)
    return run_main_task(entry_id, task_fn, action_name)