comparison piecrust/admin/web.py @ 778:5e91bc0e3b4d

internal: Move admin panel code into the piecrust package.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 16 Jul 2016 15:02:24 +0200
parents
children a9f4a6e60b0b
comparison
equal deleted inserted replaced
777:8d633ca59bc5 778:5e91bc0e3b4d
1 import os.path
2 import logging
3 from flask import Flask
4 from werkzeug import SharedDataMiddleware
5 from .blueprint import foodtruck_bp, login_manager, bcrypt_ext
6 from .configuration import FoodTruckConfigNotFoundError
7 from .sites import InvalidSiteError
8
9
10 logger = logging.getLogger(__name__)
11
12
13 def create_foodtruck_app(extra_settings=None):
14 app = Flask(__name__)
15 app.config.from_object('piecrust.admin.settings')
16 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True)
17 if extra_settings:
18 app.config.update(extra_settings)
19
20 admin_root = app.config.setdefault('FOODTRUCK_ROOT', os.getcwd())
21 config_path = os.path.join(admin_root, 'app.cfg')
22
23 # If we're being run as the `chef admin run` command, from inside a PieCrust
24 # website, do a few things differently.
25 app.config['FOODTRUCK_PROCEDURAL_CONFIG'] = None
26 if (app.config.get('FOODTRUCK_CMDLINE_MODE', False) and
27 os.path.isfile(os.path.join(admin_root, 'config.yml'))):
28 app.secret_key = os.urandom(22)
29 app.config['LOGIN_DISABLED'] = True
30 app.config['FOODTRUCK_PROCEDURAL_CONFIG'] = {
31 'sites': {
32 'local': admin_root}
33 }
34
35 # Add a special route for the `.well-known` directory.
36 app.wsgi_app = SharedDataMiddleware(
37 app.wsgi_app,
38 {'/.well-known': os.path.join(admin_root, '.well-known')})
39
40 if os.path.isfile(config_path):
41 app.config.from_pyfile(config_path)
42
43 if app.config['DEBUG']:
44 l = logging.getLogger()
45 l.setLevel(logging.DEBUG)
46 else:
47 @app.errorhandler(FoodTruckConfigNotFoundError)
48 def _on_config_missing(ex):
49 return render_template('install.html')
50
51 @app.errorhandler(InvalidSiteError)
52 def _on_invalid_site(ex):
53 data = {'error': "The was an error with your configuration file: %s" %
54 str(ex)}
55 return render_template('error.html', **data)
56
57 _missing_secret_key = False
58 if not app.secret_key:
59 # If there's no secret key, create a temp one but mark the app as not
60 # correctly installed so it shows the installation information page.
61 app.secret_key = 'temp-key'
62 _missing_secret_key = True
63
64 # Register extensions and blueprints.
65 login_manager.init_app(app)
66 bcrypt_ext.init_app(app)
67 app.register_blueprint(foodtruck_bp)
68
69 logger.debug("Created FoodTruck app with admin root: %s" % admin_root)
70
71 return app
72