Commit 8fef8307 by Chris Dodge

optimize db round trips, by getting the course descriptor outside of the user loop

parent fdc8a6fe
...@@ -73,6 +73,9 @@ class Command(BaseCommand): ...@@ -73,6 +73,9 @@ class Command(BaseCommand):
ended_courses.append(course_id) ended_courses.append(course_id)
for course_id in ended_courses: for course_id in ended_courses:
# prefetch all chapters/sequentials by saying depth=2
course = modulestore().get_instance(course_id, CourseDescriptor.id_to_location(course_id), depth=2)
print "Fetching enrolled students for {0}".format(course_id) print "Fetching enrolled students for {0}".format(course_id)
enrolled_students = User.objects.filter( enrolled_students = User.objects.filter(
courseenrollment__course_id=course_id).prefetch_related( courseenrollment__course_id=course_id).prefetch_related(
...@@ -99,6 +102,6 @@ class Command(BaseCommand): ...@@ -99,6 +102,6 @@ class Command(BaseCommand):
student, course_id)['status'] in valid_statuses: student, course_id)['status'] in valid_statuses:
if not options['noop']: if not options['noop']:
# Add the certificate request to the queue # Add the certificate request to the queue
ret = xq.add_cert(student, course_id) ret = xq.add_cert(student, course_id, course=course)
if ret == 'generating': if ret == 'generating':
print '{0} - {1}'.format(student, ret) print '{0} - {1}'.format(student, ret)
...@@ -115,7 +115,7 @@ class XQueueCertInterface(object): ...@@ -115,7 +115,7 @@ class XQueueCertInterface(object):
raise NotImplementedError raise NotImplementedError
def add_cert(self, student, course_id): def add_cert(self, student, course_id, course=None):
""" """
Arguments: Arguments:
...@@ -151,9 +151,12 @@ class XQueueCertInterface(object): ...@@ -151,9 +151,12 @@ class XQueueCertInterface(object):
if cert_status in VALID_STATUSES: if cert_status in VALID_STATUSES:
# grade the student # grade the student
course = courses.get_course_by_id(course_id)
profile = UserProfile.objects.get(user=student)
# re-use the course passed in optionally so we don't have to re-fetch everything
# for every student
if course is None:
course = courses.get_course_by_id(course_id)
profile = UserProfile.objects.get(user=student)
cert, created = GeneratedCertificate.objects.get_or_create( cert, created = GeneratedCertificate.objects.get_or_create(
user=student, course_id=course_id) user=student, course_id=course_id)
......
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