~homeworkprod/byceps

5474d378d14883d1b1723d594969aeec892040c5 — Jochen Kupperschmidt 3 years ago fef102a
Merge manage script into `app.py` to enable static file serving and debug toolbar for development purposes, drop it, update README
3 files changed, 32 insertions(+), 53 deletions(-)

M README.rst
M app.py
D manage.py
M README.rst => README.rst +1 -1
@@ 67,7 67,7 @@ debugging middleware and in-browser code evaluation:

.. code:: sh

    $ BYCEPS_CONFIG=../config/development_admin.py ./manage.py runserver -p 8080
    $ BYCEPS_CONFIG=../config/development_admin.py FLASK_ENV=development flask run -p 8080

In a production environment, it is recommended to have the application
served by uWSGI_ or Gunicorn_.

M app.py => app.py +31 -0
@@ 6,7 6,11 @@ application instance
:License: Modified BSD, see LICENSE for details.
"""

from werkzeug.wsgi import SharedDataMiddleware

from byceps.application import create_app, init_app
from byceps.config import STATIC_URL_PREFIX_BRAND, STATIC_URL_PREFIX_GLOBAL, \
    STATIC_URL_PREFIX_PARTY
from byceps.database import db
from byceps.services.brand.models.brand import Brand
from byceps.services.party.models.party import Party


@@ 29,6 33,33 @@ app = create_app(config_filename)
init_app(app)


def _assemble_exports():
    exports = {}

    _export_path_if_configured(exports, 'PATH_GLOBAL', STATIC_URL_PREFIX_GLOBAL)
    _export_path_if_configured(exports, 'PATH_BRAND', STATIC_URL_PREFIX_BRAND)
    _export_path_if_configured(exports, 'PATH_PARTY', STATIC_URL_PREFIX_PARTY)

    return exports


def _export_path_if_configured(exports, config_key, url_path):
    path = app.config.get(config_key)
    if path:
        exports[url_path] = str(path)


if app.env == 'development':
    # Share static files.
    exports = _assemble_exports()
    app.wsgi_app = SharedDataMiddleware(app.wsgi_app, exports)

    # Enable debug toolbar.
    from flask_debugtoolbar import DebugToolbarExtension
    app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
    toolbar = DebugToolbarExtension(app)


@app.shell_context_processor
def extend_shell_context():
    """Provide common objects to make available in the application shell."""

D manage.py => manage.py +0 -52
@@ 1,52 0,0 @@
#!/usr/bin/env python
"""
management script
~~~~~~~~~~~~~~~~~

Run the application, take administrative action.

:Copyright: 2006-2018 Jochen Kupperschmidt
:License: Modified BSD, see LICENSE for details.
"""

from werkzeug.wsgi import SharedDataMiddleware

from byceps.application import create_app, init_app
from byceps.config import STATIC_URL_PREFIX_BRAND, STATIC_URL_PREFIX_GLOBAL, \
    STATIC_URL_PREFIX_PARTY
from byceps.util.system import get_config_filename_from_env_or_exit


config_filename = get_config_filename_from_env_or_exit()

app = create_app(config_filename)
init_app(app)


def _assemble_exports():
    exports = {}

    _export_path_if_configured(exports, 'PATH_GLOBAL', STATIC_URL_PREFIX_GLOBAL)
    _export_path_if_configured(exports, 'PATH_BRAND', STATIC_URL_PREFIX_BRAND)
    _export_path_if_configured(exports, 'PATH_PARTY', STATIC_URL_PREFIX_PARTY)

    return exports


def _export_path_if_configured(exports, config_key, url_path):
    path = app.config.get(config_key)
    if path:
        exports[url_path] = str(path)


if app.debug:
    exports = _assemble_exports()
    app.wsgi_app = SharedDataMiddleware(app.wsgi_app, exports)

    from flask_debugtoolbar import DebugToolbarExtension
    app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
    toolbar = DebugToolbarExtension(app)


if __name__ == '__main__':
    app.run()