def init_db():
    # import all modules here that might define models so that
    # they will be registered properly on the metadata.  Otherwise
    # you will have to import them first before calling init_db()
    db.create_all()
Beispiel #2
0
def create_app(extra_config=None):
    """Create Flask app for Flaskapp
    """
    app = Flask('daspanel_web',
                template_folder='templates/default',
                static_folder='templates/default/static')

    app.config.from_object('config')
    app.config.update(**(extra_config or {}))
    app.before_request(before_request)

    # the toolbar is only enabled in debug mode:
    #app.debug = True
    #app.config['SECRET_KEY'] = os.environ.get('DASPANEL_SYS_UUID', os.urandom(25).encode('hex'))
    #app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
    #app.config['DEBUG_TB_PROFILER_ENABLED'] = True
    if app.config['DEBUG'] == True:
        print(" * [DASPANEL] Debug Toolbar enabled")
        toolbar = DebugToolbarExtension()
        toolbar.init_app(app)
    else:
        print(" * [DASPANEL] Debug Toolbar disabled")

    # import static file manifest
    js = pkg_resources.resource_string(
        __name__, '/templates/default/static/rev-manifest.json')
    app.config['static_manifest'] = json.loads(js)

    # configure jinja2
    app.jinja_env.globals.update({'h': template_helpers})

    # add Flask-WTForms CSRF Protection
    CSRFProtect(app)

    # init Flask-SQLAlchemy
    db.init_app(app)
    if not os.path.exists(
            os.path.join(app.config['DASPANEL_DATADIR'], 'db',
                         app.config['APP_DATABASE'])):
        print("Creating database file: " + os.path.join(
            app.config['DASPANEL_DATADIR'], 'db', app.config['APP_DATABASE']))
        with app.app_context():
            db.create_all()

    with app.app_context():
        update_or_create_admin(app.config['DASPANEL']["sys"]["admin"],
                               app.config['DASPANEL']["sys"]["password"])

    # init Flask-Principal
    Principal(app)
    identity_loaded.connect(on_identity_loaded, app)

    # init Flask-Login
    lm.init_app(app)
    lm.login_view = 'auth.login'
    lm.user_loader(load_user)

    # init Flask-Mail
    mail.init_app(app)

    # register blueprints
    app.register_blueprint(content.bp)
    app.register_blueprint(auth.bp, url_prefix='/auth')
    app.register_blueprint(sites.bp, url_prefix='/sites')
    app.register_blueprint(module1.bp, url_prefix='/module1')
    app.register_blueprint(services.bp, url_prefix='/services')
    app.register_blueprint(system.bp, url_prefix='/system')

    return app