Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lms/djangoapps/instructor_task/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,39 +344,39 @@ def submit_calculate_problem_responses_csv(
return submit_task(request, task_type, task_class, course_key, task_input, task_key)


def submit_calculate_grades_csv(request, course_key):
def submit_calculate_grades_csv(request, course_key, **task_kwargs):
"""
AlreadyRunningError is raised if the course's grades are already being updated.
"""
task_type = 'grade_course'
task_class = calculate_grades_csv
task_input = {}
task_input = task_kwargs
task_key = ""

return submit_task(request, task_type, task_class, course_key, task_input, task_key)


def submit_problem_grade_report(request, course_key):
def submit_problem_grade_report(request, course_key, **task_kwargs):
"""
Submits a task to generate a CSV grade report containing problem
values.
"""
task_type = 'grade_problems'
task_class = calculate_problem_grade_report
task_input = {}
task_input = task_kwargs
task_key = ""
return submit_task(request, task_type, task_class, course_key, task_input, task_key)


def submit_calculate_students_features_csv(request, course_key, features):
def submit_calculate_students_features_csv(request, course_key, features, **task_kwargs):
"""
Submits a task to generate a CSV containing student profile info.

Raises AlreadyRunningError if said CSV is already being updated.
"""
task_type = 'profile_info_csv'
task_class = calculate_students_features_csv
task_input = features
task_input = dict(features=features, **task_kwargs)
task_key = ""

return submit_task(request, task_type, task_class, course_key, task_input, task_key)
Expand Down
15 changes: 8 additions & 7 deletions lms/djangoapps/instructor_task/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ def from_config(cls, config_name):
getattr(settings, config_name).get('STORAGE_KWARGS'),
)

def store(self, course_id, filename, buff):
def store(self, course_id, filename, buff, parent_dir=''):
"""
Store the contents of `buff` in a directory determined by hashing
`course_id`, and name the file `filename`. `buff` can be any file-like
object, ready to be read from the beginning.
"""
path = self.path_to(course_id, filename)
path = self.path_to(course_id, filename, parent_dir)
# See https://github.com/boto/boto/issues/2868
# Boto doesn't play nice with unicode in python3
if not six.PY2:
Expand All @@ -291,7 +291,7 @@ def store(self, course_id, filename, buff):

self.storage.save(path, buff)

def store_rows(self, course_id, filename, rows):
def store_rows(self, course_id, filename, rows, parent_dir=''):
"""
Given a course_id, filename, and rows (each row is an iterable of
strings), write the rows to the storage backend in csv format.
Expand All @@ -303,7 +303,7 @@ def store_rows(self, course_id, filename, rows):
csvwriter = csv.writer(output_buffer)
csvwriter.writerows(self._get_utf8_encoded_rows(rows))
output_buffer.seek(0)
self.store(course_id, filename, output_buffer)
self.store(course_id, filename, output_buffer, parent_dir)

def links_for(self, course_id):
"""
Expand Down Expand Up @@ -333,9 +333,10 @@ def links_for(self, course_id):
for filename, full_path in files
]

