Example #1
0
app = Flask(
    __name__,
    template_folder="themes/",
    static_folder="themes/" + theme["active"] + "/static/",
    static_url_path="/static",
)
app.config.from_object(Config)

app.wsgi_app = ProxyFix(app.wsgi_app)
db = SQLAlchemy(app)
lg = LoginManager()
lg.setup_app(app)

# Blueprints & shit
from flaskpress.ctx import objects, filters
from flaskpress.models import users
from flaskpress.views import default

from flaskpress.views.pages import mod as pages_bp
from flaskpress.views.posts import mod as posts_bp
from flaskpress.admin.view import mod as admin_bp

# Blueprints
app.register_blueprint(admin_bp)
app.register_blueprint(pages_bp)
app.register_blueprint(posts_bp)

# Tpls
app.jinja_options = Flask.jinja_options.copy()
app.jinja_options["loader"] = ModifiedLoader(app)
Example #2
0
def create_app(cyd=None):
    """Create the web interface WSGI application"""

    if cyd is None:
        cyd = cydra.Cydra()

    from flask import Flask
    from flaskext.csrf import csrf
    from cydra.web.themes import IThemeProvider, ThemedTemplateLoader, patch_static

    app = Flask(__name__)

    # register themes
    theme_providers = ExtensionPoint(IThemeProvider, component_manager=cyd)
    app.config["cydra_themes"] = dict([(theme.name, theme) for theme in theme_providers.get_themes()])

    default_theme = cyd.config.get("web").get("default_theme")
    if default_theme is not None and default_theme in app.config["cydra_themes"]:
        default_theme = app.config["cydra_themes"][default_theme]
        logger.debug("Default theme: %s", default_theme.name)
    else:
        default_theme = None
    theme_detector = ThemeDetector(default_theme)
    app.before_request(theme_detector)

    # replace default loader
    app.jinja_options = Flask.jinja_options.copy()
    app.jinja_options["loader"] = ThemedTemplateLoader(app)

    # patch static file resolver
    patch_static(app)

    # secret key for cookies
    app.secret_key = os.urandom(24)

    # consider the cydra instance to be a form of configuration
    # and therefore store it in the config dict.
    app.config["cydra"] = cyd

    # common views
    app.add_url_rule("/login", "login", login)

    # Add shorthands to context
    app.context_processor(add_shorthands_to_context)

    # load frontend and backend
    from cydra.web.frontend import blueprint as frontend_blueprint

    patch_static(frontend_blueprint, "frontend")
    # from cydra.web.admin import blueprint as admin_blueprint
    # patch_static(admin_blueprint, 'admin')

    app.register_blueprint(frontend_blueprint)
    # app.register_blueprint(admin_blueprint)

    # load additional blueprints
    pages = ExtensionPoint(IBlueprintProvider, component_manager=cyd)
    for bpprovider in pages:
        bp = bpprovider.get_blueprint()
        patch_static(bp, bp.name)
        app.register_blueprint(bp)

    # some utility template filters
    from cydra.web.filters import filters

    # map(app.template_filter(), filters)
    _ = [app.template_filter()(f) for f in filters]

    # prevent flask from handling exceptions
    app.debug = True

    # add CSRF protection
    csrf(app)

    # wrap in authentication middleware
    from cydra.web.wsgihelper import AuthenticationMiddleware

    app = AuthenticationMiddleware(cyd, app)

    # enable debugging for certain users
    debugusers = cyd.config.get("web").get("debug_users", [])
    if debugusers:
        from cydra.web.debugging import DebuggingMiddleware

        app = DebuggingMiddleware(app, debugusers)

    return app