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
Exemple #2
0
    def random_vote_by_user(self):
        valid_user_index = random.randint(0, len(USER_IDS_TO_CREATE) - 1)
        user_id = USER_IDS_TO_CREATE[valid_user_index]
        user = User.objects.get(id=user_id)
        ps = PollService()
        user_new_polls = ps.get_polls_user_has_not_voted_on(user=user, return_all=False)
        if user_new_polls:
            index = random.randint(0, len(user_new_polls) - 1)
            poll = user_new_polls[index]
            print "Selecting from %d polls" % len(user_new_polls)
            poll_answers = list(PollAnswer.objects.filter(poll=poll))  # @UndefinedVariable
            rand_poll_answer = poll_answers[random.randint(0, len(poll_answers) - 1)]

            poll_vote = PollVote(poll=poll, poll_answer=rand_poll_answer, user=user)
            poll_vote.save()

            poll.total_votes += 1
            poll.save()
            pollview.send_voted_email(to_email=poll.user.email, from_user=user, poll=poll)

            print "User: '%s'\nVoted on poll: '%s'\nAnswer: '%s'" % (
                user.username,
                poll.question,
                rand_poll_answer.answer,
            )
        else:
            print "Could not vote, user has voted on all polls: %s" % user.username
        return False