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
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: '******'\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
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: '******'\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
def answer_poll(request): """ ajax poll voting request handler """ log = logging.getLogger('Pollview.answer_poll') user = request.user answer_id = request.GET['answer_id'] poll_id = request.GET['poll_id'] poll = Poll.objects.get(id=poll_id, active=True) #@UndefinedVariable if not request.user.is_authenticated(): next = '/view-poll/%s/' % poll.url return login_block(request, next) poll_answer = PollAnswer.objects.get(id=answer_id) #@UndefinedVariable if PollVote.objects.filter(user=user, poll=poll).count() > 0: #@UndefinedVariable # RUH OH! log.critical( "user trying to vote on poll which he has already voted on, user: %s, poll id: %d" % (user.username, poll.id)) return HttpResponseForbidden("You have already voted on this!") # save PollVote(poll=poll, poll_answer=poll_answer, user=user).save() # increment vote count poll.total_votes += 1 poll.save() vars = {} vars["your_answer"] = poll_answer.answer vars["poll"] = poll 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) pollresultslist = PollService().get_poll_results(poll, drop_zero_results=False) vars["results"] = pollresultslist # check if the last thing here is spillover list with_ans = [] for res in pollresultslist: if res.votes > 0: with_ans.append(res) gcf = GChartFactory() chart = gcf.get_chart(with_ans) resp = render_to_response("pollview-results.html", vars) hsh = {'pvr': resp.content, 'chart': chart.get_url()} return HttpResponse(simplejson.dumps(hsh), mimetype='application/json')
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("/")