def get_question(question_id): """Returns a specific question, by its question ID, which is passed in as a parameter to the call. Keyword arguments: question_id -- the identifier of the question being interacted with """ question = Question.query.filter_by(id=question_id).first() if not question: abort(404) return json_response(question.to_dict())
def delete_question(question_id): """Handles requests to delete a question, by its ID. Keyword arguments: question_id -- the identifier of the question being interacted with """ question = Question.query.filter_by(id=question_id).first() if not question: abort(404) question.delete() return json_response({}, 204)
def update_question(question_id): """Handles requests to update a specific question, identified by that questions ID, which is passed in as a parameter to the call. Keyword arguments: question_id -- the identifier of the question being interacted with """ data = json.loads(request.data) question = Question.query.filter_by(id=question_id).first() if not question: abort(404) question.update(data) return json_response({ 'result': question.to_dict(), 'timestamp': str(datetime.now()) }, 200)
def create_question(): """Creates and returns the question that adheres to the content of the request. If there is an error, an error response will be returned instead. """ data = json.loads(request.data) if not ('text' in data): raise Exception('Error: Question text is required.') if not ('answer' in data): raise Exception('Error: Question answer is required.') if not ('choices' in data): raise Exception('Error: Question answer choices are required.') #validate question? yeesh... let's keep it simple for now... question = Question(text=str(data['text']), answer=str(data['answer']), choices=str(data['choices'])) db.session.add(question) db.session.commit() return json_response(question.to_dict(), 201)
def list_questions(): """Returns a list of all questions known to the application. """ query = Question.query questions = [] limit = -1 sort_direction = 1 ## Get the sort order (ascending or descending) if 'sort' in request.args: sort_direction = get_sort_direction(request.args['sort']) ## Apply sorting logic. if 'sortby' in request.args: field = getattr(Question, request.args['sortby']) print '[INFO ] Sorting by: {} in direction: {}'.format(field, 'ASC' if sort_direction >= 0 else 'DESC') query = query.order_by(field) if sort_direction >= 0 else query.order_by(field.desc()) ## Get the limit from the query parameters, this will ## be used both for paginated and non-paginated queries if 'limit' in request.args: limit = int(request.args['limit']) print '[INFO ] Limiting query to: {} results'.format(limit) ## If the user has specified a "page" then we're going ## to assume that they want their results to be paginated. ## ## This is a good first pass, but what if the user specifies ## a page that is out of range? They get a 404... this ## might be OK. Need to think on it a bit more. [SWQ] if 'page' in request.args: limit = limit if limit > 0 else 10 questions = query.paginate(int(request.args['page']), limit).items else: if limit > 0: query = query.limit(limit) questions = query.all() results = [q.to_dict() for q in questions] return json_response({ 'results': results })