def post(self):
parser = reqparse.RequestParser()
parser.add_argument("problemID", type=str, required=True, location="json")
parser.add_argument("userID", type=str, required=True, location="json")
parser.add_argument("file", type=FileStorage, required=True, location="files")
entry = parser.parse_args()
try:
if db.problem.find_one({"_id": ObjectId(entry['problemID'])}) == None:
abort(400)
if db.user.find_one({"_id": entry['userID']}) == None:
abort(400)
except:
abort(400)
problemName = db.problem.find_one({"_id": ObjectId(entry['problemID'])})['name']
gradingFilePath = os.path.join(os.path.join(PROBLEMS_DIR, problemName.lower()), GRADING_SCRIPT)
command = "python3 "+gradingFilePath+" \""+entry["file"].stream+"\""
gradingOutput = subprocess.Popen(shlex.split(command.replace('\\','/')), stdout=subprocess.PIPE).communicate()[0]
structuredGradingOutput = json.loads(gradingOutput)
status_code = None
if "score" in structuredGradingOutput:
entry["score"] = structuredGradingOutput["score"]
entry.pop("file")
db.entry.insert_one(entry)
status_code = 201
else:
status_code = 400
return jsonify(structuredGradingOutput, status=status_code)
评论列表
文章目录