comparison piecrust/admin/web.py @ 886:dcdec4b951a1

admin: Get the admin panel working again.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 20 Jun 2017 21:13:08 -0700
parents 82509bce94ca
children ca357249a321
comparison
equal deleted inserted replaced
885:13e8b50a2113 886:dcdec4b951a1
1 import os.path 1 import os.path
2 import logging 2 import logging
3 from flask import Flask, render_template 3 from flask import Flask
4 from werkzeug import SharedDataMiddleware 4 from werkzeug import SharedDataMiddleware
5 from .blueprint import foodtruck_bp
6 from .configuration import FoodTruckConfigNotFoundError
7 from .sites import InvalidSiteError
8 5
9 6
10 logger = logging.getLogger(__name__) 7 logger = logging.getLogger(__name__)
11 8
12 9
13 def create_foodtruck_app(extra_settings=None): 10 def create_foodtruck_app(extra_settings=None):
11 from .blueprint import foodtruck_bp
12
14 app = Flask(__name__.split('.')[0], static_folder=None) 13 app = Flask(__name__.split('.')[0], static_folder=None)
15 app.config.from_object('piecrust.admin.settings') 14 app.config.from_object('piecrust.admin.settings')
16 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True) 15 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True)
17 if extra_settings: 16 if extra_settings:
18 app.config.update(extra_settings) 17 app.config.update(extra_settings)
19 18
20 admin_root = app.config.setdefault('FOODTRUCK_ROOT', os.getcwd()) 19 root_dir = 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
24 # PieCrust 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 20
35 # Add a special route for the `.well-known` directory. 21 # Add a special route for the `.well-known` directory.
36 app.wsgi_app = SharedDataMiddleware( 22 app.wsgi_app = SharedDataMiddleware(
37 app.wsgi_app, 23 app.wsgi_app,
38 {'/.well-known': os.path.join(admin_root, '.well-known')}) 24 {'/.well-known': os.path.join(root_dir, '.well-known')})
39 25
40 if os.path.isfile(config_path): 26 # Setup logging/error handling.
41 app.config.from_pyfile(config_path)
42
43 if app.config['DEBUG']: 27 if app.config['DEBUG']:
44 l = logging.getLogger() 28 l = logging.getLogger()
45 l.setLevel(logging.DEBUG) 29 l.setLevel(logging.DEBUG)
46 else:
47 @app.errorhandler(FoodTruckConfigNotFoundError)
48 def _on_config_missing(ex):
49 return render_template('install.html')
50 30
51 @app.errorhandler(InvalidSiteError) 31 if not app.config['SECRET_KEY']:
52 def _on_invalid_site(ex):
53 data = {'error':
54 "The was an error with your configuration file: %s" %
55 str(ex)}
56 return render_template('error.html', **data)
57
58 if not app.secret_key:
59 # If there's no secret key, create a temp one but mark the app as not 32 # 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. 33 # correctly installed so it shows the installation information page.
61 app.secret_key = 'temp-key' 34 app.config['SECRET_KEY'] = 'temp-key'
62 35
63 # Register extensions and blueprints. 36 # Register extensions and blueprints.
64 app.register_blueprint(foodtruck_bp) 37 bp_prefix = app.config['FOODTRUCK_URL_PREFIX']
38 app.register_blueprint(foodtruck_bp, url_prefix=bp_prefix)
65 39
66 logger.debug("Created FoodTruck app with admin root: %s" % admin_root) 40 logger.debug("Created FoodTruck app with admin root: %s" % root_dir)
67 41
68 return app 42 return app
69 43