Mercurial > piecrust2
comparison foodtruck/web.py @ 610:efc1dc916e7c
admin: Configuration changes.
* Move publish targets to site configuration.
* Add direct accessor for the current site.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Thu, 28 Jan 2016 22:17:58 -0800 |
parents | 8e1b38632702 |
children | 59968ee07a07 |
comparison
equal
deleted
inserted
replaced
609:978d8bca9fb3 | 610:efc1dc916e7c |
---|---|
13 app.config.from_object('foodtruck.settings') | 13 app.config.from_object('foodtruck.settings') |
14 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True) | 14 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True) |
15 | 15 |
16 admin_root = app.config['FOODTRUCK_ROOT'] or os.getcwd() | 16 admin_root = app.config['FOODTRUCK_ROOT'] or os.getcwd() |
17 config_path = os.path.join(admin_root, 'app.cfg') | 17 config_path = os.path.join(admin_root, 'app.cfg') |
18 | |
19 # If we're being run as the `chef admin run` command, from inside a PieCrust | |
20 # website, do a few things differently. | |
21 _procedural_config = None | |
22 | |
23 if (app.config['FOODTRUCK_CMDLINE_MODE'] and | |
24 os.path.isfile(os.path.join(admin_root, 'config.yml'))): | |
25 app.secret_key = os.urandom(22) | |
26 app.config['LOGIN_DISABLED'] = True | |
27 _procedural_config = { | |
28 'sites': { | |
29 'local': admin_root} | |
30 } | |
31 | |
32 | |
18 if os.path.isfile(config_path): | 33 if os.path.isfile(config_path): |
19 app.config.from_pyfile(config_path) | 34 app.config.from_pyfile(config_path) |
20 | 35 |
21 if app.config['DEBUG']: | 36 if app.config['DEBUG']: |
22 l = logging.getLogger() | 37 l = logging.getLogger() |
46 | 61 |
47 | 62 |
48 @app.before_request | 63 @app.before_request |
49 def _setup_foodtruck_globals(): | 64 def _setup_foodtruck_globals(): |
50 def _get_config(): | 65 def _get_config(): |
51 return get_foodtruck_config(admin_root) | 66 return get_foodtruck_config(admin_root, _procedural_config) |
52 | 67 |
53 def _get_sites(): | 68 def _get_sites(): |
69 names = g.config.get('sites') | |
70 if not names or not isinstance(names, dict): | |
71 raise InvalidSiteError( | |
72 "No sites are defined in the configuration file.") | |
73 | |
54 current = request.cookies.get('foodtruck_site_name') | 74 current = request.cookies.get('foodtruck_site_name') |
75 if current is not None and current not in names: | |
76 current = None | |
55 if current is None: | 77 if current is None: |
56 names = g.config.get('sites') | |
57 if not names or not isinstance(names, dict): | |
58 raise InvalidSiteError( | |
59 "No sites are defined in the configuration file.") | |
60 current = next(iter(names.keys())) | 78 current = next(iter(names.keys())) |
61 s = FoodTruckSites(g.config, current) | 79 s = FoodTruckSites(g.config, current) |
62 return s | 80 return s |
63 | 81 |
82 def _get_current_site(): | |
83 return g.sites.get() | |
84 | |
64 g.config = LazySomething(_get_config) | 85 g.config = LazySomething(_get_config) |
65 g.sites = LazySomething(_get_sites) | 86 g.sites = LazySomething(_get_sites) |
87 g.site = LazySomething(_get_current_site) | |
66 | 88 |
67 | 89 |
68 @app.after_request | 90 @app.after_request |
69 def _call_after_request_callbacks(response): | 91 def _call_after_request_callbacks(response): |
70 for callback in getattr(g, 'after_request_callbacks', ()): | 92 for callback in getattr(g, 'after_request_callbacks', ()): |