def create_or_search_question(): body = request.get_json() if not body: abort(400) question = body.get('question', None) answer = body.get('answer', None) category = body.get('category', None) difficulty = body.get('difficulty', None) search_term = body.get('searchTerm', None) # if not questions search case, any missing args in the body will be considered bad request if search_term is None: for arg in ['question', 'answer', 'category', 'difficulty']: if arg not in body: abort(400) try: if search_term is not None: questions = Question.query.filter( Question.question.ilike('%{}%'.format(search_term))).all() selection = [question.format() for question in questions] current_questions = paginate_questions(request, selection) return jsonify({ 'success': True, 'questions': current_questions, 'total_questions': len(selection), 'current_category': None }) else: for arg in [question, answer, category, difficulty]: if not arg: abort(422) if not Category.query.get(category): abort(422) question = Question(question=question, answer=answer, category=category, difficulty=difficulty) question.insert() question_id = question.id selection = [ question.format() for question in Question.query.all() ] current_questions = paginate_questions(request, selection) return jsonify({ 'success': True, 'created': question_id, 'questions': current_questions, 'total_questions': len(selection), 'current_category': None }) except: rollback() print(sys.exc_info()) abort(422) finally: close_connection()
def create_user(uname, fname, lname, email, password): models.open_connection() tok = binascii.b2a_hex(os.urandom(20)) User.create(username=uname, first_name=fname, last_name=lname, email=email, password=password, auth_token=tok) models.close_connection() return tok
def validate_user_token(token): models.open_connection() success = True token = None try: tmp = User.get(auth_token=token) token = tmp.auth_token except User.DoesNotExist: success = False models.close_connection() return {'success': success, 'token': token}
def validate_user(uname, password): models.open_connection() success = True token = None try: tmp = User.get(username=uname, password=password) token = tmp.auth_token except User.DoesNotExist: success = False models.close_connection() return {'success': success, 'token': token}
def delete_question(question_id): question = Question.query.get(question_id) if question is None: abort(422) try: question.delete() selection = [ question.format() for question in Question.query.all() ] current_questions = paginate_questions(request, selection) return jsonify({ 'success': True, 'deleted': question_id, 'questions': current_questions, 'total_questions': len(selection) }) except: rollback() print(sys.exc_info()) abort(422) finally: close_connection()
from models import create_connection, close_connection from models.parser import run (cnx, cursor) = create_connection() run(cursor) close_connection(cnx, cursor)