Example #1
0
def like_toggle(request, content_type_id, object_id):
    content_type = get_object_or_404(ContentType, pk=content_type_id)
    obj = content_type.get_object_for_this_type(pk=object_id)

    if not request.user.has_perm("phileo.can_like", obj):
        return HttpResponseForbidden()

    like, liked = Like.like(request.user, content_type, object_id)

    if liked:
        object_liked.send(sender=Like, like=like, request=request)
    else:
        object_unliked.send(sender=Like, object=obj, request=request)

    if request.is_ajax():
        html_ctx = widget_context(request.user, obj)
        template = "phileo/_widget.html"
        if request.GET.get("t") == "b":
            template = "phileo/_widget_brief.html"
        data = {
            "html":
            render_to_string(template,
                             html_ctx,
                             context_instance=RequestContext(request)),
            "likes_count":
            html_ctx["like_count"],
            "liked":
            html_ctx["liked"],
        }
        return HttpResponse(json.dumps(data), content_type="application/json")

    return redirect(request.META.get("HTTP_REFERER", "/"))
Example #2
0
def like_toggle(request, content_type_id, object_id):
    content_type = get_object_or_404(ContentType, pk=content_type_id)
    obj = content_type.get_object_for_this_type(pk=object_id)

    if not request.user.has_perm("phileo.can_like", obj):
        return HttpResponseForbidden()

    like, liked = Like.like(request.user, content_type, object_id)

    if liked:
        object_liked.send(sender=Like, like=like, request=request)
    else:
        object_unliked.send(sender=Like, object=obj, request=request)

    if request.is_ajax():
        html_ctx = widget_context(request.user, obj)
        template = "phileo/_widget.html"
        if request.GET.get("t") == "b":
            template = "phileo/_widget_brief.html"
        data = {
            "html": render_to_string(
                template,
                html_ctx,
                context_instance=RequestContext(request)
            ),
            "likes_count": html_ctx["like_count"],
            "liked": html_ctx["liked"],
        }
        return HttpResponse(json.dumps(data), content_type="application/json")

    return redirect(request.META.get("HTTP_REFERER", "/"))
Example #3
0
def like_toggle(request, content_type_id, object_id):
    content_type = get_object_or_404(ContentType, pk=content_type_id)
    
    like, created = Like.objects.get_or_create(
        sender = request.user,
        receiver_content_type = content_type,
        receiver_object_id = object_id
    )
    
    if created:
        object_liked.send(sender=Like, like=like)
    else:
        like.delete()
        object_unliked.send(
            sender=Like,
            object=content_type.get_object_for_this_type(
                pk=object_id
            )
        )
    
    if request.is_ajax():
        return HttpResponse(json.dumps({
            "likes_count": Like.objects.filter(
                sender = request.user,
                receiver_content_type = content_type,
                receiver_object_id = object_id
            ).count()
        }), mimetype="application/json")
    
    return redirect(request.META["HTTP_REFERER"])
Example #4
0
def like_toggle(request, pk, content_type_id, object_id):
    user = get_object_or_404(User, pk=pk)
    content_type = get_object_or_404(ContentType, pk=content_type_id)
    
    like, created = Like.objects.get_or_create(
        sender = user,
        receiver_content_type = content_type,
        receiver_object_id = object_id
    )
    
    if created:
        object_liked.send(sender=Like, like=like)
    else:
        like.delete()
        object_unliked.send(
            sender=Like,
            object=content_type.get_object_for_this_type(
                pk=object_id
            )
        )
    
    if request.is_ajax():
        return HttpResponse(json.dumps({
            "status": "OK",
            "current": request.POST.get("opposite"),
            "opposite": request.POST.get("current")
        }))
    
    return redirect(request.META["HTTP_REFERER"])