def reply(request, question_id): """Post a new answer to a question.""" question = get_object_or_404(Question, pk=question_id, is_spam=False) answer_preview = None if not question.allows_new_answer(request.user): raise PermissionDenied form = AnswerForm(request.POST, **{"user": request.user, "question": question}) # NOJS: delete images if "delete_images" in request.POST: for image_id in request.POST.getlist("delete_image"): ImageAttachment.objects.get(pk=image_id).delete() return question_details(request, question_id=question_id, form=form) # NOJS: upload image if "upload_image" in request.POST: upload_imageattachment(request, request.user) return question_details(request, question_id=question_id, form=form) if form.is_valid() and not request.limited: answer = Answer( question=question, creator=request.user, content=form.cleaned_data["content"], ) if "preview" in request.POST: answer_preview = answer else: if form.cleaned_data.get("is_spam"): _add_to_moderation_queue(request, answer) else: answer.save() ans_ct = ContentType.objects.get_for_model(answer) # Move over to the answer all of the images I added to the # reply form user_ct = ContentType.objects.get_for_model(request.user) up_images = ImageAttachment.objects.filter(creator=request.user, content_type=user_ct) up_images.update(content_type=ans_ct, object_id=answer.id) # Handle needsinfo tag if "needsinfo" in request.POST: question.set_needs_info() elif "clear_needsinfo" in request.POST: question.unset_needs_info() if Setting.get_for_user(request.user, "questions_watch_after_reply"): QuestionReplyEvent.notify(request.user, question) return HttpResponseRedirect(answer.get_absolute_url()) return question_details( request, question_id=question_id, form=form, answer_preview=answer_preview )
def edit_answer(request, question_id, answer_id): """Edit an answer.""" answer = get_object_or_404(Answer, pk=answer_id, question=question_id) answer_preview = None if not answer.allows_edit(request.user): raise PermissionDenied # NOJS: upload images, if any upload_imageattachment(request, answer) if request.method == "GET": form = AnswerForm({"content": answer.content}, user=request.user) return render(request, "questions/edit_answer.html", { "form": form, "answer": answer }) form = AnswerForm(request.POST, **{"user": request.user}) if form.is_valid(): answer.content = form.cleaned_data["content"] answer.updated_by = request.user if "preview" in request.POST: answer.updated = datetime.now() answer_preview = answer else: log.warning("User %s is editing answer with id=%s" % (request.user, answer.id)) if form.cleaned_data.get("is_spam"): _add_to_moderation_queue(request, answer) else: answer.save() return HttpResponseRedirect(answer.get_absolute_url()) return render( request, "questions/edit_answer.html", { "form": form, "answer": answer, "answer_preview": answer_preview }, )
def _answers_data(request, question_id, form=None, watch_form=None, answer_preview=None): """Return a map of the minimal info necessary to draw an answers page.""" question = get_object_or_404(Question, pk=question_id) answers_ = question.answers.all() # Remove spam flag if an answer passed the moderation queue if not settings.READ_ONLY: answers_.filter(flags__status=2).update(is_spam=False) if not request.user.has_perm("flagit.can_moderate"): answers_ = answers_.filter(is_spam=False) answers_ = paginate(request, answers_, per_page=config.ANSWERS_PER_PAGE) feed_urls = (( reverse("questions.answers.feed", kwargs={"question_id": question_id}), AnswersFeed().title(question), ), ) frequencies = dict(FREQUENCY_CHOICES) is_watching_question = request.user.is_authenticated and ( QuestionReplyEvent.is_notifying(request.user, question) or QuestionSolvedEvent.is_notifying(request.user, question)) return { "question": question, "answers": answers_, "form": form or AnswerForm(), "answer_preview": answer_preview, "watch_form": watch_form or _init_watch_form(request, "reply"), "feeds": feed_urls, "frequencies": frequencies, "is_watching_question": is_watching_question, "can_tag": request.user.has_perm("questions.tag_question"), "can_create_tags": request.user.has_perm("taggit.add_tag"), }