Exemplo n.º 1
0
def ajax_fav(request, ctype_id, obj_id):
    """
    
    """
    ctype = get_object_or_404(ContentType, pk=ctype_id)
    item = ctype.get_object_for_this_type(pk=obj_id)    
    if Favorite.objects.filter(user=request.user, content_type=ctype, object_id=obj_id):
        fav = Favorite.objects.get(user=request.user, content_type=ctype, object_id=obj_id)
        fav.delete()
        count = Favorite.objects.favorites_for_object(item).count()
        data_dict = {'id': 0, 'message': fav_settings.FAV_ADD, 'counter': build_message(count), }
    else:        
        fav = Favorite.objects.create_favorite(item, request.user)
        count = Favorite.objects.favorites_for_object(item).count()
        data_dict = {'id': fav.id, 'message': fav_settings.FAV_REMOVE, 'counter': build_message(count), }
    return HttpResponse(simplejson.dumps(data_dict), mimetype='application/javascript')
def fav_item(item, user):
    """"""
    favs = Favorite.objects.favorites_for_object(item)
    users = [fav.user for fav in favs]
    count = len(users)
    counter = build_message(count)
    faved = False
    message = fav_settings.FAV_ADD
    if user.is_authenticated():
        if Favorite.objects.favorites_for_object(item, user):
            faved = True
            message = fav_settings.FAV_REMOVE
    ctype = ContentType.objects.get_for_model(item)
    return {'faved': faved, 'message': message, 'users': users, 'counter': counter, 'item': item, 'ctype': ctype }
Exemplo n.º 3
0
def ajax_fav(request, ctype_id, obj_id):
    """
    
    """
    ctype = get_object_or_404(ContentType, pk=ctype_id)
    item = ctype.get_object_for_this_type(pk=obj_id)    
    if Favorite.objects.filter(user=request.user, content_type=ctype, object_id=obj_id):
        fav = Favorite.objects.get(user=request.user, content_type=ctype, object_id=obj_id)
        fav.delete()
        count = Favorite.objects.favorites_for_object(item).count()
        data_dict = {'id': 0, 'message': '<img src="/static/img/staroff.png" alt="Favourite"/>'+"Insertar en Favoritos", 'counter': build_message(count), }
    else:        
        fav = Favorite.objects.create_favorite(item, request.user)
        count = Favorite.objects.favorites_for_object(item).count()
        data_dict = {'id': fav.id, 'message': '<img src="/static/img/staron.png" alt="Favourite"/>'+"Quitar de Favoritos", 'counter': build_message(count), }
    return HttpResponse(simplejson.dumps(data_dict), mimetype='application/javascript')