def getUserResults(req, courseId, assignmentId, username): """Get the results for a given username""" req.content_type = 'text/html' strout = websutil.OutputString() try: vmcfg = config.CourseConfig(CourseList().course_config(courseId)) except: traceback.print_exc(file = strout) return json.dumps({'errorType' : ERR_EXCEPTION, 'errorMessage' : "", 'errorTrace' : strout.get()}) vmpaths = paths.VmcheckerPaths(vmcfg.root_path()) submission_dir = vmpaths.dir_cur_submission_root(assignmentId, username) r_path = paths.dir_submission_results(submission_dir) strout = websutil.OutputString() try: result_files = [] if os.path.isdir(r_path): update_db.update_grades(courseId, user=username, assignment=assignmentId) for fname in os.listdir(r_path): # skill all files not ending in '.vmr' if not fname.endswith('.vmr'): continue f_path = os.path.join(r_path, fname) if os.path.isfile(f_path): overflow_msg = '' f_size = os.path.getsize(f_path) if f_size > MAX_VMR_FILE_SIZE: overflow_msg = '\n\n<b>File truncated! Actual size: ' + str(f_size) + ' bytes</b>\n' # decode as utf-8 and ignore any errors, because # characters will be badly encoded as json. with codecs.open(f_path, 'r', encoding='utf-8', errors='ignore') as f: result_files.append({fname : (f.read(MAX_VMR_FILE_SIZE) + overflow_msg) }) if len(result_files) == 0: msg = "In the meantime have a fortune cookie: <blockquote>" try: process = subprocess.Popen('/usr/games/fortune', shell=False, stdout=subprocess.PIPE) msg += process.communicate()[0] + "</blockquote>" except: msg += "Knock knock. Who's there? [Silence] </blockquote>" result_files = [ {'fortune.vmr' : msg } ] result_files.append({'queue-contents.vmr' : websutil.get_test_queue_contents(courseId) }) result_files.append({'late-submission.vmr' : websutil.submission_upload_info(courseId, username, assignmentId)}) result_files = websutil.sortResultFiles(result_files) return json.dumps(result_files) except: traceback.print_exc(file = strout) return json.dumps({'errorType' : ERR_EXCEPTION, 'errorMessage' : "", 'errorTrace' : strout.get()})
def getUserResults(req, courseId, assignmentId, username): """Get the results for a given username""" req.content_type = 'text/html' strout = websutil.OutputString() try: vmcfg = config.CourseConfig(CourseList().course_config(courseId)) except: traceback.print_exc(file = strout) return json.dumps({'errorType' : ERR_EXCEPTION, 'errorMessage' : "", 'errorTrace' : strout.get()}) vmpaths = paths.VmcheckerPaths(vmcfg.root_path()) submission_dir = vmpaths.dir_cur_submission_root(assignmentId, username) r_path = paths.dir_submission_results(submission_dir) strout = websutil.OutputString() try: result_files = [] if os.path.isdir(r_path): update_db.update_grades(courseId, user=username, assignment=assignmentId) for fname in os.listdir(r_path): # skill all files not ending in '.vmr' if not fname.endswith('.vmr'): continue f_path = os.path.join(r_path, fname) if os.path.isfile(f_path): overflow_msg = '' f_size = os.path.getsize(f_path) if f_size > MAX_VMR_FILE_SIZE: overflow_msg = '\n\n<b>File truncated! Actual size: ' + str(f_size) + ' bytes</b>\n' # decode as utf-8 and ignore any errors, because # characters will be badly encoded as json. with codecs.open(f_path, 'r', encoding='utf-8', errors='ignore') as f: result_files.append({fname : (f.read(MAX_VMR_FILE_SIZE) + overflow_msg) }) if len(result_files) == 0: msg = "In the meantime have a fortune cookie: <blockquote>" try: process = subprocess.Popen('/usr/games/fortune', shell=False, stdout=subprocess.PIPE) msg += process.communicate()[0] + "</blockquote>" except: msg += "Knock knock. Who's there? [Silence] </blockquote>" result_files = [ {'fortune.vmr' : msg } ] result_files.append({'queue-contents.vmr' : websutil.get_test_queue_contents(courseId) }) result_files.append({'late-submission.vmr' : websutil.submission_upload_info(courseId, username, assignmentId)}) result_files = websutil.sortResultFiles(result_files) return json.dumps(result_files) except: traceback.print_exc(file = strout) return json.dumps({'errorType' : ERR_EXCEPTION, 'errorMessage' : "", 'errorTrace' : strout.get()})
def getUserResults(courseId, assignmentId, username): strout = websutil.OutputString() try: vmcfg = config.CourseConfig(CourseList().course_config(courseId)) except: traceback.print_exc(file = strout) return json.dumps({'errorType' : ERR_EXCEPTION, 'errorMessage' : "", 'errorTrace' : strout.get()}) vmpaths = paths.VmcheckerPaths(vmcfg.root_path()) submission_dir = vmpaths.dir_cur_submission_root(assignmentId, username) r_path = paths.dir_submission_results(submission_dir) strout = websutil.OutputString() try: result_files = [] if os.path.isdir(r_path): # XXX should we narrow scope on user & assignment? # TODO: add a routine to update user+asignment+course grade only #update_db.update_grades(courseId) for fname in os.listdir(r_path): # skill all files not ending in '.vmr' if not fname.endswith('.vmr'): continue f_path = os.path.join(r_path, fname) if os.path.isfile(f_path): overflow_msg = '' f_size = os.path.getsize(f_path) if f_size > MAX_VMR_FILE_SIZE: overflow_msg = '\n\n<b>File truncated! Actual size: ' + str(f_size) + ' bytes</b>\n' # decode as utf-8 and ignore any errors, because # characters will be badly encoded as json. with codecs.open(f_path, 'r', encoding='utf-8', errors='ignore') as f: result_files.append({fname : (f.read(MAX_VMR_FILE_SIZE) + overflow_msg) }) if len(result_files) == 0: msg = "In the meantime have a fortune cookie" result_files = [ {'fortune.vmr' : msg } ] result_files.append({'queue-contents.vmr' : '' }) result_files.append({'late-submission.vmr' : websutil.submission_upload_info(courseId, username, assignmentId)}) result_files = websutil.sortResultFiles(result_files) return json.dumps(result_files) except: traceback.print_exc(file = strout) return json.dumps({'errorType' : ERR_EXCEPTION, 'errorMessage' : "", 'errorTrace' : strout.get()})
def getUserResults(req, courseId, assignmentId, username): """Get the results for a given username""" req.content_type = "text/html" strout = websutil.OutputString() try: vmcfg = config.CourseConfig(CourseList().course_config(courseId)) except: traceback.print_exc(file=strout) return json.dumps({"errorType": ERR_EXCEPTION, "errorMessage": "", "errorTrace": strout.get()}) vmpaths = paths.VmcheckerPaths(vmcfg.root_path()) submission_dir = vmpaths.dir_submission_root(assignmentId, username) r_path = paths.dir_submission_results(submission_dir) strout = websutil.OutputString() try: result_files = [] if os.path.isdir(r_path): update_db.update_all(courseId) for fname in os.listdir(r_path): # skill all files not ending in '.vmr' if not fname.endswith(".vmr"): continue f_path = os.path.join(r_path, fname) if os.path.isfile(f_path): with open(f_path, "r") as f: result_files.append({fname: f.read()}) if len(result_files) != 0: result_files = websutil.sortResultFiles(result_files) else: process = subprocess.Popen("/usr/games/fortune", shell=False, stdout=subprocess.PIPE) msg = "In the meantime have a fortune cookie: <blockquote>" msg += process.communicate()[0] + "</blockquote>" result_files = [{"Your results are not ready yet": msg}] return json.dumps(result_files) except: traceback.print_exc(file=strout) return json.dumps({"errorType": ERR_EXCEPTION, "errorMessage": "", "errorTrace": strout.get()})
def getResultsHelper(courseId, assignmentId, currentUser, strout, username = None, teamname = None, currentTeam = None): # assume that the session was already checked if username != None and teamname != None: return json.dumps({'errorType' : ERR_OTHER, 'errorMessage' : "Can't query both user and team results at the same time.", 'errorTrace' : ""}) try: vmcfg = StorerCourseConfig(CourseList().course_config(courseId)) except: traceback.print_exc(file = strout) return json.dumps({'errorType' : ERR_EXCEPTION, 'errorMessage' : "", 'errorTrace' : strout.get()}) # Check if the current user is allowed to view any other user's grade. # TODO: This should be implemented neater using some group # and permission model. is_authorized = vmcfg.public_results() or \ currentUser in vmcfg.admin_list() or \ username == currentUser or \ teamname == currentTeam if not is_authorized: traceback.print_exc(file = strout) return json.dumps({'errorType' : ERR_EXCEPTION, 'errorMessage' : "User is not authorized to view results.", 'errorTrace' : strout.get()}) vmpaths = paths.VmcheckerPaths(vmcfg.root_path()) account = None if username != None: # Get the individual results for this user account = username isTeamAccount = False elif teamname != None: # Get the team results for this team account = teamname isTeamAccount = True else: # Check if the user is part of a team with a mutual account for this submission (isTeamAccount, account) = getAssignmentAccountName(courseId, assignmentId, currentUser, strout) submission_dir = vmpaths.dir_cur_submission_root(assignmentId, account) r_path = paths.dir_submission_results(submission_dir) assignments = vmcfg.assignments() ignored_vmrs = assignments.ignored_vmrs(assignmentId) try: isGraded = False result_files = [] if os.path.isdir(r_path): update_db.update_grades(courseId, account=account, assignment=assignmentId) for fname in os.listdir(r_path): # skill all files not ending in '.vmr' if not fname.endswith('.vmr'): continue if fname in ignored_vmrs: continue f_path = os.path.join(r_path, fname) if os.path.isfile(f_path): overflow_msg = u"" f_size = os.path.getsize(f_path) if f_size > MAX_VMR_FILE_SIZE: overflow_msg = '\n\n' + _('File truncated! Actual size') + ': ' + str(f_size) + ' ' + _('bytes') + '\n' # decode as utf-8 and ignore any errors, because # characters will be badly encoded as json. with codecs.open(f_path, 'r', encoding='utf-8', errors='ignore') as f: content = f.read(MAX_VMR_FILE_SIZE) + overflow_msg.decode("utf-8") content = xssescape(content) result_files.append({fname : content}) if fname == 'grade.vmr' and \ "".join(content.split()) not in submissions.GENERATED_STATUSES: isGraded = True if (len(result_files) == 1 and result_files[0].keys()[0] == "grade.vmr") and \ not vmcfg.assignments().submit_only(assignmentId): msg = _("In the meantime have a fortune cookie") + ": <blockquote>" try: process = subprocess.Popen('/usr/games/fortune', shell=False, stdout=subprocess.PIPE) msg += process.communicate()[0] + "</blockquote>" except: msg += "Knock knock. Who's there? [Silence] </blockquote>" result_files = [ {'fortune.vmr' : msg } ] result_files.append({'queue-contents.vmr' : get_test_queue_contents(vmcfg, courseId) }) if 'submission.vmr' not in ignored_vmrs: result_files.append({'submission.vmr' : submission_upload_info(vmcfg, courseId, assignmentId, account, isTeamAccount, isGraded)}) result_files = sortResultFiles(result_files) return json.dumps(result_files) except: traceback.print_exc(file = strout) return json.dumps({'errorType' : ERR_EXCEPTION, 'errorMessage' : "", 'errorTrace' : strout.get()})
def getResultsHelper(courseId, assignmentId, currentUser, strout, username=None, teamname=None, currentTeam=None): # assume that the session was already checked if username != None and teamname != None: return json.dumps({ 'errorType': ERR_OTHER, 'errorMessage': "Can't query both user and team results at the same time.", 'errorTrace': "" }) try: vmcfg = StorerCourseConfig(CourseList().course_config(courseId)) except: traceback.print_exc(file=strout) return json.dumps({ 'errorType': ERR_EXCEPTION, 'errorMessage': "", 'errorTrace': strout.get() }) # Check if the current user is allowed to view any other user's grade. # TODO: This should be implemented neater using some group # and permission model. is_authorized = vmcfg.public_results() or \ currentUser in vmcfg.admin_list() or \ username == currentUser or \ teamname == currentTeam if not is_authorized: traceback.print_exc(file=strout) return json.dumps({ 'errorType': ERR_EXCEPTION, 'errorMessage': "User is not authorized to view results.", 'errorTrace': strout.get() }) vmpaths = paths.VmcheckerPaths(vmcfg.root_path()) account = None if username != None: # Get the individual results for this user account = username isTeamAccount = False elif teamname != None: # Get the team results for this team account = teamname isTeamAccount = True else: # Check if the user is part of a team with a mutual account for this submission (isTeamAccount, account) = getAssignmentAccountName(courseId, assignmentId, currentUser, strout) submission_dir = vmpaths.dir_cur_submission_root(assignmentId, account) r_path = paths.dir_submission_results(submission_dir) assignments = vmcfg.assignments() ignored_vmrs = assignments.ignored_vmrs(assignmentId) try: isGraded = False result_files = [] if os.path.isdir(r_path): update_db.update_grades(courseId, account=account, assignment=assignmentId) for fname in os.listdir(r_path): # skill all files not ending in '.vmr' if not fname.endswith('.vmr'): continue if fname in ignored_vmrs: continue f_path = os.path.join(r_path, fname) if os.path.isfile(f_path): overflow_msg = '' f_size = os.path.getsize(f_path) if f_size > MAX_VMR_FILE_SIZE: overflow_msg = '\n\n' + _( 'File truncated! Actual size') + ': ' + str( f_size) + ' ' + _('bytes') + '\n' # decode as utf-8 and ignore any errors, because # characters will be badly encoded as json. with codecs.open(f_path, 'r', encoding='utf-8', errors='ignore') as f: content = f.read(MAX_VMR_FILE_SIZE) + overflow_msg content = xssescape(content) result_files.append({fname: content}) if fname == 'grade.vmr' and \ "".join(content.split()) not in submissions.GENERATED_STATUSES: isGraded = True if (len(result_files) == 1 and result_files[0].keys()[0] == "grade.vmr") and \ not vmcfg.assignments().submit_only(assignmentId): msg = _("In the meantime have a fortune cookie") + ": <blockquote>" try: process = subprocess.Popen('/usr/games/fortune', shell=False, stdout=subprocess.PIPE) msg += process.communicate()[0] + "</blockquote>" except: msg += "Knock knock. Who's there? [Silence] </blockquote>" result_files = [{'fortune.vmr': msg}] result_files.append({ 'queue-contents.vmr': get_test_queue_contents(vmcfg, courseId) }) if 'submission.vmr' not in ignored_vmrs: result_files.append({ 'submission.vmr': submission_upload_info(vmcfg, courseId, assignmentId, account, isTeamAccount, isGraded) }) result_files = sortResultFiles(result_files) return json.dumps(result_files) except: traceback.print_exc(file=strout) return json.dumps({ 'errorType': ERR_EXCEPTION, 'errorMessage': "", 'errorTrace': strout.get() })