Commit 406daa51 by Nimisha Asthagiri

Move get_grades management command from student to grades app.

parent ce1eb237
...@@ -19,12 +19,15 @@ import csv ...@@ -19,12 +19,15 @@ import csv
class RequestMock(RequestFactory): class RequestMock(RequestFactory):
"""
Class to create a mock request.
"""
def request(self, **request): def request(self, **request):
"Construct a generic request object." "Construct a generic request object."
request = RequestFactory.request(self, **request) request = RequestFactory.request(self, **request)
handler = BaseHandler() handler = BaseHandler()
handler.load_middleware() handler.load_middleware()
for middleware_method in handler._request_middleware: for middleware_method in handler._request_middleware: # pylint: disable=protected-access
if middleware_method(request): if middleware_method(request):
raise Exception("Couldn't create request mock object - " raise Exception("Couldn't create request mock object - "
"request middleware returned a response") "request middleware returned a response")
...@@ -32,6 +35,9 @@ class RequestMock(RequestFactory): ...@@ -32,6 +35,9 @@ class RequestMock(RequestFactory):
class Command(BaseCommand): class Command(BaseCommand):
"""
Management command for get_grades
"""
help = """ help = """
Generate a list of grades for all students Generate a list of grades for all students
...@@ -69,7 +75,7 @@ class Command(BaseCommand): ...@@ -69,7 +75,7 @@ class Command(BaseCommand):
raise CommandError("File {0} already exists".format( raise CommandError("File {0} already exists".format(
options['output'])) options['output']))
STATUS_INTERVAL = 100 status_interval = 100
# parse out the course into a coursekey # parse out the course into a coursekey
if options['course']: if options['course']:
...@@ -106,26 +112,28 @@ class Command(BaseCommand): ...@@ -106,26 +112,28 @@ class Command(BaseCommand):
print "Grading students" print "Grading students"
for count, student in enumerate(enrolled_students): for count, student in enumerate(enrolled_students):
count += 1 count += 1
if count % STATUS_INTERVAL == 0: if count % status_interval == 0:
# Print a status update with an approximation of # Print a status update with an approximation of
# how much time is left based on how long the last # how much time is left based on how long the last
# interval took # interval took
diff = datetime.datetime.now() - start diff = datetime.datetime.now() - start
timeleft = diff * (total - count) / STATUS_INTERVAL timeleft = diff * (total - count) / status_interval
hours, remainder = divmod(timeleft.seconds, 3600) hours, remainder = divmod(timeleft.seconds, 3600)
minutes, __ = divmod(remainder, 60) minutes, __ = divmod(remainder, 60)
print "{0}/{1} completed ~{2:02}:{3:02}m remaining".format( print "{0}/{1} completed ~{2:02}:{3:02}m remaining".format(
count, total, hours, minutes) count, total, hours, minutes)
start = datetime.datetime.now() start = datetime.datetime.now()
request.user = student request.user = student
grade = course_grades.summary(student, request, course) grade = course_grades.summary(student, course)
if not header: if not header:
header = [section['label'] for section in grade[u'section_breakdown']] header = [section['label'] for section in grade[u'section_breakdown']]
rows.append(["email", "username", "certificate-grade", "grade"] + header) rows.append(["email", "username", "certificate-grade", "grade"] + header)
percents = {section['label']: section['percent'] for section in grade[u'section_breakdown']} percents = {section['label']: section['percent'] for section in grade[u'section_breakdown']}
row_percents = [percents[label] for label in header] row_percents = [percents[label] for label in header]
if student.username in cert_grades: if student.username in cert_grades:
rows.append([student.email, student.username, cert_grades[student.username], grade['percent']] + row_percents) rows.append(
[student.email, student.username, cert_grades[student.username], grade['percent']] + row_percents,
)
else: else:
rows.append([student.email, student.username, "N/A", grade['percent']] + row_percents) rows.append([student.email, student.username, "N/A", grade['percent']] + row_percents)
with open(options['output'], 'wb') as f: with open(options['output'], 'wb') as f:
......
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