Ejemplo n.º 1
0
def get_init_api():

    if app.config['ADMIN_TOKEN'] != request.args.get('token'):
        abort(404)

    db_uri = app.config['SQLALCHEMY_DATABASE_URI']

    if database_exists(db_uri):
        return handle_response({
            'message': 'db already exists',
        }, 422)

    ## Create DB
    create_database(db_uri)

    ## Migrate table structure
    migrate = Migrate()
    migrate.init_app(app, db)

    from os.path import dirname, abspath
    d = "%s/migrations" % dirname(dirname(dirname(abspath(__file__))))

    upgrade(directory=d)

    return handle_response({
        'status': 'ok',
    })
Ejemplo n.º 2
0
def create_app(config_key='local'):
    app = Flask(__name__)
    # Enabling config initiation
    app.config.from_object(config_dict[config_key])
    config_dict[config_key].init_app(app)
    db = SQLAlchemy(app)
    db.init_app(app)
    migrate = Migrate(app, db)
    migrate.init_app(app, db)
    # register api
    from api import api as api_blueprint
    app.register_blueprint(api_blueprint)
    return app, db, migrate
Ejemplo n.º 3
0
def create_app():
    app = Flask(__name__)

    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///creditcard.db'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    from app.model import Card

    config_db(app)
    # migrate =  Migrate(app, app.db)
    migrate = Migrate()

    migrate.init_app(app, app.db)

    from .cards import bp_cards
    app.register_blueprint(bp_cards)

    return app
Ejemplo n.º 4
0
def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)
    db.init_app(app)
    migrate.init_app(app, db, render_as_batch=True)
    login.init_app(app)
    bootstrap.init_app(app)
    ma.init_app(app)

    from app.admin import admin as admin_blueprint

    app.register_blueprint(admin_blueprint, url_prefix="/admin")

    from app.api import api as api_blueprint

    app.register_blueprint(api_blueprint, url_prefix="/api")

    from app.auth import auth as auth_blueprint

    app.register_blueprint(auth_blueprint)

    from app import views, models

    return app
Ejemplo n.º 5
0
def create_app(config_name):

    from app.models import Bucketlist, User

    app = FlaskAPI(__name__, instance_relative_config=True)
    # overriding Werkzeugs built-in password hashing utilities using Bcrypt.
    bcrypt = Bcrypt(app)

    app.config.from_object(app_config[config_name])
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
    db.init_app(app)
    migrate = Migrate(db, app)
    migrate.init_app(app, db)

    @app.route('/bucketlists/', methods=['POST', 'GET'])
    def bucketlists():
        # get the access token
        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                # Go ahead and handle the request, the user is authed
                if request.method == "POST":
                    name = str(request.data.get('name', ''))
                    if name:
                        bucketlist = Bucketlist(name=name, created_by=user_id)
                        bucketlist.save()
                        response = jsonify({
                            'id': bucketlist.id,
                            'name': bucketlist.name,
                            'date_created': bucketlist.date_created,
                            'date_modified': bucketlist.date_modified,
                            'created_by': user_id
                        })

                        return make_response(response), 201

                else:
                    # GET
                    # get all the bucketlists for this user
                    bucketlists = Bucketlist.get_all(user_id)
                    results = []

                    for bucketlist in bucketlists:
                        obj = {
                            'id': bucketlist.id,
                            'name': bucketlist.name,
                            'date_created': bucketlist.date_created,
                            'date_modified': bucketlist.date_modified,
                            'created_by': bucketlist.created_by
                        }
                        results.append(obj)

                    return make_response(jsonify(results)), 200
            else:
                # user is not legit, so the payload is an error message
                message = user_id
                response = {'message': message}
                return make_response(jsonify(response)), 401

    @app.route('/bucketlists/<int:id>', methods=['GET', 'PUT', 'DELETE'])
    def bucketlist_manipulation(id, **kwargs):

        auth_header = request.headers.get('Authorization')
        access_token = auth_header.split(" ")[1]

        if access_token:
            user_id = User.decode_token(access_token)
            if not isinstance(user_id, str):
                bucketlist = Bucketlist.query.filter_by(id=id).first()
                if not bucketlist:
                    # Raise an HTTPException with a 404 not found status code
                    abort(404)

                if request.method == "DELETE":
                    bucketlist.delete()
                    return {
                        "message":
                        "bucketlist {} deleted".format(bucketlist.id)
                    }, 200
                elif request.method == 'PUT':
                    name = str(request.data.get('name', ''))
                    bucketlist.name = name
                    bucketlist.save()
                    response = {
                        'id': bucketlist.id,
                        'name': bucketlist.name,
                        'date_created': bucketlist.date_created,
                        'date_modified': bucketlist.date_modified,
                        'created_by': bucketlist.created_by
                    }
                    return make_response(jsonify(response)), 200
                else:
                    # GET
                    response = jsonify({
                        'id': bucketlist.id,
                        'name': bucketlist.name,
                        'date_created': bucketlist.date_created,
                        'date_modified': bucketlist.date_modified,
                        'created_by': bucketlist.created_by
                    })
                    return make_response(response), 200
            else:
                # user is not legit, so the payload is an error message
                message = user_id
                response = {'message': message}
                return make_response(jsonify(response)), 401

    # import the authentication blueprint and register it on the app
    from .auth import auth_blueprint
    app.register_blueprint(auth_blueprint)

    return app