示例#1
0
def add_answer(request):
    id = request.GET.get("id", None)
    answer = request.GET.get("a", None)

    # cant modify active polls
    p = Poll.objects.get(id=id, active=False)  #@UndefinedVariable
    # make sure user owns polls
    if p.user != request.user:
        return HttpResponseForbidden("unauthorized")

    vars = {}
    if answer.strip() != "":
        if PollAnswer.objects.filter(
                poll=p, answer=answer).count() < 1:  #@UndefinedVariable
            pa = PollAnswer()
            pa.poll = p
            pa.answer = answer
            pa.user = request.user
            pa.save()
        else:
            vars["dupe"] = True
    else:
        vars["whitespace"] = True

    vars["answers"] = PollAnswer.objects.filter(poll=p)  #@UndefinedVariable
    return render_to_response("ajax/poll_answers.html", vars)
示例#2
0
def add_random_answers(poll_id, num_answers, create_new=False, user=None):
    poll = Poll.objects.get(pk=poll_id)      #@UndefinedVariable
    poll_answers = list(PollAnswer.objects.filter(poll=poll))      #@UndefinedVariable
    users = User.objects.filter(username__contains='RandomUser')
    letters = list("abcdefghijklmnopqrstuvwxyz")
    if users.count() < num_answers:
        sys.exit("There are %d users, not enough for %d answers" % (users.count(), num_answers))

    answers = 0
    user_index = 0
    while answers < num_answers:
        try:
            user_voted = PollVote.objects.filter(poll=poll, user=users[user_index]).count() > 0      #@UndefinedVariable
            if not user_voted:
                if create_new == False:
                    rand_poll_answer = poll_answers[random.randint(0, len(poll_answers) - 1)]
                else:
                    # generate a new answer for each user voting
                    new_ans = PollAnswer()
                    new_ans.poll = poll
                    random_word = ""
                    for i in range(0,5):
                        random_word += letters[random.randint(0, len(letters)-1)]
                    new_ans.answer = random_word
                    new_ans.user=users[user_index]
                    new_ans.save()
                    rand_poll_answer = new_ans 
                poll_vote = PollVote(poll=poll, poll_answer=rand_poll_answer, user=users[user_index])
                poll_vote.save()
                answers += 1
                poll.total_votes += 1
                poll.save()                
        except IndexError:
            sys.exit("Only made %d answers, not enough users who havent voted" % answers)
        user_index += 1
示例#3
0
def new_answer(request):
    """
    handles form requests to add a new answer
    """
    log = logging.getLogger('Pollview.new_answer')
    vars = {}
    pid = request.POST.get("pid", None)
    if pid == None:
        return HttpResponseRedirect("/")

    preview = request.POST.get('preview', None)
    if str(preview).strip().lower() == 'true':
        return HttpResponseRedirect(
            '/preview-poll/%s/' %
            Poll.objects.get(id=pid).url)  #@UndefinedVariable

    poll = Poll.objects.get(id=pid, active=True)  #@UndefinedVariable

    if not poll.allow_user_answers:
        # poll does not allow this sort of behavior
        return HttpResponseRedirect("/view-poll/%s/" % poll.url)

    user = request.user
    if not user.is_authenticated():
        return HttpResponseRedirect("/login/?next=" +
                                    '/view-poll/%s/' % poll.url)

    # user has already voted they cant add anymore answers
    if PollVote.objects.filter(poll=poll,
                               user=user).count() > 0:  #@UndefinedVariable
        log.warning("already voted: %s" + user.username)
        return HttpResponseRedirect('/view-poll/%s/' % poll.url)

    if request.method == 'POST':  # If the form has been submitted...
        form = AnswerForm(request.POST)  # A form bound to the POST data
        if form.is_valid():  # All validation rules pass
            answer = form.cleaned_data["new_answer"]

            newAnswer = None
            try:
                # see if it exists
                newAnswer = PollAnswer.objects.get(
                    answer=answer, poll=poll)  #@UndefinedVariable
            except PollAnswer.DoesNotExist:  #@UndefinedVariable
                newAnswer = None

            if newAnswer == None:
                # make answer
                newAnswer = PollAnswer()
                newAnswer.poll = poll
                newAnswer.answer = answer
                newAnswer.user = user
                newAnswer.save()

            # automatically cast vote for new answer
            PollVote(poll=poll, poll_answer=newAnswer, user=user).save()

            # increment total vote count
            poll.total_votes += 1
            poll.save()

            if poll.user != request.user and poll.user.userdata_set.all(
            )[0].email_on_vote:
                send_voted_email(to_email=poll.user.email,
                                 from_user=user,
                                 poll=poll)

            return HttpResponseRedirect("/view-poll/%s/" % poll.url)
        else:
            return HttpResponseRedirect("/")
    return HttpResponseRedirect("/")