Commit 355d4be7 by David Ormsbee

Make it so that CSV-cert generation passes back file-like objs instead of strings.

parent 12a599b0
...@@ -125,7 +125,7 @@ class XQueueCertInterface(object): ...@@ -125,7 +125,7 @@ class XQueueCertInterface(object):
raise NotImplementedError raise NotImplementedError
def add_cert(self, student, course_id, course=None, forced_grade=None, template_file=None, title='None'): def add_cert(self, student, course_id, course=None, forced_grade=None, template_file=None, title='None', forced_percentage_grade=None):
""" """
Request a new certificate for a student. Request a new certificate for a student.
...@@ -135,7 +135,9 @@ class XQueueCertInterface(object): ...@@ -135,7 +135,9 @@ class XQueueCertInterface(object):
forced_grade - a string indicating a grade parameter to pass with forced_grade - a string indicating a grade parameter to pass with
the certificate request. If this is given, grading the certificate request. If this is given, grading
will be skipped. will be skipped.
forced_percentage_grade - Value between 0 and 1 that is the overall
percentage grade that the student got for
this course.
Will change the certificate status to 'generating'. Will change the certificate status to 'generating'.
Certificate must be in the 'unavailable', 'error', Certificate must be in the 'unavailable', 'error',
...@@ -179,7 +181,13 @@ class XQueueCertInterface(object): ...@@ -179,7 +181,13 @@ class XQueueCertInterface(object):
course_name = course.display_name or course_id.to_deprecated_string() course_name = course.display_name or course_id.to_deprecated_string()
is_whitelisted = self.whitelist.filter(user=student, course_id=course_id, whitelist=True).exists() is_whitelisted = self.whitelist.filter(user=student, course_id=course_id, whitelist=True).exists()
grade = grades.grade(student, self.request, course) if forced_percentage_grade is None:
grade = grades.grade(student, self.request, course)
else:
grade = dict(
percent=forced_percentage_grade,
grade=grades.grade_for_percentage(course.grade_cutoffs, forced_percentage_grade)
)
enrollment_mode, __ = CourseEnrollment.enrollment_mode_for_user(student, course_id) enrollment_mode, __ = CourseEnrollment.enrollment_mode_for_user(student, course_id)
mode_is_verified = (enrollment_mode == GeneratedCertificate.MODES.verified) mode_is_verified = (enrollment_mode == GeneratedCertificate.MODES.verified)
user_is_verified = SoftwareSecurePhotoVerification.user_is_verified(student) user_is_verified = SoftwareSecurePhotoVerification.user_is_verified(student)
......
...@@ -86,6 +86,8 @@ from opaque_keys.edx.keys import CourseKey ...@@ -86,6 +86,8 @@ from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locations import SlashSeparatedCourseKey from opaque_keys.edx.locations import SlashSeparatedCourseKey
from opaque_keys import InvalidKeyError from opaque_keys import InvalidKeyError
from student.models import UserProfile, Registration from student.models import UserProfile, Registration
from certificates.queue import XQueueCertInterface
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -1599,8 +1601,16 @@ def calculate_grades_csv(request, course_id): ...@@ -1599,8 +1601,16 @@ def calculate_grades_csv(request, course_id):
def _generate_certificates_from_grades_csv_task(course_id, filename): def _generate_certificates_from_grades_csv_task(course_id, filename):
"""Assume this will be run async later.""" """Assume this will be run async later."""
report_store = ReportStore.from_config() report_store = ReportStore.from_config()
grades_csv = report_store.get_as_string(CourseKey.from_string(course_id), filename) course_key = CourseKey.from_string(course_id)
print grades_csv grades_csv = report_store.get(course_key, filename)
course = get_course_by_id(course_key, depth=2)
csv_reader = csv.DictReader(grades_csv)
xq = XQueueCertInterface()
for row in csv_reader:
student = User.objects.get(username=row['username'])
percentage_grade = float(row['grade'])
xq.add_cert(student, course_key, course=course, forced_percentage_grade=percentage_grade)
@ensure_csrf_cookie @ensure_csrf_cookie
......
...@@ -274,9 +274,12 @@ class S3ReportStore(ReportStore): ...@@ -274,9 +274,12 @@ class S3ReportStore(ReportStore):
return key return key
def get_as_string(self, course_id, filename): def get(self, course_id, filename):
key = self.key_for(course_id, filename) key = self.key_for(course_id, filename)
return key.get_contents_as_string() buff = StringIO()
key.get_contents_to_file(buff)
buff.seek(0)
return GzipFile(fileobj=buff)
def store(self, course_id, filename, buff): def store(self, course_id, filename, buff):
""" """
...@@ -377,10 +380,10 @@ class LocalFSReportStore(ReportStore): ...@@ -377,10 +380,10 @@ class LocalFSReportStore(ReportStore):
"""Return the full path to a given file for a given course.""" """Return the full path to a given file for a given course."""
return os.path.join(self.root_path, urllib.quote(course_id.to_deprecated_string(), safe=''), filename) return os.path.join(self.root_path, urllib.quote(course_id.to_deprecated_string(), safe=''), filename)
def get_as_string(self, course_id, filename): def get(self, course_id, filename):
full_path = self.path_to(course_id, filename) full_path = self.path_to(course_id, filename)
with open(full_path, "rb") as report_file: with open(full_path, "rb") as report_file:
return report_file.read() return StringIO(report_file.read())
def store(self, course_id, filename, buff): def store(self, course_id, filename, buff):
""" """
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment