def _rerun_test_harness(assignment): try: # Get assignment assn = Assignment.objects.get(id = ObjectId(assignment)) if not assn.test_harness: logger.info("Attempting to rerun test harnesses on assignment " "with no test harnesses") return # Form the submissions query query = { "assignment": ObjectId(assignment), "most_recent": True } # Grab the most recent submissions from each user. submissions = list(Submission.objects(**query)) if not submissions: logger.info("No submissions found for this assignment.") return # Send a bunch of test requests to shepherd to be rerun. for i in submissions: i.test_request_timestamp = datetime.datetime.now() i.save() logger.info("Sent test request to shepherd for %s" % str(i.id)) send_test_request(shepherd_config["PUBLIC_SOCKET"], i.id) time.sleep(30) except Exception as e: logger.error(str(e)) raise
def resubmit_submission(assignment_id, submission_id): # Figure out which assignment the submission belongs to. try: assignment_id = ObjectId(assignment_id) assignment = Assignment.objects.get(id=assignment_id) except (InvalidId, Assignment.DoesNotExist) as e: logger.info("Could not retrieve assignment: %s", str(e)) abort(404) # Figure out where we should redirect the user to once we're done. redirect_to = request.args.get("next") or request.referrer if not is_url_on_site(app, redirect_to): # Default going back to the assignment screen redirect_to = url_for("view_assignment", assignment_id=assignment_id) # Recheck if the assignment's cutoff date has passed. today = datetime.datetime.today() deadline = current_user.personal_deadline if ( assignment.due_cutoff and assignment.due_cutoff < today and (str(assignment_id) not in deadline or deadline[str(assignment_id)] < datetime.datetime.today) ): logger.info("Submission rejected, cutoff date has already passed.") return craft_response(error="The cutoff date has already passed, your submission was " "not accepted.") try: submission_id = ObjectId(submission_id) submission = Submission.objects.get(id=submission_id) except (InvalidId, Submission.DoesNotExist) as e: logger.info("Could not retrieve submission: %s", str(e)) abort(404) # Recheck that this assignment has a test harness before signaling shepherd. if assignment.test_harness: submission.test_request_timestamp = datetime.datetime.now() send_test_request(config["PUBLIC_SOCKET"], submission.id) logger.info("Resending test request to shepherd for %s" % str(submission.id)) # If the test result failed, remove it from submissions and database if submission.test_results: result = TestResult.objects.get(id=submission.test_results) if result.failed: result.delete() submission.test_results = None # Save new reqeust timestamp in submission. submission.save() flash("Successfully resubmitted files.", category="message") return redirect(redirect_to)
def resubmit_submission(assignment_id, submission_id): # Figure out which assignment the submission belongs to. try: assignment_id = ObjectId(assignment_id) assignment = Assignment.objects.get(id = assignment_id) except (InvalidId, Assignment.DoesNotExist) as e: logger.info("Could not retrieve assignment: %s", str(e)) abort(404) # Figure out where we should redirect the user to once we're done. redirect_to = request.args.get("next") or request.referrer if not is_url_on_site(app, redirect_to): # Default going back to the assignment screen redirect_to = url_for( "view_assignment", assignment_id = assignment_id ) # Recheck if the assignment's cutoff date has passed. today = datetime.datetime.today() deadline = current_user.personal_deadline if assignment.due_cutoff and \ assignment.due_cutoff < today and \ (str(assignment_id) not in deadline or deadline[str(assignment_id)] < datetime.datetime.today): logger.info("Submission rejected, cutoff date has already passed.") return craft_response( error = "The cutoff date has already passed, your submission was " "not accepted." ) try: submission_id = ObjectId(submission_id) submission = Submission.objects.get(id = submission_id) except (InvalidId, Submission.DoesNotExist) as e: logger.info("Could not retrieve submission: %s", str(e)) abort(404) # Recheck that this assignment has a test harness before signaling shepherd. if (assignment.test_harness): submission.test_request_timestamp = datetime.datetime.now() send_test_request(config["PUBLIC_SOCKET"], submission.id) logger.info("Resending test request to shepherd for %s" \ % str(submission.id)) # If the test result failed, remove it from submissions and database if submission.test_results: result = TestResult.objects.get(id = submission.test_results) if result.failed: result.delete() submission.test_results = None # Save new reqeust timestamp in submission. submission.save() flash("Successfully resubmitted files.", category = "message") return redirect(redirect_to)
def upload_submission(assignment_id): # Figure out which assignment the user asked for. try: id = ObjectId(assignment_id) assignment = Assignment.objects.get(id = id) except (InvalidId, Assignment.DoesNotExist) as e: logger.info("Could not retrieve assignment: %s", str(e)) abort(404) # Figure out where we should redirect the user to once we're done. redirect_to = request.args.get("next") or request.referrer if not is_url_on_site(app, redirect_to): # Default going back to the assignment screen redirect_to = url_for( "view_assignment", assignment_id = assignment_id ) assignment.apply_personal_deadlines(current_user) # Check if the assignment's cutoff date has passed if assignment.due_cutoff and \ assignment.due_cutoff < datetime.datetime.today(): logger.info("Submission rejected, cutoff date has already passed.") flash( "The cutoff date has already passed, your submission was not " "accepted.", category = "error" ) return redirect(redirect_to) form = SimpleArchiveForm() if not form.validate_on_submit(): logger.info( "Submission rejected due to internal validation problem." ) flash( "Submission rejected due to internal validation problem. Try " "again.", category = "error" ) return redirect(redirect_to) if not [i for i in form.archive.entries if i.data.filename]: logger.info("Submission rejected. User did not submit any files.") flash("You did not submit any files.", category = "error") return redirect(redirect_to) new_submission = Submission( assignment = id, user = current_user.id, timestamp = datetime.datetime.now(), test_type = "final" if form.marked_as_final.data else "public", most_recent = True ) new_submission.id = ObjectId() logger.info(str(new_submission.to_dict())) # Craft a unique directory path where we will store the new submission. We # are guarenteed an ObjectId is unique. However we are not guarenteed that # we will have the proper permissions and that we will be able to make the # directory thus this could error because of that. new_submission.testables = new_submission.getFilePath() os.makedirs(new_submission.testables) # Save each file the user uploaded into the submissions directory for i in form.archive.entries: if not i.data.filename: continue # Figure out where we want to save the user's file file_path = os.path.join( new_submission.testables, secure_filename(i.data.filename) ) # Do the actual saving i.data.save(file_path) new_submission.uploaded_filenames.extend( secure_filename(i.data.filename) for i in form.archive.entries if i.data.filename ) logger.info( "Succesfully uploaded a new submission (id = %s) with files %s.", str(new_submission.id), str(new_submission.uploaded_filenames) ) # The old "most_recent" submission is no longer the most recent. Submission.objects( user = current_user.email, assignment = id, most_recent = True ).update( multi = False, unset__most_recent = 1 ) if assignment.test_harness: new_submission.test_request_timestamp = datetime.datetime.now() logger.info("Sent test request to shepherd for %s" % \ str(new_submission.id)) new_submission.save() # Tell shepherd to start running tests if there is a test_harness. if assignment.test_harness: send_test_request(config["PUBLIC_SOCKET"], new_submission.id) # Communicate to the next page what submission was just added. flash(str(new_submission.id), category = "new_submission") flash( "Successfully uploaded %s %s." % ( plural_if("file", len(new_submission.uploaded_filenames)), pretty_list(new_submission.uploaded_filenames) ), category = "message" ) # Everything seems to have gone well return redirect(redirect_to)
def upload_submission(assignment_id): # Figure out which assignment the user asked for. try: id = ObjectId(assignment_id) assignment = Assignment.objects.get(id=id) except (InvalidId, Assignment.DoesNotExist) as e: logger.info("Could not retrieve assignment: %s", str(e)) abort(404) # Figure out where we should redirect the user to once we're done. redirect_to = request.args.get("next") or request.referrer if not is_url_on_site(app, redirect_to): # Default going back to the assignment screen redirect_to = url_for("view_assignment", assignment_id=assignment_id) assignment.apply_personal_deadlines(current_user) # Check if the assignment's cutoff date has passed if assignment.due_cutoff and \ assignment.due_cutoff < datetime.datetime.today(): logger.info("Submission rejected, cutoff date has already passed.") flash( "The cutoff date has already passed, your submission was not " "accepted.", category="error") return redirect(redirect_to) form = SimpleArchiveForm() if not form.validate_on_submit(): logger.info("Submission rejected due to internal validation problem.") flash( "Submission rejected due to internal validation problem. Try " "again.", category="error") return redirect(redirect_to) if not [i for i in form.archive.entries if i.data.filename]: logger.info("Submission rejected. User did not submit any files.") flash("You did not submit any files.", category="error") return redirect(redirect_to) new_submission = Submission( assignment=id, user=current_user.id, timestamp=datetime.datetime.now(), test_type="final" if form.marked_as_final.data else "public", most_recent=True) new_submission.id = ObjectId() logger.info(str(new_submission.to_dict())) # Craft a unique directory path where we will store the new submission. We # are guarenteed an ObjectId is unique. However we are not guarenteed that # we will have the proper permissions and that we will be able to make the # directory thus this could error because of that. new_submission.testables = new_submission.getFilePath() os.makedirs(new_submission.testables) # Save each file the user uploaded into the submissions directory for i in form.archive.entries: if not i.data.filename: continue # Figure out where we want to save the user's file file_path = os.path.join(new_submission.testables, secure_filename(i.data.filename)) # Do the actual saving i.data.save(file_path) new_submission.uploaded_filenames.extend( secure_filename(i.data.filename) for i in form.archive.entries if i.data.filename) logger.info( "Succesfully uploaded a new submission (id = %s) with files %s.", str(new_submission.id), str(new_submission.uploaded_filenames)) # The old "most_recent" submission is no longer the most recent. Submission.objects(user=current_user.email, assignment=id, most_recent=True).update(multi=False, unset__most_recent=1) if assignment.test_harness: new_submission.test_request_timestamp = datetime.datetime.now() logger.info("Sent test request to shepherd for %s" % \ str(new_submission.id)) new_submission.save() # Tell shepherd to start running tests if there is a test_harness. if assignment.test_harness: send_test_request(config["PUBLIC_SOCKET"], new_submission.id) # Communicate to the next page what submission was just added. flash(str(new_submission.id), category="new_submission") flash("Successfully uploaded %s %s." % (plural_if("file", len(new_submission.uploaded_filenames)), pretty_list(new_submission.uploaded_filenames)), category="message") # Everything seems to have gone well return redirect(redirect_to)