def add_answer(request, q_id): g_candidates = Group.objects.get(name=CANDIDATES_GROUP_NAME) if not request.user.is_authenticated() or g_candidates not in request.user.groups.all(): return HttpResponseForbidden(_("You must be logged in as a candidate to post answers")) question = Question.objects.get(id=q_id) content = request.POST.get("content") if not (question and content): return HttpResponseBadRequest(_("Question does not exist, or empty answer")) answer = Answer(author=request.user, content=content, question=question) answer.save() return HttpResponse(_("Your answer was recorded"))
def post_answer(request, q_id): context = {} question = Question.objects.get(id=q_id) if not question.can_answer(request.user): return HttpResponseForbidden(_("You must be logged in as a candidate to post answers")) try: # make sure the user haven't answered already answer = question.answers.get(author=request.user) except question.answers.model.DoesNotExist: answer = Answer(author=request.user, question = question) answer.content = request.POST.get("content") answer.save() return HttpResponseRedirect(question.get_absolute_url())