Пример #1
0
def get_answers(request):
    """Get answers of one question"""

    question_id = request.GET.get('question_id')
    if not question_id:
        return HttpResponse(content=json.dumps({"error": "no id in request"}),
                            status=HTTPStatus.BAD_REQUEST)
    try:
        question = Question.objects.get(id=question_id)
    except Question.DoesNotExist:
        return HttpResponse(content=json.dumps({"error": "no question with this question id"}),
                            status=HTTPStatus.NOT_FOUND)

    page = request.GET.get("page", 1)
    batch = request.GET.get("batch", 10)

    answers, count_pages = Answer.get_answers(question, page, batch)
    serialized_answers = AnswerSerializer(answers, many=True)
    return HttpResponse(json.dumps({"question_id": question_id,
                                    "question_title": question.title,
                                    "question_text": question.text,
                                    "page": page,
                                    "count_pages": count_pages,
                                    "answers": serialized_answers.data}, indent=4))