Beispiel #1
0
def question_adjust_vote(request, question_id, delta=0):
    vote_question_up_rep = getattr(settings, 'VOTE_QUESTION_UP_REP', 20)
    vote_question_down_rep = getattr(settings, 'VOTE_QUESTION_DOWN_REP', 100)

    if not request.user.is_authenticated():
        if delta == 1:
            msg = "You need to sign up and get at least %s rep to vote up a question" % vote_question_up_rep
        elif delta == -1:
            msg = "You need to sign up and get at least %s rep to vote down a question" % vote_question_down_rep
        else:
            msg = None
        return question_read(request, question_id, context={'msg': msg})

    if delta == 1 and get_user_rep(
            request.user.username) < vote_question_up_rep:
        return question_read(
            request,
            question_id,
            context={'msg': "You need at least %s rep to vote up a question"\
                     % vote_question_up_rep}
        )
    elif delta == -1 and get_user_rep(
            request.user.username) < vote_question_down_rep:
        return question_read(
            request,
            question_id,
            context={'msg': "You need at least %s rep to vote down a question"\
                    % vote_question_down_rep}
        )

    question = get_object_or_404(Question, pk=question_id)

    if request.user == question.author:
        return question_read(
            request,
            question_id,
            context={'msg': "You cannot vote on your own questions"})

    try:
        question_vote = QuestionVote.objects.get(question=question,
                                                 user=request.user)
    except QuestionVote.DoesNotExist:
        question_vote = QuestionVote(question=question, user=request.user)
    question_vote.score = delta
    question_vote.save()
    return HttpResponseRedirect(
        reverse('quanda_question_read', args=[question_id]))
Beispiel #2
0
def answer_adjust_vote(request, answer_id, delta=0):
    answer = get_object_or_404(Answer, pk=answer_id)

    vote_answer_up_rep = getattr(settings, 'VOTE_ANSWER_UP_REP', 20)
    vote_answer_down_rep = getattr(settings, 'VOTE_ANSWER_DOWN_REP', 100)

    if not request.user.is_authenticated():
        if delta == 1:
            msg = "You need to sign up and get at least %s rep to vote up an answer" % vote_answer_up_rep
        elif delta == -1:
            msg = "You need to sign up and get at least %s rep to vote down an answer" % vote_answer_down_rep
        else:
            msg = None
        return question_read(request, answer.question.id, context={'msg': msg})

    if delta == 1 and get_user_rep(request.user.username) < vote_answer_up_rep:
        return question_read(
            request,
            answer.question.id,
            context={'msg': "You need at least %s rep to vote up an answer"\
                     % vote_answer_up_rep}
        )
    elif delta == -1 and get_user_rep(
            request.user.username) < vote_answer_down_rep:
        return question_read(
            request,
            answer.question.id,
            context={'msg': "You need at least %s rep to vote down an answer"\
                    % vote_answer_down_rep}
        )

    if request.user == answer.author:
        return question_read(
            request,
            answer.question_id,
            context={'msg': "You cannot vote on your own answers"})

    try:
        answer_vote = AnswerVote.objects.get(answer=answer, user=request.user)
    except AnswerVote.DoesNotExist:
        answer_vote = AnswerVote(answer=answer, user=request.user)
    answer_vote.score = delta
    answer_vote.save()
    return HttpResponseRedirect(
        reverse('quanda_question_read', args=[answer.question.id]))
Beispiel #3
0
def answer_adjust_vote(request, answer_id, delta=0):
    answer = get_object_or_404(Answer, pk=answer_id)

    vote_answer_up_rep = getattr(settings, "VOTE_ANSWER_UP_REP", 20)
    vote_answer_down_rep = getattr(settings, "VOTE_ANSWER_DOWN_REP", 100)

    if not request.user.is_authenticated():
        if delta == 1:
            msg = "You need to sign up and get at least %s rep to vote up an answer" % vote_answer_up_rep
        elif delta == -1:
            msg = "You need to sign up and get at least %s rep to vote down an answer" % vote_answer_down_rep
        else:
            msg = None
        return question_read(request, answer.question.id, context={"msg": msg})

    if delta == 1 and get_user_rep(request.user.username) < vote_answer_up_rep:
        return question_read(
            request,
            answer.question.id,
            context={"msg": "You need at least %s rep to vote up an answer" % vote_answer_up_rep},
        )
    elif delta == -1 and get_user_rep(request.user.username) < vote_answer_down_rep:
        return question_read(
            request,
            answer.question.id,
            context={"msg": "You need at least %s rep to vote down an answer" % vote_answer_down_rep},
        )

    if request.user == answer.author:
        return question_read(request, answer.question_id, context={"msg": "You cannot vote on your own answers"})

    try:
        answer_vote = AnswerVote.objects.get(answer=answer, user=request.user)
    except AnswerVote.DoesNotExist:
        answer_vote = AnswerVote(answer=answer, user=request.user)
    answer_vote.score = delta
    answer_vote.save()
    return HttpResponseRedirect(reverse("quanda_question_read", args=[answer.question.id]))
Beispiel #4
0
def question_adjust_vote(request, question_id, delta=0):
    vote_question_up_rep = getattr(settings, "VOTE_QUESTION_UP_REP", 20)
    vote_question_down_rep = getattr(settings, "VOTE_QUESTION_DOWN_REP", 100)

    if not request.user.is_authenticated():
        if delta == 1:
            msg = "You need to sign up and get at least %s rep to vote up a question" % vote_question_up_rep
        elif delta == -1:
            msg = "You need to sign up and get at least %s rep to vote down a question" % vote_question_down_rep
        else:
            msg = None
        return question_read(request, question_id, context={"msg": msg})

    if delta == 1 and get_user_rep(request.user.username) < vote_question_up_rep:
        return question_read(
            request,
            question_id,
            context={"msg": "You need at least %s rep to vote up a question" % vote_question_up_rep},
        )
    elif delta == -1 and get_user_rep(request.user.username) < vote_question_down_rep:
        return question_read(
            request,
            question_id,
            context={"msg": "You need at least %s rep to vote down a question" % vote_question_down_rep},
        )

    question = get_object_or_404(Question, pk=question_id)

    if request.user == question.author:
        return question_read(request, question_id, context={"msg": "You cannot vote on your own questions"})

    try:
        question_vote = QuestionVote.objects.get(question=question, user=request.user)
    except QuestionVote.DoesNotExist:
        question_vote = QuestionVote(question=question, user=request.user)
    question_vote.score = delta
    question_vote.save()
    return HttpResponseRedirect(reverse("quanda_question_read", args=[question_id]))
Beispiel #5
0
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))
Beispiel #6
0
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))