Esempio n. 1
0
from flask_pw import Peewee
from flask_debugtoolbar import DebugToolbarExtension
import app.helpers as helpers

app = Flask(__name__)
app.jinja_env.globals.update(html=helpers)
app.jinja_env.filters['currency'] = helpers.format_currency
app.jinja_env.filters['date'] = helpers.format_date
app.jinja_env.filters['time'] = helpers.format_time
app.jinja_env.filters['datetime'] = helpers.format_datetime

app.config.update(prod)
if prod['DEBUG'] is True or prod['DEBUG'] is None:
    app.config.update(dev)
    app.debug = True
db = Peewee(app)
#toobar = DebugToolbarExtension(app)
if db:
    import app.models as models

app.cli.add_command(db.cli, 'db')

for name in find_modules('app.blueprints'):
    mod = import_string(name)
    if hasattr(mod, 'blueprint'):
        app.register_blueprint(mod.blueprint)


@app.before_request
def before_request():
    g.startTime = datetime.now()
from flask import Flask

from flask_bootstrap import Bootstrap
from flask_moment import Moment
from flask_pw import Peewee
from flask_login import LoginManager

from config import config


bootstrap = Bootstrap()
moment = Moment()
db = Peewee()

login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'auth.login'


def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)
    app.config['BOOTSTRAP_SERVE_LOCAL'] = True

    bootstrap.init_app(app)
    moment.init_app(app)
    db.init_app(app)
    app.cli.add_command(db.cli, 'db')
    login_manager.init_app(app)
Esempio n. 3
0
def create_app(config, app=None):
    class Result(object):
        def __init__(self, **kwargs):
            for k, v in kwargs.items():
                setattr(self, k, v)

    if app is None:
        app = Flask(__name__)
        app.config.from_object(config)

    app.admin = Admin(
        name='App',
        # base_template='admin_master.html',
        template_mode='bootstrap3',
        index_view=AdminIndexView(
            # template='admin/my_index.html',
        ),
    )
    app.collect = Collect()
    app.db = Peewee(app)
    db.db = app.db
    app.blueprint = Blueprint(
        'app',
        __name__,
        template_folder='templates',
        static_folder='static',
        static_url_path='/static/app',
    )
    app.register_blueprint(app.blueprint)

    from api import api_v0, api
    app.api = api
    app.register_blueprint(api_v0)
    app.register_blueprint(apidoc.apidoc)

    from .models.auth import User, Role, UserRoles
    app.user_datastore = PeeweeUserDatastore(
        app.db,
        User,
        Role,
        UserRoles,
    )
    app.security = Security(app, app.user_datastore)

    app.admin.init_app(app)

    def authenticate(username, password):
        try:
            user = User.get(email=username)
        except User.DoesNotExist:
            return None
        result = Result(
            id=user.id,
            email=user.email,
        )
        if verify_password(password, user.password):
            return result

    def identity(payload):
        try:
            user = User.get(id=payload['identity'])
        except User.DoesNotExist:
            user = None
        return user

    app.jwt = JWT(app, authenticate, identity)

    from .api import auth, gallery, event, user

    return app