Beispiel #1
0
def question_answers(request, question_id):
    question = Question.objects.question_by_id(question_id)
    answers = Answer.objects.answers_by_question(question)

    form = AddAnswerForm(request.POST or None)
    if request.method == "POST" and form.is_valid(
    ) and request.user.is_authenticated():
        new_answer = Answer(text=form.cleaned_data['text'])
        new_answer.question = question
        new_answer.author = request.user
        new_answer.save()

        question.answers_count += 1
        question.save()

        # Send email to question author
        subject = 'New answer for \"' + question.title + '\"'
        message = str(request.build_absolute_uri()) + '#' + str(new_answer.id)
        from_email = settings.EMAIL_HOST_USER
        to_email = str(question.author.email)
        send_mail(subject,
                  message,
                  from_email, [to_email],
                  fail_silently=settings.DEBUG)

        return redirect(
            reverse('questions.views.question_answers',
                    kwargs={'question_id': question_id}) + '#' +
            str(new_answer.id))
    return render(
        request, 'question.html', {
            'popular_tags': cron_cache.get_popular_tags(),
            'best_members': cron_cache.get_best_members(),
            'question': question,
            'answers': answers,
            'form': form,
        })