from flask_api import FlaskAPI
from flask_sqlalchemy import SQLAlchemy
from flask import jsonify, request, abort


from instance.config import app_config

# Create db instance
db = SQLAlchemy()


def create_app(config_name):

    from app.models import Bucketlist

    app = FlaskAPI(__name__, instance_relative_config=True)
    app.config.from_object(app_config[config_name])
    app.config.from_pyfile('config.py')

    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    db.init_app(app)

    @app.route('/v1/bucketlists/', methods=['POST', 'GET'])
    def bucketlists():
        """Creation and retrieval of bucketlists"""
        if request.method == 'POST':
            name = str(request.data.get('name', ''))
            if name:
                bucketlist = Bucketlist(name=name)
                bucketlist.save()
                response = jsonify({
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified
                })
                response.status_code = 201
                return response
        else:
            # GET
            bucketlists = Bucketlist.get_all()
            results = []

            for bucketlist in bucketlists():
                obj = {
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified
                }
                results.append(obj)
            response = jsonify(results)
            response.status_code = 200
            return response

    @app.route('/v1/bucketlists/<int:id>', methods=['GET', 'PUT', 'DELETE'])
    def bucketlist_control(id, **kwargs):
        """Retrieval, Editing and Deletion of bucketlists"""
        bucketlist = Bucketlist.query.filter_by(id=id).first()
        if not bucketlist:
            # Raise an exception with a 404 code for not found
            abort(404)

        if request.method == 'DELETE':
            bucketlist.delete()
            return {
                "Bucketlist {} deleted successfully".format(bucketlist.id)
            }, 200

        elif request.method == 'PUT':
            name = str(request.data.get('name', ''))
            bucketlist.name = name
            bucketlist.save()
            response = jsonify({
                    'id': bucketlist.id,
                    'name': bucketlist.name,
                    'date_created': bucketlist.date_created,
                    'date_modified': bucketlist.date_modified
                })
            response.status_code = 200
            return response

    return app

from flask import Flask, Blueprint, abort
from flask_restplus import Api, Resource, fields
from flask_sqlalchemy import SQLAlchemy


from instance.config import app_config

# Create db instance
db = SQLAlchemy()

# Create v1 blueprint for api
api_v1 = Blueprint('api', __name__, url_prefix='/api/v1')

api = Api(api_v1, version='1.0', title='BucketList API',
          description='api to allow the creation & control of bucketlists')


ns = api.namespace('bucketlists', description='Bucketlists operations')
api.init_app(api_v1)

BUCKETLISTS = {}


def create_app(config_name):

    app = Flask(__name__, instance_relative_config=True)

    app.config.from_object(app_config[config_name])
    app.config.from_pyfile('config.py')

    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    app.register_blueprint(api_v1)
    db.init_app(app)

    return app


def abort_if_bucketlist_doesnt_exist(bucketlist_id):
    '''abort with 404 if bucketlist doesnt exist'''
    from app.models.bucketlist import Bucketlist
    bucketlist = Bucketlist.query.filter_by(id=bucketlist_id).first()
    if not bucketlist:
        abort(404)

Create blueprint for API Remove bucketlist API route functionality