def answer(question_id): """ Check if the answer to a question is correct """ # make sure question is valid answers = loader.psets()['answers'] if not question_id in answers: abort(404) # check if answer is correct answer = answers[question_id] correct = re.match(answer['answer'], request.form['answer']) != None # if question is correct and unanswered, user gets points count = db.session.query(Answer).filter_by(user_id=g.user.id).filter_by(question=question_id).\ filter_by(correct=True).count() if count == 0 and correct: g.user.add_points(answer['points']) # log answer (for analytics) new_answer = Answer(question_id, g.user.id, request.form['answer'], correct) db.session.add(new_answer) db.session.commit() return util.json_success({ 'correct': correct, 'points': answer['points'] if correct and count == 0 else 0 })
def read(chapter, section): """ Mark a section as read """ # if user hasn't read this section, then update points count = db.session.query(ChapterRead).filter_by(user_id=g.user.id).filter_by(chapter=chapter).\ filter_by(section=section).count() if count == 0: g.user.add_points(settings.POINTS['section']) # add read to database (for analytics) read = ChapterRead(g.user.id, chapter, section) db.session.add(read) db.session.commit() return util.json_success({ 'points': settings.POINTS['section'] if count == 0 else 0 })