Example #1
0
# -*- coding: utf-8 -*-
"""
Shikin, political donations database.
"""

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.babel import Babel

from . import config

# create our little application
app = Flask(config.APP_NAME)
app.config.from_object(config)
app.config.from_envvar("SHIKIN_SETTINGS", silent=True)
app.dbobj = SQLAlchemy(app)
app.babel = Babel(app)

from . import views
from . import api
from . import pageapi
from . import review
Example #2
0
def create_app(config_filename=None):
    app = Flask(__name__)
    app.request_class = Request
    app.url_map.converters["re"] = RegexConverter

    if config_filename is None:
        HerokuConfig(app)
    else:
        AppConfig(app, config_filename)

    from_envvars(app.config, prefix="")
    app.debug = app.config.get("DEBUG")

    # TODO: these variables probably aren't unjsonnable anymore

    # push all variables into the environment
    unjsonnable = (datetime, timedelta)
    data = {k: json.dumps(v) for k, v in app.config.items() if not isinstance(v, unjsonnable)}
    os.environ.update(data)

    # app.logger.info('App is running on port %s', os.environ.get('PORT'))

    if app.config["DEBUG"] is not True:
        log_level = app.config.get("LOG_LEVEL", "DEBUG")
        app.logger.setLevel(getattr(logging, log_level.upper()))

    import bauble.db as db

    if "LOG_SQL" in os.environ:
        logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)

    db.init_app(app)

    # register flask extensionsa
    SSLify(app, permanent=True)
    app.login_manager = LoginManager(app)
    app.login_manager.login_view = "auth.login"
    app.mail = Mail(app)
    app.babel = Babel(app)

    from .assets import init_app

    init_app(app)

    # TODO: just import everything controllers

    for controller in ["auth", "index", "batch"]:
        module = import_module("bauble.controllers.{}".format(controller))
        app.register_blueprint(module.blueprint)

    from bauble.resource import Resource

    controllers = ["search", "family", "genus", "taxon", "accession", "plant", "location", "vernacular_name"]
    for controller in controllers:
        module = import_module("bauble.controllers.{}".format(controller))
        for attr_name in dir(module):
            attr = getattr(module, attr_name)
            # find all the blueprints in the files
            if isinstance(attr, Blueprint):
                app.register_blueprint(attr)
            # if isclass(attr) and issubclass(attr, Blueprint) and attr != Resource:
            #     app.register_blueprint(attr())

    from bauble.error import init_errorhandlers

    init_errorhandlers(app)

    app.json_encoder = JSONEncoder

    return app