Mercurial > piecrust2
annotate foodtruck/web.py @ 772:3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
This makes it possible to use an app factory, which makes it easier to write
unit tests.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sun, 03 Jul 2016 07:54:54 -0700 |
parents | e67da1f7293b |
children | ba0a6bd5e913 |
rev | line source |
---|---|
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
587
diff
changeset
|
1 import os.path |
587
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
2 import logging |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
3 from flask import Flask |
706
e67da1f7293b
admin: Add support for `.well-known` folder.
Ludovic Chabant <ludovic@chabant.com>
parents:
669
diff
changeset
|
4 from werkzeug import SharedDataMiddleware |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
5 from .blueprint import foodtruck_bp, login_manager, bcrypt_ext |
587
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
6 |
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
7 |
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
8 logger = logging.getLogger(__name__) |
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
9 |
610
efc1dc916e7c
admin: Configuration changes.
Ludovic Chabant <ludovic@chabant.com>
parents:
608
diff
changeset
|
10 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
11 def create_foodtruck_app(extra_settings=None): |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
12 app = Flask(__name__) |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
13 app.config.from_object('foodtruck.settings') |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
14 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True) |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
15 if extra_settings: |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
16 app.config.from_object(extra_settings) |
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
587
diff
changeset
|
17 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
18 admin_root = app.config.setdefault('FOODTRUCK_ROOT', os.getcwd()) |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
19 config_path = os.path.join(admin_root, 'app.cfg') |
587
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
20 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
21 # If we're being run as the `chef admin run` command, from inside a PieCrust |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
22 # website, do a few things differently. |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
23 app.config['FOODTRUCK_PROCEDURAL_CONFIG'] = None |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
24 if (app.config.get('FOODTRUCK_CMDLINE_MODE', False) and |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
25 os.path.isfile(os.path.join(admin_root, 'config.yml'))): |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
26 app.secret_key = os.urandom(22) |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
27 app.config['LOGIN_DISABLED'] = True |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
28 app.config['FOODTRUCK_PROCEDURAL_CONFIG'] = { |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
29 'sites': { |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
30 'local': admin_root} |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
31 } |
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
587
diff
changeset
|
32 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
33 # Add a special route for the `.well-known` directory. |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
34 app.wsgi_app = SharedDataMiddleware( |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
35 app.wsgi_app, |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
36 {'/.well-known': os.path.join(admin_root, '.well-known')}) |
597
79a31a3c947b
admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents:
587
diff
changeset
|
37 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
38 if os.path.isfile(config_path): |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
39 app.config.from_pyfile(config_path) |
587
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
40 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
41 if app.config['DEBUG']: |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
42 l = logging.getLogger() |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
43 l.setLevel(logging.DEBUG) |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
44 else: |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
45 @app.errorhandler(FoodTruckConfigNotFoundError) |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
46 def _on_config_missing(ex): |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
47 return render_template('install.html') |
608
8e1b38632702
admin: Show the install page if no secret key is available.
Ludovic Chabant <ludovic@chabant.com>
parents:
602
diff
changeset
|
48 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
49 @app.errorhandler(InvalidSiteError) |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
50 def _on_invalid_site(ex): |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
51 data = {'error': "The was an error with your configuration file: %s" % |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
52 str(ex)} |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
53 return render_template('error.html', **data) |
587
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
54 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
55 _missing_secret_key = False |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
56 if not app.secret_key: |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
57 # If there's no secret key, create a temp one but mark the app as not |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
58 # correctly installed so it shows the installation information page. |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
59 app.secret_key = 'temp-key' |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
60 _missing_secret_key = True |
587
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
61 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
62 # Register extensions and blueprints. |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
63 login_manager.init_app(app) |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
64 bcrypt_ext.init_app(app) |
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
65 app.register_blueprint(foodtruck_bp) |
657
c1a94e1beb9d
admin: Show a more classic blog post listing in FoodTruck.
Ludovic Chabant <ludovic@chabant.com>
parents:
640
diff
changeset
|
66 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
67 logger.debug("Created FoodTruck app with admin root: %s" % admin_root) |
657
c1a94e1beb9d
admin: Show a more classic blog post listing in FoodTruck.
Ludovic Chabant <ludovic@chabant.com>
parents:
640
diff
changeset
|
68 |
772
3885421c29a3
admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents:
706
diff
changeset
|
69 return app |
587
d4a01a023998
admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff
changeset
|
70 |