def delete_image(): if 'question' in request.form and 'file_name' in request.form: del_query = database_session.query(QuestionsImages.file_name).filter( and_(QuestionsImages.question == request.form['question'], QuestionsImages.file_name == request.form['file_name'])) del_query.delete() database_session.commit() return "success"
def delete_answer(): if 'question' in request.form and 'answer' in request.form: del_query = database_session.query(Questions.answer).filter( and_(Questions.question == request.form['question'], Questions.answer == request.form['answer'])) del_query.delete() database_session.commit() return "success"
def edit_answer(): if 'question' in request.form and 'answer' in request.form and 'new_answer' in request.form: rows_to_update = database_session.query(Questions).filter( Questions.question == request.form['question'], Questions.answer == request.form['answer']) for row in rows_to_update: row.answer = request.form['new_answer'] database_session.commit() return redirect(url_for('admin.admin_edit_questions'))
def index(): '''The index for the login page It logs users into the web application and redirects them to appropriate user page''' if current_user.is_authenticated: if current_user.user_type == 'Supervisor': return redirect(url_for('admin.admin_index')) elif current_user.user_type == 'Grader': return redirect(url_for('grader.grader_index')) elif current_user.user_type == 'Student': return redirect(url_for('student_team.student_team_index')) else: # The user type is some value we don't define here so this is some sort of server error abort(500) if request.method == 'GET': return render_template('logon.html') elif request.method == 'POST': # Check the form to make sure it has the fields we are looking for if 'Username' in request.form and 'Password' in request.form: user = Users.query.filter( Users.username == request.form['Username']).first() # If the Username is not in the database if user is None: flash('Invalid Username', 'error') return redirect(url_for('index')) else: if user.password == 'Deactivated': flash('Account deactivated', 'error') return redirect(url_for('index')) elif check_password_hash(user.password, request.form['Password']): login_user(user) if current_user.user_type == 'Supervisor': return redirect(url_for('admin.admin_index')) elif current_user.user_type == 'Grader': return redirect(url_for('grader.grader_index')) elif current_user.user_type == 'Student': current_user.password = '******' database_session.commit() return redirect( url_for('student_team.student_team_index')) else: # The user type is some value we don't define here so this is some sort of server error abort(500) else: # If the user did not use the correct password flash('Invalid Password', 'error') return redirect(url_for('index')) else: # If the form did not contain a username or password submission return render_template('logon.html')
def test_remove_question(): if ('testId' in request.form) and ('question' in request.form): question = database_session.query(iComputeTest).filter_by( test_name=request.form['testId'], question=request.form['question']).first() if question: database_session.delete(question) database_session.commit() return 'success' return 'error'
def edit_question(): if 'team_name' in request.form and 'team_year' in request.form and 'section' in request.form and 'score' in request.form: if request.form['section'] == 'section_three': rows_to_update = database_session.query(StudentScore).filter( StudentScore.team_name == request.form['team_name'], StudentScore.team_year == request.form['team_year']) for row in rows_to_update: row.section_three_score = request.form['score'] row.total_score = ( row.section_one_score + (row.section_two_score if row.section_two_score else 0) + int(row.section_three_score)) database_session.commit() return "success"
def add_image(): if ('inputFile' in request.files) and ('hiddenfield_id' in request.form): file = request.files['inputFile'] myQuestion = request.form['hiddenfield_id'] if not QuestionsImages.query.filter_by(file_name=file.filename, question=myQuestion).first(): questionImage = [ QuestionsImages(question=myQuestion, file_name=file.filename, data=file.read()), ] database_session.add_all(questionImage) database_session.commit() return redirect(url_for('admin.admin_edit_questions'))
def student_s3(): student_team = StudentTeam.query.filter_by(team_name=current_user.username).order_by(StudentTeam.team_year).first() exam_questions_s3 = [] images=[] question = {} fileData = {} counter = 0 fileCounter = 0 # Build Dictionary for questions pulled from the db # TODO grab the correct exam for this team! for counter, exam_question_s3 in enumerate(database_session.query(iComputeTest.question).filter(and_(iComputeTest.test_name == student_team.test_id, iComputeTest.section == 3)).order_by(iComputeTest.orderId), start=1): question['id'] = counter question['question'] = exam_question_s3.question for data in database_session.query(QuestionsImages.data).filter(QuestionsImages.question == exam_question_s3.question): fileData = {} fileData['file_id'] = fileCounter fileData['file_counter'] = counter image = base64.encodestring(data.data) image2 = image.decode("UTF8") fileData['image'] = image2 images.append(fileData) fileCounter +=1 exam_questions_s3.append(question) counter += 1 question = {} if request.method == 'POST': if ('inputFile' in request.files) and ('hiddenfield_id' in request.form): file = request.files['inputFile'] myQuestion = request.form['hiddenfield_id'] database_session.add(StudentAnswer( team_name=current_user.username, team_year=student_team.team_year, section=3, # TODO since the question was escaped we may need to unescape it here question=myQuestion, answer=file.filename, scratch_file=file.read())) database_session.commit() current_user.password = '******' database_session.commit() logout_user() flash('Your answers have been submitted. Thank you for participating!', 'info') return redirect(url_for('index')) return render_template('scratch_submit.html', exam_questions_s3 = exam_questions_s3, images = images)
def add_answer(): if 'question' in request.form and 'answer' in request.form: question_text = request.form['question'] answer_text = request.form['answer'] # Check to see if the answer is already associated with this question if Questions.query.filter_by(question=question_text, answer=answer_text).count() > 0: return "error" new_answer = Questions(question=question_text, answer=answer_text, is_correct=False, section=1) database_session.add(new_answer) database_session.commit() return 'success'
def edit_question(): if 'question' in request.form and 'new_question' in request.form: rows_to_update = database_session.query(Questions).filter( Questions.question == request.form['question']) test_rows_to_update = database_session.query(iComputeTest).filter( iComputeTest.question == request.form['question']) for row in rows_to_update: row.question = request.form['new_question'] for row in test_rows_to_update: row.question = request.form['new_question'] database_session.commit() rows_to_update = database_session.query(QuestionsImages).filter( QuestionsImages.question == request.form['question']) for row in rows_to_update: row.question = request.form['new_question'] database_session.commit() return "success"
def grade_section_one(team): student_team = StudentTeam.query.filter_by(team_name=team).order_by(StudentTeam.team_year).first() total_questions = database_session.query(iComputeTest).filter(iComputeTest.test_name==student_team.test_id).count() correct_answers = 0 for response in StudentAnswer.query.filter_by(team_name=team).all(): question = Questions.query.filter_by(question=response.question, is_correct=True).first() if question: if response.answer == question.answer: correct_answers += 1 student_score = StudentScore(team_name=student_team.team_name, team_year=student_team.team_year, test_name=student_team.test_id, total_score=(correct_answers*2), # WESO Scoring says that each multiple choice question is worth 2 points section_one_score=(correct_answers*2), section_two_score=0, section_three_score=0) database_session.add(student_score) database_session.commit()
def test_add_question(): if ('testId' in request.form) and ('question' in request.form): existing_question = iComputeTest.query.filter_by( test_name=request.form['testId'], question=request.form['question']).first() question_section = Questions.query.filter_by( question=request.form['question']).first() if (not existing_question) and question_section: # Check if this test already has a section three question section_question_count = iComputeTest.query.filter_by( test_name=request.form['testId'], section=question_section.section).count() if (question_section.section == 3) and (section_question_count == 1)\ or (question_section.section == 2) and (section_question_count == 2)\ or (question_section.section == 1) and (section_question_count == 5): return 'too many questions' test = iComputeTest.query.filter_by( test_name=request.form['testId']).order_by( desc(iComputeTest.orderId)).first() test_question = iComputeTest( orderId=(test.orderId + 1 if test else 1), question=request.form['question'], section=question_section.section, test_name=request.form['testId'], year=(test.year if test else datetime.now().year), student_grade='5' # TODO Figure out what to do with this ) database_session.add(test_question) database_session.commit() return 'success' return 'error'
def student_team_index(): student_team = StudentTeam.query.filter_by(team_name=current_user.username).order_by(StudentTeam.team_year).first() exam_questions_s1 = [] question = {} # Build Dictionary for questions pulled from the db # TODO grab the correct exam for this team! for counter, exam_question_s1 in enumerate(database_session.query(iComputeTest.question).filter(and_(iComputeTest.test_name == student_team.test_id, iComputeTest.section == 1)).order_by(iComputeTest.orderId), start=1): question['id'] = str(counter) question['question'] = exam_question_s1.question question['answers'] = [] for answer in database_session.query(Questions.answer).filter(Questions.question == exam_question_s1.question): question['answers'].append(escape(answer.answer)) random.shuffle(question['answers']) exam_questions_s1.append(question) question = {} # Grab the Student submitted answers off the form and submit to database if request.method == 'POST': for question in exam_questions_s1: form_response = request.form[question['id']] if question['id'] in request.form else None database_session.add(StudentAnswer( team_name=current_user.username, team_year=student_team.team_year, section=1, # TODO since the question was escaped we may need to unescape it here question=question['question'], answer=form_response)) database_session.commit() grade_section_one(current_user.username) # For now when the team is done taking the test the application will destroy the password and log the team out. return redirect(url_for('student_team.student_s3')) return render_template('multiple_choice.html', exam_questions_s1 = exam_questions_s1)
def delete_question(): if 'question' in request.form: del_query = database_session.query(Questions).filter( Questions.question == request.form['question']) del_query.delete() database_session.commit() del_query = database_session.query(iComputeTest).filter( iComputeTest.question == request.form['question']) del_query.delete() database_session.commit() del_query = database_session.query(QuestionsImages).filter( QuestionsImages.question == request.form['question']) del_query.delete() database_session.commit() return "success"
def admin_add_users(): if 'user_type' in request.form: user_type = request.form['user_type'] if user_type == "Student": if 'username' in request.form and 'password' in request.form and 'test_name' in request.form and 'team_year' in request.form and 'school_name' in request.form: username = request.form['username'] password = request.form['password'] test_name = request.form['test_name'] team_year = request.form['team_year'] school_name = request.form['school_name'] userData = [ Users(username=username, password=generate_password_hash(password), user_type=user_type), StudentTeam(team_name=username, team_year=team_year, school_name=school_name, test_id=test_name) ] database_session.add_all(userData) database_session.commit() elif user_type == "Grader": if 'username' in request.form and 'password' in request.form: username = request.form['username'] password = request.form['password'] userData = [ Users(username=username, password=generate_password_hash(password), user_type=user_type) ] database_session.add_all(userData) database_session.commit() else: if 'username' in request.form and 'password' in request.form: username = request.form['username'] password = request.form['password'] userData = [ Users(username=username, password=generate_password_hash(password), user_type=user_type) ] database_session.add_all(userData) database_session.commit() return 'success'
def add_question(): if request.method == 'POST': if 'section' in request.form: mySection = request.form['section'] if mySection == "multiple-choice": if 'question' in request.form and 'answer' in request.form: myQuestion = request.form['question'] myAnswer = request.form['answer'] question = [ Questions(question=myQuestion, answer=myAnswer, is_correct=True, section=1) ] database_session.add_all(question) database_session.commit() return redirect(url_for('admin.admin_edit_questions')) elif mySection == "short-answer": if 'question' in request.form: myQuestion = request.form['question'] question = [ Questions(question=myQuestion, answer="This is a section 2 question", is_correct=True, section=2) ] database_session.add_all(question) database_session.commit() return redirect(url_for('admin.admin_edit_questions')) elif mySection == "scratch-answer": if 'question' in request.form: myQuestion = request.form['question'] question = [ Questions(question=myQuestion, answer="This is a section 3 question", is_correct=True, section=3), ] database_session.add_all(question) database_session.commit() return redirect(url_for('admin.admin_edit_questions')) return 'success'
def clear_student_answers(): del_query = database_session.query(StudentAnswers) del_query.delete() database_session.commit()