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 friend(request): id = request.GET.get("id") user = User.objects.get(id=id) if request.user == user: return HttpResponseForbidden("Nope!!") us = UserService(request.user) data = us.getUserData() # add friend data.friends.add(user) data.save() # do reverse as well # TODO: check user settings before auto adding this to_data = UserService(user).getUserData() # add friend to_data .friends.add(request.user) to_data.save() # notify user ps = PollService() subject = "%s added you as friend" % request.user.username body = "Hi, %s has just added you as a friend. As a result %s has been automatically added to your friends list.\n This is because he/she added you to their list. You can change this setting in your settings if you like." % (request.user.username, request.user.username) ps.sendPollsterMessage(to_user=user, from_user=request.user, subject=subject, body=body) return HttpResponse(content="You are now friends!")
def watch_poll(request): """ ajax handler signs up users to watch a poll """ log = logging.getLogger('pollview.watch_poll') pid = request.GET.get("pid", None) type = request.GET.get("type", None) ps = PollService() if type == 'a': # add the poll to request users watch userdata = UserService(request.user).getUserData() poll = Poll.objects.get(id=pid, active=True) #@UndefinedVariable if PollWatch.objects.filter( user=request.user, poll=poll).count() == 0: #@UndefinedVariable pw = PollWatch() pw.poll = poll pw.user = request.user pw.save() userdata.polls_watched.add(pw) userdata.save() else: log.error("User trying to watch poll multiple times, user: %s" % request.user.username) return HttpResponse("Already watching!") if poll.user != request.user: # send message to poll creator # as long as you arent poll creator subject = "%s is watching your poll" % request.user.username body = "Hello. This is just a note to let you know %s is watching your poll '%s'. Thanks, The Mgmt." % ( request.user.username, poll.question) ps.sendPollsterMessage(to_user=poll.user, from_user=request.user, subject=subject, body=body) return HttpResponse("You are watching this poll") elif type == 'd': userdata = UserService(user=request.user).getUserData() poll = Poll.objects.get(id=pid) #@UndefinedVariable pw = PollWatch.objects.get(user=request.user, poll=poll) #@UndefinedVariable userdata.polls_watched.remove(pw) userdata.save() pw.delete() return HttpResponse("You are no longer watching this poll") return HttpResponseForbidden("unauthorized")
def index(request): vars = {} inactive_polls = Poll.objects.filter(user=request.user, active=False) #@UndefinedVariable if len(inactive_polls) > 0: vars["inactive_polls"] = inactive_polls form = PollForm() if request.method == 'POST': form = PollForm(request.POST) if form.is_valid(): # All validation rules pass p = Poll() question = form.cleaned_data["question"] question = question.strip() q_url = PollService().get_next_url(question) p.question = form.cleaned_data["question"] p.url = q_url p.user = request.user p.total_votes = 0 p.total_comments = 0 p.save() return HttpResponseRedirect("/create-poll/answers/%s/" % q_url) vars["form"] = form return base.render(request, "pollcreate.html", vars)
def index(request, selected='recent', db_polls=None, daysago=None, page_num=1, page_link_prefix="", time_link_prefix=""): # PollService().reconcileTagCount() # PollService().reconcilePollVoteCounts() # PollService().reconcileUntaggedPolls() # PollService().reconcilePollCommentsCount() # PollService().resizeAnswerColumn() print 'setting pw' #UserService().set_user_pw('ew', '8a7p9a0a') polls = PollService().getPollsForLister( db_polls, request.user, page_num, page_link_prefix + time_link_prefix) vars = {} vars.update(polls) if page_num > 1: vars['show_ad'] = True vars["selected"] = selected vars["link_prefix"] = page_link_prefix vars["time_link_prefix"] = time_link_prefix return base.render(request, "home.html", vars)
def index(request, search_terms=None, page_num=1, sort=None): initial = {"type":0} vars = {} vars["sort"] = sort if search_terms: ps = PollService() user = RequestUtils.getCurrentUser(request) prefix = "/search/%s" % search_terms vars["prefix"] = prefix polls = ps.searchPolls(search_terms, user, sort) paging_prefix = ps.addSortSuffix(prefix, sort) polls = ps.getPollsForLister(polls, request.user, page_num, page_link_prefix=paging_prefix) vars.update(polls) vars["search_terms"] = search_terms vars["form"] = SearchForm(initial=initial) vars["meta_desc"] = "Pollstruck search for %s." % search_terms return base.render(request, "search.html", vars)
def doSearch(request, search_terms=None): if search_terms == None: return HttpResponse("Please enter something!") user = RequestUtils.getCurrentUser(request) polls = PollService().searchPolls(search_terms, user) return render_to_response("base/poll_lister.html", {"polls": polls})
def watch_poll(request): """ ajax handler signs up users to watch a poll """ log = logging.getLogger('pollview.watch_poll') pid = request.GET.get("pid", None) type = request.GET.get("type", None) ps = PollService() if type == 'a': # add the poll to request users watch userdata = UserService(request.user).getUserData() poll = Poll.objects.get(id=pid, active=True) #@UndefinedVariable if PollWatch.objects.filter(user=request.user, poll=poll).count() == 0: #@UndefinedVariable pw = PollWatch() pw.poll = poll pw.user = request.user pw.save() userdata.polls_watched.add(pw) userdata.save() else: log.error("User trying to watch poll multiple times, user: %s" % request.user.username) return HttpResponse("Already watching!") if poll.user != request.user: # send message to poll creator # as long as you arent poll creator subject = "%s is watching your poll" % request.user.username body = "Hello. This is just a note to let you know %s is watching your poll '%s'. Thanks, The Mgmt." % (request.user.username, poll.question) ps.sendPollsterMessage(to_user=poll.user, from_user=request.user, subject=subject, body=body) return HttpResponse("You are watching this poll") elif type == 'd': userdata = UserService(user=request.user).getUserData() poll = Poll.objects.get(id=pid) #@UndefinedVariable pw = PollWatch.objects.get(user=request.user, poll=poll) #@UndefinedVariable userdata.polls_watched.remove(pw) userdata.save() pw.delete() return HttpResponse("You are no longer watching this poll") return HttpResponseForbidden("unauthorized")
def activity_home_index(request, username=None): vars = {} user = User.objects.get(username=username) owner = request.user == user activity, remain = PollService().getUserFeed(user=user, owner=owner, request_user=request.user) vars["activity"] = activity vars["remain"] = remain vars['class'] = 'activity-feed' return index(request, username, polls=None, vars=vars, page_num=1, page_link_prefix='/profile/%s/activity/')
def tag(request, tag_name=None, page_num=1, sort=None): """ tag search page """ vars = {} vars["sort"] = sort initial = {"type": 1} if tag_name: vars["search_terms"] = tag_name vars["tag"] = tag_name tag_prefix = "/tag/%s" % tag_name vars["prefix"] = tag_prefix tags = [] tag_names = tag_name.split(",") for t in tag_names: try: tags.append( Tag.objects.get(name=t.strip())) #@UndefinedVariable except Tag.DoesNotExist: #@UndefinedVariable pass if len(tags) > 0: ps = PollService() user = RequestUtils.getCurrentUser(request) polls = ps.searchPollsByTag(tag=tags, sort=sort) prefix = ps.addSortSuffix(tag_prefix, sort) spolls = PollService().getPollsForLister(polls, user, page_num=page_num, page_link_prefix=prefix) vars.update(spolls) vars["form"] = SearchForm(initial=initial) # special side bar for tag search view tags = Tag.objects.filter()[0:200] #@UndefinedVariable vars["side_nav_top_title"] = "Tags" vars["tag_cloud"] = buildTagCloud(tags) desc = "Pollstruck Tag search for: " for t in tag_names: desc += t.strip() desc = desc.rstrip(", ") vars["meta_desc"] = desc return base.render(request, "search.html", vars)
def index(request, search_terms=None, page_num=1, sort=None): initial = {"type": 0} vars = {} vars["sort"] = sort if search_terms: ps = PollService() user = RequestUtils.getCurrentUser(request) prefix = "/search/%s" % search_terms vars["prefix"] = prefix polls = ps.searchPolls(search_terms, user, sort) paging_prefix = ps.addSortSuffix(prefix, sort) polls = ps.getPollsForLister(polls, request.user, page_num, page_link_prefix=paging_prefix) vars.update(polls) vars["search_terms"] = search_terms vars["form"] = SearchForm(initial=initial) vars["meta_desc"] = "Pollstruck search for %s." % search_terms return base.render(request, "search.html", vars)
def mail(request, username=None): """ handles ajax send of message """ log = logging.getLogger('mail') subject = request.POST.get("subject", None) body = request.POST.get("body", None) mid = request.POST.get("mid", None) if mid == 'undefined': mid = None # get user who message is to try: user = User.objects.get(username=username) except User.DoesNotExist: #@UndefinedVariable log.warning("Attempt to send mail to invalid user: %s done by user: %s" % (username, request.user.username)) return HttpResponse(content="<img src=\"/i/icons/email_delete.png\"/> Your message was not sent - 101") if not body or body.strip() == "": return HttpResponse(content="<img src=\"/i/icons/email_delete.png\"/> You need to provide a message") if user == request.user: return HttpResponseForbidden("D'oh! ;)") body = string.strip(body) body = body.replace("\n", "<br/>") if mid: old_msg = PollsterMessage.objects.get(id=mid) #@UndefinedVariable old_body = old_msg.body old_arr = old_body.split("<br/>") old_body = "" for msg_chnk in old_arr: old_body += "> " + msg_chnk + "<br/>" body = str(body) + "<br/><br/>Previous message from " + str(old_msg.from_user.username) + "<hr/><br/>" + str(old_body) # create message ps = PollService() ps.sendPollsterMessage(to_user=user, from_user=request.user, subject=subject, body=body) return HttpResponse(content="<img src=\"/i/icons/email_go.png\"/> Your message has been sent!")
def answers(request, poll_url=None, errors=None, form=None): """answers creation view""" vars = {} ps = PollService() log = logging.getLogger("POLL CREATE") # get the poll p = Poll.objects.get(url=poll_url) #@UndefinedVariable vars["poll"] = p # make sure the user editing the poll is the creator if p.user != request.user: log.debug( "User attempting to finish poll creator who did not create it: %s" % p.url) return HttpResponseRedirect("/") if p.active: vars["edit_mode"] = True votes = PollVote.objects.filter(poll=p).count() #@UndefinedVariable vars[ "edit_active"] = votes < consts.POLL_VOTES_BEFORE_PERMANENTLY_ACTIVE # get the answers pa = PollAnswer.objects.filter(poll=p) #@UndefinedVariable pa_len = len(pa) if pa_len > 0: moreAnswers = pa_len - consts.MIN_ANSWERS_FOR_ACTIVE_POLL if moreAnswers < 0: vars["moreAnswers"] = moreAnswers vars["answers"] = pa #load up tags tags = p.tags.all() if len(tags) > 0: vars["tags"] = tags if form == None: form = PollAnswerForm() # set initial fields form.fields["description"].initial = p.description plink = p.link form.fields["link"].initial = plink if plink != None and plink.strip() != "": vars["link"] = plink vars["domain"] = urlparse(plink)[1] vars["form"] = form vars["allow_user_answers"] = p.allow_user_answers vars["errors"] = errors return base.render(request, "poll_create_answers.html", vars)
def get_spillover_answers(request): """ ajax handler for spill over request """ pid = request.GET.get("pid", None) poll = Poll.objects.get(id=pid) #@UndefinedVariable answers = PollService().get_poll_results(poll, drop_zero_results=False) vars = {} for a in answers: if a.__class__ == PollResultSpilloverListItem: vars["answers"] = a.spill_over_list vars["poll"] = poll return base.render(request, "pollview/pollview-answer-lister.html", vars)
def mailusers(request): vars = {} form = None if request.method == 'POST': # If the form has been submitted... form = MailUsersForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass subject = form.cleaned_data["subject"] body = form.cleaned_data["body"] body = body.replace("\n", "<br/>") par_user = User.objects.get(username='******') # message every user in the system for u in User.objects.all(): ps = PollService() ps.sendPollsterMessage(to_user=u, from_user=par_user, subject=subject, body=body) vars["done"] = True if not form: form = MailUsersForm(initial={"subject":"Recent updates"}) vars["form"] = form return base.render(request, template="admin/mailuser.html", vars=vars)
def tag(request, tag_name=None, page_num=1, sort=None): """ tag search page """ vars= {} vars["sort"] = sort initial = {"type":1} if tag_name: vars["search_terms"] = tag_name vars["tag"] = tag_name tag_prefix = "/tag/%s" % tag_name vars["prefix"] = tag_prefix tags = [] tag_names = tag_name.split(",") for t in tag_names: try: tags.append(Tag.objects.get(name=t.strip())) #@UndefinedVariable except Tag.DoesNotExist: #@UndefinedVariable pass if len(tags) > 0: ps = PollService() user = RequestUtils.getCurrentUser(request) polls = ps.searchPollsByTag(tag=tags, sort=sort) prefix = ps.addSortSuffix(tag_prefix, sort) spolls = PollService().getPollsForLister(polls, user, page_num=page_num, page_link_prefix=prefix) vars.update(spolls) vars["form"] = SearchForm(initial=initial) # special side bar for tag search view tags = Tag.objects.filter()[0:200] #@UndefinedVariable vars["side_nav_top_title"] = "Tags" vars["tag_cloud"] = buildTagCloud(tags) desc = "Pollstruck Tag search for: " for t in tag_names: desc += t.strip() desc = desc.rstrip(", ") vars["meta_desc"] = desc return base.render(request, "search.html", vars)
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 index(request, username=None, polls=None, vars=None, page_num=1, page_link_prefix=None): #if vars == None: # return voted_on(request, username=username, page_num=page_num) #vars = {"class":"index-view"} user_for_profile = None if username == None: # if no username specified then just display # profile of user who is logged in user_for_profile = request.user else: user_for_profile = User.objects.get(username=username) vars["user_for_profile"] = user_for_profile vars['show_ad'] = True # get some sweet user stats! total_polls_count = Poll.objects.filter(active=1).count() #@UndefinedVariable user_polls_count = Poll.objects.filter(user=user_for_profile, active=1).count() #@UndefinedVariable vars["user_polls_count"] = user_polls_count if total_polls_count > 0: vars["user_polls_percent"] = (1.0 * user_polls_count / total_polls_count) * 100 user_comments_count = 0 poll_set = set() for tc in ThreadedComment.objects.filter(user=user_for_profile): poll_set.add(tc.object_id) user_comments_count+=1 vars["user_comments_count"] = user_comments_count vars["user_poll_comments"] = len(poll_set) user_votes_count = PollVote.objects.filter(user=user_for_profile).count() #@UndefinedVariable vars["user_votes_count"] = user_votes_count if total_polls_count > 0: vars["user_votes_percent"] = (1.0 * user_votes_count / total_polls_count) * 100 # check owner owner = user_for_profile.username == request.user.username vars["owner"] = owner # pass in the logged in user if not vars.has_key('activity'): if not page_link_prefix: page_link_prefix = "/profile/%s" % user_for_profile.username polls = PollService().getPollsForLister(polls, request.user, page_num=page_num, page_link_prefix=page_link_prefix) vars.update(polls) else: pass vars["user_prof_data"] = UserService(user_for_profile).getUserData() vars["messageForm"] = MessageForm() return base.render(request, "user/profile.html", vars)
def activity_index(request, username=None): vars = {} phase = int(request.GET.get('phase', 0)) user_for_profile = User.objects.get(username=username) owner = user_for_profile == request.user vars["owner"] = owner activity, remain = PollService().getUserFeed(user=user_for_profile, owner=owner, phase=phase, request_user=request.user) vars["activity"] = activity vars["remain"] = remain vars["user_for_profile"] = user_for_profile vars["datetimenow"] = datetime.datetime.now() if remain: return base.render(request=request, template="user/profile-activity-lister.html", vars=vars) else: return HttpResponse(content="0")
def index(request, poll_url=None, demographics=None, preview=False): """ index handler """ log = logging.getLogger("POLL VIEW") pollService = PollService() vars = {} vars['show_ad'] = True vars["pollview"] = True vars["preview"] = preview p = None random_poll = pollService.get_random_polls(1, request.user) if len(random_poll) > 0: vars["random_poll"] = random_poll[0] if poll_url == None: return HttpResponseRedirect("/view-poll/%s/" % random_poll.url) else: try: # get the poll and its tags if preview: p = Poll.objects.get(url=poll_url) #@UndefinedVariable if request.user != p.user: log.debug("User %s trying to preview poll he doesn't own, %s" % (request.user.username, p.url)) raise Http404 else: p = Poll.objects.get(url=poll_url, active=True) #@UndefinedVariable except Poll.DoesNotExist: #@UndefinedVariable log.debug("Attempt to view poll which is not active or does not exist, trying to send to create, slug: %s" % poll_url) return HttpResponseRedirect("/create-poll/answers/%s/" % poll_url) tags = p.tags.all() vars["tags"] = tags vars["poll"] = p if p.description and p.description != "": vars["poll_desc"] = p.description if p.link: vars["domain"] = urlparse(p.link)[1] if p.total_votes == 0: vars["noVotes"] = True # get the poll files pf = PollFile.objects.filter(poll=p) #@UndefinedVariable vars["pollFiles"] = pollService.get_poll_file_w_icons(pf) # check for content box if len(pf) > 0 or p.video_link or p.link: vars["showContentBox"] = True # do not show voting form if user has already voted form = None results = pollService.get_poll_results(p, drop_zero_results=False) # user is logged in if request.user.is_authenticated(): # check to see if user is watching this us = UserService(request.user) data = us.getUserData() if data.polls_watched.filter(poll=p).count() > 0: vars["watching"] = True if request.user == p.user: vars["creator"] = True # get the answer forms try: # check if user has voted poll_vote = PollVote.objects.get(poll=p, user=request.user) #@UndefinedVariable vars['your_answer'] = poll_vote.poll_answer.answer except PollVote.DoesNotExist: #@UndefinedVariable # they haven't so show them the answer form form = poll_answer_style_factory(consts.PREDEFINED_ANSWERS, p, results) except PollVote.MultipleObjectsReturned: #@UndefinedVariable # oh jesus, somehow they have multiple votes, log it and take their first one poll_vote = PollVote.objects.filter(poll=p, user=request.user)[0] #@UndefinedVariable logging.getLogger("PollView").critical("user has more than one vote for a poll id: %d and user: %s." % (p.id, request.user.username)) else: form = poll_answer_style_factory(consts.PREDEFINED_ANSWERS, p, results) # open flash chart demographic_url_param = '' if demographics: demographic_url_param = demographics ansForm = AnswerForm() vars["answerForm"] = ansForm vars['form'] = form # poll display type chart_type = consts.DEFAULT_CHART_TYPE vars["link_in_new_window"] = True if request.user.is_authenticated(): data = UserService(request.user).getUserData() vars["link_in_new_window"] = data.link_in_new_window chart_type = data.default_chart_type # get the gchart vars["chart_type"] = chart_type gcf = GChartFactory() res_w_votes = [] res_wo_votes = [] # grab only the results with votes to display on the chart for r in results: if r.votes > 0: res_w_votes.append(r) else: res_wo_votes.append(r) pchart = gcf.get_chart(res_w_votes) vars["gChart"] = pchart.get_url() vars["results"] = res_w_votes vars['results_wo_votes'] = res_wo_votes # create description tag: description = "" description += "Is it: " for i in range(0, len(vars["results"])): a = vars["results"][i] if isinstance(a.answer, PollAnswer): description += a.answer.answer + ", " if i+1 == len(vars["results"]) - 1: description += "or " description = description.rstrip(", ") description += "? " if p.description: description += p.description vars["meta_desc"] = description if p.link: vars['domain'] = urlparse(p.link) return base.render(request, "pollview.html", vars)
def index(request, poll_url=None, demographics=None, preview=False): """ index handler """ log = logging.getLogger("POLL VIEW") pollService = PollService() vars = {} vars['show_ad'] = True vars["pollview"] = True vars["preview"] = preview p = None random_poll = pollService.get_random_polls(1, request.user) if len(random_poll) > 0: vars["random_poll"] = random_poll[0] if poll_url == None: return HttpResponseRedirect("/view-poll/%s/" % random_poll.url) else: try: # get the poll and its tags if preview: p = Poll.objects.get(url=poll_url) #@UndefinedVariable if request.user != p.user: log.debug( "User %s trying to preview poll he doesn't own, %s" % (request.user.username, p.url)) raise Http404 else: p = Poll.objects.get(url=poll_url, active=True) #@UndefinedVariable except Poll.DoesNotExist: #@UndefinedVariable log.debug( "Attempt to view poll which is not active or does not exist, trying to send to create, slug: %s" % poll_url) return HttpResponseRedirect("/create-poll/answers/%s/" % poll_url) tags = p.tags.all() vars["tags"] = tags vars["poll"] = p if p.description and p.description != "": vars["poll_desc"] = p.description if p.link: vars["domain"] = urlparse(p.link)[1] if p.total_votes == 0: vars["noVotes"] = True # get the poll files pf = PollFile.objects.filter(poll=p) #@UndefinedVariable vars["pollFiles"] = pollService.get_poll_file_w_icons(pf) # check for content box if len(pf) > 0 or p.video_link or p.link: vars["showContentBox"] = True # do not show voting form if user has already voted form = None results = pollService.get_poll_results(p, drop_zero_results=False) # user is logged in if request.user.is_authenticated(): # check to see if user is watching this us = UserService(request.user) data = us.getUserData() if data.polls_watched.filter(poll=p).count() > 0: vars["watching"] = True if request.user == p.user: vars["creator"] = True # get the answer forms try: # check if user has voted poll_vote = PollVote.objects.get( poll=p, user=request.user) #@UndefinedVariable vars['your_answer'] = poll_vote.poll_answer.answer except PollVote.DoesNotExist: #@UndefinedVariable # they haven't so show them the answer form form = poll_answer_style_factory(consts.PREDEFINED_ANSWERS, p, results) except PollVote.MultipleObjectsReturned: #@UndefinedVariable # oh jesus, somehow they have multiple votes, log it and take their first one poll_vote = PollVote.objects.filter( poll=p, user=request.user)[0] #@UndefinedVariable logging.getLogger("PollView").critical( "user has more than one vote for a poll id: %d and user: %s." % (p.id, request.user.username)) else: form = poll_answer_style_factory(consts.PREDEFINED_ANSWERS, p, results) # open flash chart demographic_url_param = '' if demographics: demographic_url_param = demographics ansForm = AnswerForm() vars["answerForm"] = ansForm vars['form'] = form # poll display type chart_type = consts.DEFAULT_CHART_TYPE vars["link_in_new_window"] = True if request.user.is_authenticated(): data = UserService(request.user).getUserData() vars["link_in_new_window"] = data.link_in_new_window chart_type = data.default_chart_type # get the gchart vars["chart_type"] = chart_type gcf = GChartFactory() res_w_votes = [] res_wo_votes = [] # grab only the results with votes to display on the chart for r in results: if r.votes > 0: res_w_votes.append(r) else: res_wo_votes.append(r) pchart = gcf.get_chart(res_w_votes) vars["gChart"] = pchart.get_url() vars["results"] = res_w_votes vars['results_wo_votes'] = res_wo_votes # create description tag: description = "" description += "Is it: " for i in range(0, len(vars["results"])): a = vars["results"][i] if isinstance(a.answer, PollAnswer): description += a.answer.answer + ", " if i + 1 == len(vars["results"]) - 1: description += "or " description = description.rstrip(", ") description += "? " if p.description: description += p.description vars["meta_desc"] = description if p.link: vars['domain'] = urlparse(p.link) return base.render(request, "pollview.html", vars)