Beispiel #1
0
    def view(request):
        if request.method == 'POST':
            # Get comments model
            model_name = request.POST['model']
            model = get_vote_model(model_name)

            object_id = request.POST['object_id']
            # View
            result = func(request, model, object_id)

            if request.is_ajax():
                data = {
                    "upvotes": model.objects.filter(object__id=object_id, value__gt=0).count(),
                    "downvotes": model.objects.filter(object__id=object_id, value__lt=0).count()
                }
                return HttpResponse(simplejson.dumps(data), content_type="application/json")


            if result:
                return result
            else:
                # ... and redirect to next.
                if 'next' in request.REQUEST:
                    return HttpResponseRedirect(request.REQUEST['next'])
                else:
                    return HttpResponse('OK')
        else:
            # Default response: 403
            return HttpResponseForbidden()
Beispiel #2
0
def rating_result(request, model_name, object_id):
    """
    Display the average rating of an item
    """

    model = get_vote_model(model_name)

    # Get your own vote
    your_vote = model.objects.filter(voter=request.user)

    if your_vote:
        your_vote = your_vote[0]

    # Extract the object what these votes are about. (There's gotta be a better way to do this)
    object = model.objects.select_related('object').filter(object__id=object_id)[:1][0].object

    # Get the average rating
    rating = model.objects.filter(object__id=object_id).aggregate(value=Avg('value'))

    context = {'model_name': model_name,
               'object': object,
               'rating': rating['value'],
               'your_vote': your_vote}

    return render_to_response('django_votes/rating.html',
                              context,
                              context_instance=RequestContext(request))
Beispiel #3
0
def rating_result(request, model_name, object_id):
    """
    Display the average rating of an item
    """

    model = get_vote_model(model_name)

    # Get your own vote
    your_vote = model.objects.filter(voter=request.user)

    if your_vote:
        your_vote = your_vote[0]

    # Extract the object what these votes are about. (There's gotta be a better way to do this)
    object = model.objects.select_related('object').filter(object__id=object_id)[:1][0].object

    # Get the average rating
    rating = model.objects.filter(object__id=object_id).aggregate(value=Avg('value'))

    context = {'model_name': model_name,
               'object': object,
               'rating': rating['value'],
               'your_vote': your_vote}

    return render_to_response('django_votes/rating.html',
                              context,
                              context_instance=RequestContext(request))
Beispiel #4
0
    def get_info(self, context):
        v = Variable(self.object)
        object = v.resolve(context)

        self.model_name = '%s.%sVote' % (object._meta.app_label,
                                         object._meta.object_name,)

        self.model = get_vote_model(self.model_name)

        return object
Beispiel #5
0
def updownvote_result(request, model_name, object_id):
    """
    Display the likes and dislikes of an item
    """

    model = get_vote_model(model_name)

    # Get your own vote
    your_vote = model.objects.filter(voter=request.user)

    if your_vote:
        your_vote = your_vote[0]

    # Extract the object what these votes are about. (There's gotta be a better way to do this)
    object = model.objects.select_related('object').filter(object__id=object_id)[:1][0].object

    # Get the likes
    up_votes = model.objects.filter(object__id=object_id,
                                    value__gt=0).count()
    # Get the dislikes
    down_votes = model.objects.filter(object__id=object_id,
                                      value__lt=0).count()
    # Get the total votes
    total_votes = model.objects.filter(object__id=object_id).count()

    # Calculate the percentages in order to fill the bars
    up_pct = (float(up_votes) / float(total_votes) if total_votes else 0) * 98
    down_pct = (float(down_votes) / float(total_votes) if total_votes else 0) * 98

    context = {'model_name': model_name,
               'object': object,
               'up_votes': up_votes,
               'down_votes': down_votes,
               'your_vote': your_vote,
               'total_votes': total_votes,
               'up_pct': up_pct,
               'down_pct': down_pct}

    return render_to_response('django_votes/updownvote.html',
                              context,
                              context_instance=RequestContext(request))
Beispiel #6
0
def updownvote_result(request, model_name, object_id):
    """
    Display the likes and dislikes of an item
    """

    model = get_vote_model(model_name)

    # Get your own vote
    your_vote = model.objects.filter(voter=request.user)

    if your_vote:
        your_vote = your_vote[0]

    # Extract the object what these votes are about. (There's gotta be a better way to do this)
    object = model.objects.select_related('object').filter(object__id=object_id)[:1][0].object

    # Get the likes
    up_votes = model.objects.filter(object__id=object_id,
                                    value__gt=0).count()
    # Get the dislikes
    down_votes = model.objects.filter(object__id=object_id,
                                      value__lt=0).count()
    # Get the total votes
    total_votes = model.objects.filter(object__id=object_id).count()

    # Calculate the percentages in order to fill the bars
    up_pct = (float(up_votes) / float(total_votes) if total_votes else 0) * 98
    down_pct = (float(down_votes) / float(total_votes) if total_votes else 0) * 98

    context = {'model_name': model_name,
               'object': object,
               'up_votes': up_votes,
               'down_votes': down_votes,
               'your_vote': your_vote,
               'total_votes': total_votes,
               'up_pct': up_pct,
               'down_pct': down_pct}

    return render_to_response('django_votes/updownvote.html',
                              context,
                              context_instance=RequestContext(request))
Beispiel #7
0
    def view(request):
        if request.method == 'POST':
            # Get comments model
            model_name = request.POST['model']
            model = get_vote_model(model_name)

            object_id = request.POST['object_id']
            # View
            result = func(request, model, object_id)

            if result:
                return result
            else:
                # ... and redirect to next.
                if 'next' in request.REQUEST:
                    return HttpResponseRedirect(request.REQUEST['next'])
                else:
                    return HttpResponse('OK')
        else:
            # Default response: 403
            return HttpResponseForbidden()