tasks.py 6.3 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
from django.conf import settings
23
from django.utils.translation import ugettext_noop
24
from celery import task
25
from functools import partial
26 27 28 29 30 31 32
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,
33
    push_grades_to_s3,
34
)
35
from bulk_email.tasks import perform_delegate_email_batches
36 37


38
@task(base=BaseInstructorTask)  # pylint: disable=E1102
39
def rescore_problem(entry_id, xmodule_instance_args):
40
    """Rescores a problem in a course, for all students or one specific student.
41 42

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
43 44 45 46
    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:
47 48

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

50 51
      'student': the identifier (username or email) of a particular user whose
          problem submission should be rescored.  If not specified, all problem
52
          submissions for the problem will be rescored.
53 54 55 56

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

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

65 66
    visit_fcn = partial(perform_module_state_update, update_fcn, filter_fcn)
    return run_main_task(entry_id, visit_fcn, action_name)
67 68


69
@task(base=BaseInstructorTask)  # pylint: disable=E1102
70
def reset_problem_attempts(entry_id, xmodule_instance_args):
71
    """Resets problem attempts to zero for a particular problem for all students in a course.
72 73

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
74 75 76 77
    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:
78 79 80 81 82 83

      '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.
    """
84 85
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('reset')
86 87 88
    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)
89 90


91
@task(base=BaseInstructorTask)  # pylint: disable=E1102
92
def delete_problem_state(entry_id, xmodule_instance_args):
93
    """Deletes problem state entirely for all students on a particular problem in a course.
94 95

    `entry_id` is the id value of the InstructorTask entry that corresponds to this task.
96 97 98 99
    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:
100 101 102 103 104 105

      '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.
    """
106 107
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('deleted')
108 109 110 111 112
    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)


113 114 115
@task(base=BaseInstructorTask)  # pylint: disable=E1102
def send_bulk_course_email(entry_id, _xmodule_instance_args):
    """Sends emails to recipients enrolled in a course.
116 117 118 119 120

    `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.

121
    The task_input should be a dict with the following entries:
122

123 124 125 126
      '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.
127
    """
128 129
    # Translators: This is a past-tense verb that is inserted into task progress messages as {action}.
    action_name = ugettext_noop('emailed')
130
    visit_fcn = perform_delegate_email_batches
131
    return run_main_task(entry_id, visit_fcn, action_name)
132 133 134 135 136 137 138 139 140


@task(base=BaseInstructorTask, routing_key=settings.GRADES_DOWNLOAD_ROUTING_KEY)  # pylint: disable=E1102
def calculate_grades_csv(entry_id, xmodule_instance_args):
    """
    Grade a course and push the results to an S3 bucket for download.
    """
    action_name = ugettext_noop('graded')
    task_fn = partial(push_grades_to_s3, xmodule_instance_args)
141
    return run_main_task(entry_id, task_fn, action_name)