def path_to(self, course_id, filename=''):
def path_to(self, course_id, filename='', parent_dir=''):
"""
Return the full path to a given file for a given course.
"""
hashed_course_id = hashlib.sha1(text_type(course_id).encode('utf-8')).hexdigest()
return os.path.join(hashed_course_id, filename)
hashed_course_id = hashlib.sha1(str(course_id).encode('utf-8')).hexdigest()
directory = parent_dir if bool(parent_dir) else hashed_course_id
return os.path.join(directory, filename)
6 changes: 4 additions & 2 deletions lms/djangoapps/instructor_task/tasks_helper/enrollments.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def upload_students_csv(_xmodule_instance_args, _entry_id, course_id, task_input
task_progress.update_task_state(extra_meta=current_step)

# compute the student features table and format it
query_features = task_input
query_features = task_input.get('features')
student_data = enrolled_students_features(course_id, query_features)
header, rows = format_dictlist(student_data, query_features)

Expand All @@ -87,6 +87,8 @@ def upload_students_csv(_xmodule_instance_args, _entry_id, course_id, task_input
task_progress.update_task_state(extra_meta=current_step)

# Perform the upload
upload_csv_to_report_store(rows, 'student_profile_info', course_id, start_date)
upload_parent_dir = task_input.get('upload_parent_dir', '')
upload_filename = task_input.get('filename', 'student_profile_info')
upload_csv_to_report_store(rows, upload_filename, course_id, start_date, parent_dir=upload_parent_dir)

return task_progress.update_task_state(extra_meta=current_step)
26 changes: 20 additions & 6 deletions lms/djangoapps/instructor_task/tasks_helper/grades.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ def _upload(self, context, success_rows, error_rows):
Creates and uploads a CSV for the given headers and rows.
"""
date = datetime.now(UTC)
upload_csv_to_report_store(success_rows, context.file_name, context.course_id, date)
upload_csv_to_report_store(success_rows, context.upload_filename, context.course_id, date)
if len(error_rows) > 1:
upload_csv_to_report_store(error_rows, context.file_name + '_err', context.course_id, date)
upload_csv_to_report_store(error_rows, context.upload_filename + '_err', context.course_id, date)

def log_additional_info_for_testing(self, context, message):
"""
Expand Down Expand Up @@ -224,6 +224,8 @@ def __init__(self, _xmodule_instance_args, _entry_id, course_id, _task_input, ac
self.course_id = course_id
self.task_progress = TaskProgress(self.action_name, total=None, start_time=time())
self.report_for_verified_only = course_grade_report_verified_only(self.course_id)
self.upload_parent_dir = _task_input.get('upload_parent_dir', '')
self.upload_filename = _task_input.get('filename', 'grade_report')

@lazy
def course(self):
Expand Down Expand Up @@ -317,7 +319,8 @@ def __init__(self, _xmodule_instance_args, _entry_id, course_id, _task_input, ac
self.course_id = course_id
self.report_for_verified_only = problem_grade_report_verified_only(self.course_id)
self.task_progress = TaskProgress(self.action_name, total=None, start_time=time())
self.file_name = 'problem_grade_report'
self.upload_filename = _task_input.get('filename', 'problem_grade_report')
self.upload_dir = _task_input.get('upload_parent_dir', '')

@lazy
def course(self):
Expand Down Expand Up @@ -485,10 +488,21 @@ def _upload(self, context, success_headers, success_rows, error_headers, error_r
Creates and uploads a CSV for the given headers and rows.
"""
date = datetime.now(UTC)
upload_csv_to_report_store([success_headers] + success_rows, 'grade_report', context.course_id, date)
upload_csv_to_report_store(
[success_headers] + success_rows,
context.upload_filename,
context.course_id,
date,
parent_dir=context.upload_parent_dir
)
if len(error_rows) > 0:
error_rows = [error_headers] + error_rows
upload_csv_to_report_store(error_rows, 'grade_report_err', context.course_id, date)
upload_csv_to_report_store(
[error_headers] + error_rows,
'{}_err'.format(context.upload_filename),
context.course_id,
date,
parent_dir=context.upload_parent_dir
)

def _grades_header(self, context):
"""
Expand Down
5 changes: 3 additions & 2 deletions lms/djangoapps/instructor_task/tasks_helper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
UPDATE_STATUS_SKIPPED = 'skipped'


def upload_csv_to_report_store(rows, csv_name, course_id, timestamp, config_name='GRADES_DOWNLOAD'):
def upload_csv_to_report_store(rows, csv_name, course_id, timestamp, config_name='GRADES_DOWNLOAD', parent_dir=''):
"""
Upload data as a CSV using ReportStore.

Expand All @@ -32,6 +32,7 @@ def upload_csv_to_report_store(rows, csv_name, course_id, timestamp, config_name
]
csv_name: Name of the resulting CSV
course_id: ID of the course
parent_dor: Name of the directory where the CSV file will be stored

Returns:
report_name: string - Name of the generated report
Expand All @@ -43,7 +44,7 @@ def upload_csv_to_report_store(rows, csv_name, course_id, timestamp, config_name
timestamp_str=timestamp.strftime("%Y-%m-%d-%H%M")
)

report_store.store_rows(course_id, report_name, rows)
report_store.store_rows(course_id, report_name, rows, parent_dir)
tracker_emit(csv_name)
return report_name

Expand Down
4 changes: 2 additions & 2 deletions lms/djangoapps/instructor_task/tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ def test_both_groups_problems(self):
self.submit_student_answer(self.student_b.username, problem_b_url, [OPTION_1, OPTION_2])

with patch('lms.djangoapps.instructor_task.tasks_helper.runner._get_current_task'):
result = CourseGradeReport.generate(None, None, self.course.id, None, 'graded')
result = CourseGradeReport.generate(None, None, self.course.id, {}, 'graded')
self.verify_csv_task_success(result)
self.verify_grades_in_csv(
[
Expand Down Expand Up @@ -671,7 +671,7 @@ def test_one_group_problem(self):
self.submit_student_answer(self.student_a.username, problem_a_url, [OPTION_1, OPTION_1])

with patch('lms.djangoapps.instructor_task.tasks_helper.runner._get_current_task'):
result = CourseGradeReport.generate(None, None, self.course.id, None, 'graded')
result = CourseGradeReport.generate(None, None, self.course.id, {}, 'graded')
self.verify_csv_task_success(result)
self.verify_grades_in_csv(
[
Expand Down
Loading