Mercurial > piecrust2
comparison foodtruck/web.py @ 597:79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
* Use a `settings` object to configure Flask.
* Accept an `app.cfg` file in the admin folder to configure Flask.
* Get a proper first site name when the cookie isn't set yet.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Wed, 20 Jan 2016 21:39:18 -0800 |
parents | d4a01a023998 |
children | effbc78b5528 |
comparison
equal
deleted
inserted
replaced
596:e2c91ba44d6c | 597:79a31a3c947b |
---|---|
1 import os | |
2 import os.path | |
1 import logging | 3 import logging |
2 from flask import Flask, g, request, render_template | 4 from flask import Flask, g, request, render_template |
3 from .config import ( | 5 from .config import ( |
4 FoodTruckConfigNotFoundError, get_foodtruck_config) | 6 FoodTruckConfigNotFoundError, get_foodtruck_config) |
5 from .sites import FoodTruckSites | 7 from .sites import FoodTruckSites |
6 | 8 |
7 | 9 |
8 logger = logging.getLogger(__name__) | 10 logger = logging.getLogger(__name__) |
9 | 11 |
10 app = Flask(__name__) | 12 app = Flask(__name__) |
13 app.config.from_object('foodtruck.settings') | |
14 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True) | |
11 | 15 |
12 c = get_foodtruck_config() | 16 admin_root = app.config['FOODTRUCK_ROOT'] or os.getcwd() |
13 app.secret_key = c.get('foodtruck/secret_key') | 17 config_path = os.path.join(admin_root, 'app.cfg') |
14 del c | 18 if os.path.isfile(config_path): |
19 app.config.from_pyfile(config_path) | |
20 | |
21 if app.config['DEBUG']: | |
22 l = logging.getLogger() | |
23 l.setLevel(logging.DEBUG) | |
24 | |
25 app.logger.debug("Using FoodTruck admin root: %s" % admin_root) | |
15 | 26 |
16 | 27 |
17 def after_this_request(f): | 28 def after_this_request(f): |
18 if not hasattr(g, 'after_request_callbacks'): | 29 if not hasattr(g, 'after_request_callbacks'): |
19 g.after_request_callbacks = [] | 30 g.after_request_callbacks = [] |
34 return getattr(self._something, name) | 45 return getattr(self._something, name) |
35 | 46 |
36 | 47 |
37 @app.before_request | 48 @app.before_request |
38 def _setup_foodtruck_globals(): | 49 def _setup_foodtruck_globals(): |
50 def _get_config(): | |
51 return get_foodtruck_config(admin_root) | |
52 | |
39 def _get_sites(): | 53 def _get_sites(): |
40 s = FoodTruckSites(g.config, | 54 current = request.cookies.get('foodtruck_site_name') |
41 request.cookies.get('foodtruck_site_name')) | 55 if current is None: |
56 names = g.config.get('sites') | |
57 if not names or not isinstance(names, dict): | |
58 raise FoodTruckConfigNotFoundError() | |
59 current = next(iter(names.keys())) | |
60 s = FoodTruckSites(g.config, current) | |
42 return s | 61 return s |
43 g.config = LazySomething(get_foodtruck_config) | 62 |
63 g.config = LazySomething(_get_config) | |
44 g.sites = LazySomething(_get_sites) | 64 g.sites = LazySomething(_get_sites) |
45 | 65 |
46 | 66 |
47 @app.after_request | 67 @app.after_request |
48 def _call_after_request_callbacks(response): | 68 def _call_after_request_callbacks(response): |