예제 #1
0
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')
예제 #2
0
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') 
예제 #3
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)
예제 #4
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)