def question(self, request, slug, sort='votes', page=1): question = Question.query.filter_by(slug=slug).one() answer_query = Answer.query.filter_by(question=question) # Order by "votes", "latest" or "oldest" answer_query = getattr(answer_query, sort) pagination = URLPagination(answer_query, page) form = AnswerQuestionForm(request.form) if form.validate_on_submit(): answer = Answer(author=request.user, question=question, text=form.text.data) db.session.commit() return redirect(href(question)) # increase counters question.touch() # precalculate user votes answers = pagination.query user_votes = Vote.query.get_user_votes_on(request.user.id, [a.id for a in answers]) return { 'sort': sort, 'question': question, 'answers': pagination.query, 'form': form, 'pagination': pagination, 'user_votes': user_votes }
def answer(self, request, entry_id, action=None): answer = Answer.query.get(entry_id) if action == 'edit': form = AnswerQuestionForm(request.form, text=answer.text) if form.validate_on_submit(): answer.text = form.text.data db.session.commit() return redirect_to(answer) return {'form': form} else: kwargs = {'_anchor': 'answer-%s' % answer.id} return redirect_to(answer.question, **kwargs)
def answer(self, request, entry_id, action=None): answer = Answer.query.get(entry_id) if action == 'edit': form = AnswerQuestionForm(request.form, text=answer.text) if form.validate_on_submit(): answer.text = form.text.data db.session.commit() return redirect_to(answer) return { 'form': form } else: kwargs = {'_anchor': 'answer-%s' % answer.id} return redirect_to(answer.question, **kwargs)