def answer_edit(request, answer_id=None): answer = get_object_or_404(Answer, pk=answer_id) if request.user != answer.author: return HttpResponse("Unauthorized") if request.method == 'POST': answer_form = AnswerForm(request.user, answer.question, request.POST, edit=True, instance=answer) if answer_form.is_valid(): answer_form.save() return HttpResponseRedirect( reverse('quanda_question_read', args=[answer.question.id])) else: answer_form = AnswerForm(request.user, answer.question, instance=answer) return render_to_response("quanda/answer_edit.html", { 'answer_form': answer_form, 'answer': answer, 'tinymce': TINY_MCE_JS_LOCATION, }, context_instance=RequestContext(request)) return HttpResponse('edit')
def answer_edit(request, answer_id=None): answer = get_object_or_404(Answer, pk=answer_id) if request.user != answer.author: return HttpResponse("Unauthorized") if request.method == 'POST': answer_form = AnswerForm(request.user, answer.question, request.POST, edit=True, instance=answer) if answer_form.is_valid(): answer_form.save() return HttpResponseRedirect(reverse('quanda_question_read', args=[answer.question.id])) else: answer_form = AnswerForm(request.user, answer.question, instance=answer) return render_to_response("quanda/answer_edit.html", { 'answer_form': answer_form, 'answer': answer, 'tinymce': TINY_MCE_JS_LOCATION, }, context_instance=RequestContext(request)) return HttpResponse('edit')
def question_read(request, question_id=None, msg=None, context={}): question = get_object_or_404(Question, pk=question_id) # user answers question answer_form = AnswerForm(request.user, question) if request.method == "POST" and request.POST.has_key('answer_question'): answer_form = AnswerForm(request.user, question, request.POST) if answer_form.is_valid(): answer = answer_form.save() return HttpResponseRedirect( reverse('quanda_question_read', args=[question_id])) # ==== comments ==== if request.method == "POST" and request.POST.has_key('comment'): # in order to leave a comment, one must be logged in & be the # author of the question or have more rep than the configurable # requirement to leave comments if request.user.is_authenticated(): required_rep = getattr(settings, 'LEAVE_COMMENT', 0) if get_user_rep(request.user.username) >= required_rep: comment_form = CommentForm(request.POST) if comment_form.is_valid(): # create the comment and redirect comment = Comment( content_object=getattr( quanda.models, comment_form.cleaned_data['content_type'], ).objects.get( pk=comment_form.cleaned_data['object_id']), comment_text=comment_form.cleaned_data['comment_text'], posted=datetime.datetime.now(), user=request.user, ip=request.META['REMOTE_ADDR'], ) comment.save() return HttpResponseRedirect( reverse('quanda_question_read', args=[question_id])) else: context[ 'msg'] = "You must have at least %s reputation in order to leave comments " % required_rep else: context['msg'] = "You must be logged in to comment" # get how the user previously voted on this question try: user_question_previous_vote = QuestionVote.objects.get( question=question, user=request.user).score except Exception: user_question_previous_vote = 0 # get questions related to this one related_questions = Question.objects\ .filter(tags__id__in=question.tags.all())\ .exclude(pk=question.id)\ .distinct()\ .order_by('-posted') answers_q = Answer.objects\ .filter(question=question)\ .annotate(score=Sum('answervotes__score'))\ .order_by('-posted')\ .order_by('-score')\ .order_by('-user_chosen') user_answered_question = False # whether this user answered the question answers = [] for answer in answers_q: # did the user answer the question? if answer.author == request.user: user_answered_question = True # indicates the user's last vote on this answer try: answer_vote = AnswerVote.objects.get(answer=answer, user=request.user).score except Exception: answer_vote = 0 answer.user_prev_vote = answer_vote answer.comment_form = CommentForm(initial={ 'content_type': 'Answer', 'object_id': answer.id }) answers.append(answer) # add comment form to the question question.comment_form = CommentForm(initial={ 'content_type': 'Question', 'object_id': question.id }) context['question'] = question context['user_question_previous_vote'] = user_question_previous_vote context['related_questions'] = related_questions context['answer_form'] = answer_form context['answers'] = answers context['user_answered_question'] = user_answered_question context['tinymce'] = TINY_MCE_JS_LOCATION return render_to_response('quanda/question_read.html', context, context_instance=RequestContext(request))
def question_read(request, question_id=None, msg=None, context={}): question = get_object_or_404(Question, pk=question_id) # user answers question answer_form = AnswerForm(request.user, question) if request.method == "POST" and request.POST.has_key('answer_question'): answer_form = AnswerForm(request.user, question, request.POST) if answer_form.is_valid(): answer = answer_form.save() return HttpResponseRedirect(reverse('quanda_question_read', args=[question_id])) # ==== comments ==== if request.method == "POST" and request.POST.has_key('comment'): # in order to leave a comment, one must be logged in & be the # author of the question or have more rep than the configurable # requirement to leave comments if request.user.is_authenticated(): required_rep = getattr(settings, 'LEAVE_COMMENT', 0) if get_user_rep(request.user.username) >= required_rep: comment_form = CommentForm(request.POST) if comment_form.is_valid(): # create the comment and redirect comment = Comment( content_object = getattr( quanda.models, comment_form.cleaned_data['content_type'], ).objects.get(pk=comment_form.cleaned_data['object_id']), comment_text = comment_form.cleaned_data['comment_text'], posted = datetime.datetime.now(), user = request.user, ip = request.META['REMOTE_ADDR'], ) comment.save() return HttpResponseRedirect(reverse('quanda_question_read', args=[question_id])) else: context['msg'] = "You must have at least %s reputation in order to leave comments " % required_rep else: context['msg'] = "You must be logged in to comment" # get how the user previously voted on this question try: user_question_previous_vote = QuestionVote.objects.get(question=question, user=request.user).score except Exception: user_question_previous_vote = 0 # get questions related to this one related_questions = Question.objects\ .filter(tags__id__in=question.tags.all())\ .exclude(pk=question.id)\ .distinct()\ .order_by('-posted') answers_q = Answer.objects\ .filter(question=question)\ .annotate(score=Sum('answervotes__score'))\ .order_by('-posted')\ .order_by('-score')\ .order_by('-user_chosen') user_answered_question = False # whether this user answered the question answers = [] for answer in answers_q: # did the user answer the question? if answer.author == request.user: user_answered_question = True # indicates the user's last vote on this answer try: answer_vote = AnswerVote.objects.get(answer=answer, user=request.user).score except Exception: answer_vote = 0 answer.user_prev_vote = answer_vote answer.comment_form = CommentForm(initial={'content_type': 'Answer', 'object_id': answer.id}) answers.append(answer) # add comment form to the question question.comment_form = CommentForm(initial={'content_type': 'Question', 'object_id': question.id}) context['question'] = question context['user_question_previous_vote'] = user_question_previous_vote context['related_questions'] = related_questions context['answer_form'] = answer_form context['answers'] = answers context['user_answered_question'] = user_answered_question context['tinymce'] = TINY_MCE_JS_LOCATION return render_to_response('quanda/question_read.html', context, context_instance=RequestContext(request))