"""API endpoints for bookmarks."""

from flask import request, jsonify, Blueprint

from bookmarks import csrf
from bookmarks.models import Bookmark, BookmarkSchema, VoteSchema
from bookmarks.forms import AddBookmarkForm, UpdateBookmarkForm
from bookmarks.logic import _get, _post, _put, _delete

from .auth import token_auth

bookmarks_api = Blueprint('bookmarks_api', __name__)
csrf.exempt(bookmarks_api)


@bookmarks_api.route('/api/bookmarks/')
@bookmarks_api.route('/api/bookmarks/<int:id>')
@token_auth.login_required
def get(id=None):
    """Return all bookmarks."""
    query = _get(id)
    if id is None:
        bookmarks = BookmarkSchema().dump(query.all(), many=True).data
        return jsonify(bookmarks=bookmarks), 200
    else:
        bookmark = Bookmark.query.get(id)
        if bookmark is None:
            return jsonify(message='Bookmark not found', status=404), 404
        return BookmarkSchema().jsonify(bookmark), 200

Beispiel #2
0
"""API endpoints for votes."""

from flask import request, Blueprint, url_for, g

from bookmarks import csrf

from .auth import token_auth
from ..logic import _post_vote, _put_vote, _delete_vote


votes_api = Blueprint('votes_api', __name__)
csrf.exempt(votes_api)


@votes_api.route('/api/vote', methods=['POST', 'PUT', 'DELETE'])
@token_auth.login_required
def vote():
    """Add a new vote for a bookmark."""
    bookmark_id = request.get_json().get('bookmark_id')
    if request.method == 'POST':
        response = _post_vote(bookmark_id)
        response.headers['Location'] = url_for(
            'bookmarks_api.get_votes', bookmark_id=bookmark_id,
            user_id=g.user.id, _external=True)
        return response
    elif request.method == 'PUT':
        return _put_vote(bookmark_id)
    else:
        return _delete_vote(bookmark_id)
"""API endpoints for favorites."""

from flask import request, Blueprint, jsonify, url_for, g

from bookmarks import csrf

from .auth import token_auth
from ..logic import _save, _unsave


favourites_api = Blueprint('favourites_api', __name__)
csrf.exempt(favourites_api)


@favourites_api.route('/api/save', methods=['POST'])
@token_auth.login_required
def save():
    """Save a bookmark to user's saved listings."""
    bookmark_id = request.args.get('bookmark_id')
    response = _save(bookmark_id)
    # FIXME below is wrong, it should be something like:
    # /users/3/favourites/1 OR /users/3/favourites and be a list with 1 entry
    response.headers['Location'] = url_for('users_api.get_favourites',
                                           id=g.user.id, _external=True)
    return response


@favourites_api.route('/api/unsave', methods=['DELETE'])
@token_auth.login_required
def unsave():
    """Un-save a bookmark from user's saved listings."""