def toggle_bookmark()

in pulseapi/entries/views.py [0:0]


def toggle_bookmark(request, entryid, **kwargs):
    """
    Toggle whether or not this user "bookmarked" the url-indicated entry.
    This is currently defined outside of the entry class, as functionality
    that is technically independent of entries themselves. We might
    change this in the future.
    """
    user = request.user

    if user.is_authenticated:
        profile = user.profile

        entry = get_object_or_404(Entry, id=entryid)

        # find out if there is already a {user,entry,(timestamp)} triple
        bookmarks = entry.bookmarked_by.filter(profile=profile)

        # if there is a bookmark, remove it. Otherwise, make one.
        if bookmarks:
            bookmarks.delete()
        else:
            UserBookmarks.objects.create(entry=entry, profile=profile)

        return Response("Toggled bookmark.", status=status.HTTP_204_NO_CONTENT)
    return Response("Anonymous bookmarks cannot be saved.", status=status.HTTP_403_FORBIDDEN)