Пример #1
0
def question_answer_update(request, room, questiongroup, question):
    if not request.method == 'POST':
        return HttpResponse(status=402)

    if "question_text" in request.POST:
        question_obj = get_object_or_404(Question, id=question)

        # question_obj = Question.objects.get(id=question)
        questionform = AddQuestionForm(request.POST, instance=question_obj)
        if questionform.is_valid():
            questionform.instance.question_text = bleach.clean(questionform.instance.question_text, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, styles=ALLOWED_STYLES)
            questionform.save()
        return JsonResponse(questionform.errors)

    if "answer_text" in request.POST:
        my_id = request.POST['answer_id']
        if my_id == 'None':
            my_id = None

        answerform = AddAnswerForm(request.POST)
        if answerform.is_valid():
            (answer_obj, created) = Answer.objects.update_or_create(id=my_id,
                                                                    question_id=question,
                                                                    defaults={'answer_text': bleach.clean(answerform.instance.answer_text, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, styles=ALLOWED_STYLES),
                                                                                                              'correct': answerform.instance.correct})
            data = serializers.serialize("json", [answer_obj])
            return JsonResponse(data, safe=False)
        return JsonResponse(answerform.errors)

    # return JsonResponse({"message": "Could not update"})
    return HttpResponse(status=402)
Пример #2
0
def question_answer_create(request, room, questiongroup):
    size = len([k for k in request.POST if 'answer_text' in k])
    room_obj = get_object_or_404(Room, pk=room)
    questiongroup_obj = get_object_or_404(QuestionGroup, pk=questiongroup)

    if not room_obj.owner == request.user:
        return redirect(questiongroup_obj)
    if request.method == 'POST':
        questionform = AddQuestionForm(request.POST or None, instance=Question())
        answerform = [AddAnswerForm(request.POST or None, prefix=str(x), instance=Answer()) for x in range(0, size)]
        questionform.instance.group_id = questiongroup

        if questionform.is_valid() and all([af.is_valid() for af in answerform]):
            new_question = questionform.save(commit=False)
            new_question.question_text = bleach.clean(new_question.question_text, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, styles=ALLOWED_STYLES)
            new_question.save()
            for af in answerform:
                new_answer = af.save(commit=False)
                # af.cleaned_data['answer_text'] = bleach.clean(af.cleaned_data['answer_text'])
                new_answer.answer_text = bleach.clean(new_answer.answer_text, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, styles=ALLOWED_STYLES)
                new_answer.question = new_question
                new_answer.save()
            return redirect(new_question)

    else:
        questionform = AddQuestionForm()
        answerform = [AddAnswerForm(prefix=str(0), instance=Answer())]

    return render(request, 'vote/question_create.html', {'qform': questionform, 'aforms': answerform, 'room': room, 'questiongroup': questiongroup, 'qg': questiongroup_obj})