def addTestFile(pid): try: p = Problem.objects.get(id=pid) c, a = p.getParents() #For security purposes we send anyone who isnt an instructor or #admin away if not c in current_user.courseInstructor: abort(403) if request.method == "POST": form = AddTestForm(request.form) testFileParsers = getTestFileParsers() form.testType.choices = [(x, x) for x in testFileParsers.keys()] if form.validate(): filepath = getTestPath(c, a, p) if not os.path.isdir(filepath): os.makedirs(filepath) filename = secure_filename( request.files[form.testFile.name].filename) if os.path.isfile(os.path.join(filepath, filename)): flash("This file already exists") return redirect( url_for('instructorProblemSettings', pid=p.id)) request.files[form.testFile.name].save( os.path.join(filepath, filename)) # Add the file to the problem p.testfiles.append(filename) p.save() #Create json spec file gradeSpec = {} gradeSpec['file'] = filename gradeSpec['type'] = form.testType.data gradeSpec['tests'] = testFileParsers[form.testType.data]( os.path.join(filepath, filename)) gradeSpec['sections'] = [] filename += ".json" with open(os.path.join(filepath, filename), 'w') as f: json.dump(gradeSpec, f) flash("Added test file") else: flash("Form didn't validate", "error") return redirect(url_for('instructorProblemSettings', pid=p.id)) except (Course.DoesNotExist, Problem.DoesNotExist, AssignmentGroup.DoesNotExist): abort(404)
def addTestFile(pid): try: p = Problem.objects.get(id=pid) c,a = p.getParents() #For security purposes we send anyone who isnt an instructor or #admin away if not c in current_user.courseInstructor: abort(403) if request.method == "POST": form = AddTestForm(request.form) testFileParsers = getTestFileParsers() form.testType.choices = [(x,x) for x in testFileParsers.keys()] if form.validate(): filepath = getTestPath(c, a, p) if not os.path.isdir(filepath): os.makedirs(filepath) filename = secure_filename(request.files[form.testFile.name].filename) if os.path.isfile(os.path.join(filepath, filename)): flash("This file already exists") return redirect(url_for('instructorProblemSettings', pid=p.id)) request.files[form.testFile.name].save(os.path.join(filepath, filename)) # Add the file to the problem p.testfiles.append(filename) p.save() #Create json spec file gradeSpec = {} gradeSpec['file'] = filename gradeSpec['type'] = form.testType.data gradeSpec['tests'] = testFileParsers[form.testType.data](os.path.join(filepath, filename)) gradeSpec['sections'] = [] filename += ".json" with open(os.path.join(filepath, filename), 'w') as f: json.dump(gradeSpec, f) flash("Added test file") else: flash("Form didn't validate", "error") return redirect(url_for('instructorProblemSettings', pid=p.id)) except (Course.DoesNotExist, Problem.DoesNotExist, AssignmentGroup.DoesNotExist): abort(404)
def instructorReuploadTestFile(pid, filename): try: p = Problem.objects.get(id=pid) c,a = p.getParents() #For security purposes we send anyone who isnt an instructor or #admin away if not c in current_user.courseInstructor: abort(403) filepath = getTestPath(c, a, p) filepath = os.path.join(filepath, filename) gradeSpec = getTestData(filepath) parser = getTestFileParsers()[gradeSpec['type']] if request.method == "POST": form = ReuploadTestForm(request.form) if form.validate(): filename = secure_filename(request.files[form.testFile.name].filename) if filename != gradeSpec['file']: flash("Uploaded file does not have the same name as the existing file. Reupload failed.", "warning") return redirect(url_for('instructorEditTestFile', pid=pid, filename=gradeSpec['file'])) request.files[form.testFile.name].save(filepath) tests = parser(filepath) #Filter out removed tests for sec in gradeSpec['sections']: sec['tests'] = [x for x in sec['tests'] if x in tests] gradeSpec['tests'] = tests with open(filepath+".json", 'w') as f: json.dump(gradeSpec, f) flash("File successfully reuploaded", "success") return redirect(url_for('instructorEditTestFile', pid=pid, filename=filename)) except (Course.DoesNotExist, Problem.DoesNotExist, AssignmentGroup.DoesNotExist): abort(404)
def instructorProblemSettings(pid): ''' Function Type: View Function Template: instructor/problemSettings.html Purpose: Allows an instructor to edit the settings for a problem in a course. Inputs: pid: THe object ID of the problem to be modified Template Parameters: course: The course object for this problem assignment: The assignment group object for this problem problem: This problesm object form: A form for the options of this problem testForm: A form for creating new tests. testFiles: A list of filenames for existing tests ''' try: p = Problem.objects.get(id=pid) c, a = p.getParents() #For security purposes we send anyone who isnt an instructor or #admin away if not c in current_user.courseInstructor: abort(403) testFiles = [] filepath = getTestPath(c, a, p) for f in p.testfiles: testFiles.append(getTestInfo(os.path.join(filepath, f))) testForm = AddTestForm() testFileParsers = getTestFileParsers() testForm.testType.choices = sorted([(x, x) for x in testFileParsers.keys()]) return render_template("instructor/problemSettings.html", course=c, problem=p, assignment=a,\ form=ProblemOptionsForm(), testForm=testForm,\ testFiles=testFiles) except (Problem.DoesNotExist, Course.DoesNotExist, AssignmentGroup.DoesNotExist): abort(404)
def instructorProblemSettings(pid): ''' Function Type: View Function Template: instructor/problemSettings.html Purpose: Allows an instructor to edit the settings for a problem in a course. Inputs: pid: THe object ID of the problem to be modified Template Parameters: course: The course object for this problem assignment: The assignment group object for this problem problem: This problesm object form: A form for the options of this problem testForm: A form for creating new tests. testFiles: A list of filenames for existing tests ''' try: p = Problem.objects.get(id=pid) c,a = p.getParents() #For security purposes we send anyone who isnt an instructor or #admin away if not c in current_user.courseInstructor: abort(403) testFiles = [] filepath = getTestPath(c, a, p) for f in p.testfiles: testFiles.append(getTestInfo(os.path.join(filepath,f))) testForm = AddTestForm() testFileParsers = getTestFileParsers() testForm.testType.choices = sorted([(x,x) for x in testFileParsers.keys()]) return render_template("instructor/problemSettings.html", course=c, problem=p, assignment=a,\ form=ProblemOptionsForm(), testForm=testForm,\ testFiles=testFiles) except (Problem.DoesNotExist, Course.DoesNotExist, AssignmentGroup.DoesNotExist): abort(404)
printSet(extraTests) print "\n" print "Parsing test Finished for file: %s\n\n" % (testFile) if __name__ == "__main__": if len(sys.argv) < 3: print USAGE sys.exit(1) print "\n\n" #Get the file print "Getting plugin" fileParser = getTestFileParsers()[sys.argv[1]] testFolder = sys.argv[2] testFolder = abspath(testFolder) #Each test case is contained in a folder which has the name of the test file. #We look into each directory in turn and use the test #file and the testfile.json file to determine if everything is correct #any supplemental files can be included in these folders and will be isolated #from the rest of the files for d in listdir(testFolder): #Skip non dir if not isdir(join(testFolder, d)): continue
print "Parser found the following extra tests: " printSet(extraTests) print "\n" print "Parsing test Finished for file: %s\n\n" % (testFile) if __name__ == "__main__": if len(sys.argv) < 3: print USAGE sys.exit(1) print "\n\n" #Get the file print "Getting plugin" fileParser = getTestFileParsers()[sys.argv[1]] testFolder = sys.argv[2] testFolder = abspath(testFolder) #Each test case is contained in a folder which has the name of the test file. #We look into each directory in turn and use the test #file and the testfile.json file to determine if everything is correct #any supplemental files can be included in these folders and will be isolated #from the rest of the files for d in listdir(testFolder): #Skip non dir if not isdir(join(testFolder, d)): continue