annotate foodtruck/web.py @ 775:ba0a6bd5e913

tests: Make it possible to run FoodTruck tests.
author Ludovic Chabant <ludovic@chabant.com>
date Mon, 04 Jul 2016 00:12:49 -0700
parents 3885421c29a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
775
ba0a6bd5e913 tests: Make it possible to run FoodTruck tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 772
diff changeset
6 from .configuration import FoodTruckConfigNotFoundError
ba0a6bd5e913 tests: Make it possible to run FoodTruck tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 772
diff changeset
7 from .sites import InvalidSiteError
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
10 logger = logging.getLogger(__name__)
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
11
610
efc1dc916e7c admin: Configuration changes.
Ludovic Chabant <ludovic@chabant.com>
parents: 608
diff changeset
12
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
13 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
14 app = Flask(__name__)
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
15 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
16 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
17 if extra_settings:
775
ba0a6bd5e913 tests: Make it possible to run FoodTruck tests.
Ludovic Chabant <ludovic@chabant.com>
parents: 772
diff changeset
18 app.config.update(extra_settings)
597
79a31a3c947b admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents: 587
diff changeset
19
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
20 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
21 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
22
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
23 # 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
24 # 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
25 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
26 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
27 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
28 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
29 app.config['LOGIN_DISABLED'] = True
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
30 app.config['FOODTRUCK_PROCEDURAL_CONFIG'] = {
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
31 'sites': {
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
32 'local': admin_root}
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
33 }
597
79a31a3c947b admin: Better production config for FoodTruck, provide proper first site.
Ludovic Chabant <ludovic@chabant.com>
parents: 587
diff changeset
34
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
35 # 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
36 app.wsgi_app = SharedDataMiddleware(
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
37 app.wsgi_app,
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
38 {'/.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
39
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
40 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
41 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
42
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
43 if app.config['DEBUG']:
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
44 l = logging.getLogger()
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
45 l.setLevel(logging.DEBUG)
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
46 else:
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
47 @app.errorhandler(FoodTruckConfigNotFoundError)
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
48 def _on_config_missing(ex):
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
49 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
50
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
51 @app.errorhandler(InvalidSiteError)
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
52 def _on_invalid_site(ex):
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
53 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
54 str(ex)}
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
55 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
56
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
57 _missing_secret_key = False
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
58 if not app.secret_key:
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
59 # 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
60 # 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
61 app.secret_key = 'temp-key'
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
62 _missing_secret_key = True
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
64 # Register extensions and blueprints.
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
65 login_manager.init_app(app)
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
66 bcrypt_ext.init_app(app)
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
67 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
68
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
69 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
70
772
3885421c29a3 admin: Make the whole FoodTruck site into a blueprint.
Ludovic Chabant <ludovic@chabant.com>
parents: 706
diff changeset
71 return app
587
d4a01a023998 admin: Add "FoodTruck" admin panel from the side experiment project.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
72