annotate piecrust/admin/web.py @ 951:c50ff76e0596

admin: Fix old API calls and bugs when editing pages.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 05 Oct 2017 00:28:34 -0700
parents 7ecb946bfafd
children e1cadbfddb48
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
1 import os.path
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
2 import logging
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
3 from flask import Flask
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
4 from werkzeug import SharedDataMiddleware
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
5
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
6
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
7 logger = logging.getLogger(__name__)
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
8
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
9
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
10 def create_foodtruck_app(extra_settings=None, url_prefix=None):
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
11 from .blueprint import foodtruck_bp
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
12
808
8f2d32f90095 admin: Don't have the static folder for the app collide with the blueprint's.
Ludovic Chabant <ludovic@chabant.com>
parents: 783
diff changeset
13 app = Flask(__name__.split('.')[0], static_folder=None)
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
14 app.config.from_object('piecrust.admin.settings')
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
15 if extra_settings:
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
16 app.config.update(extra_settings)
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
17
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
18 root_dir = app.config.setdefault('FOODTRUCK_ROOT_DIR', os.getcwd())
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
19
894
ca357249a321 admin: Read an optional Flask-app configuration file.
Ludovic Chabant <ludovic@chabant.com>
parents: 886
diff changeset
20 app.config.from_pyfile(os.path.join(root_dir, 'admin_app.cfg'),
ca357249a321 admin: Read an optional Flask-app configuration file.
Ludovic Chabant <ludovic@chabant.com>
parents: 886
diff changeset
21 silent=True)
ca357249a321 admin: Read an optional Flask-app configuration file.
Ludovic Chabant <ludovic@chabant.com>
parents: 886
diff changeset
22 app.config.from_envvar('FOODTRUCK_SETTINGS', silent=True)
ca357249a321 admin: Read an optional Flask-app configuration file.
Ludovic Chabant <ludovic@chabant.com>
parents: 886
diff changeset
23
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
24 # Setup logging/error handling.
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
25 if app.config['DEBUG']:
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
26 l = logging.getLogger()
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
27 l.setLevel(logging.DEBUG)
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
28
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
29 if not app.config['SECRET_KEY']:
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
30 # If there's no secret key, create a temp one but mark the app as not
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
31 # correctly installed so it shows the installation information page.
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
32 app.config['SECRET_KEY'] = 'temp-key'
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
33
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
34 # Register extensions and blueprints.
935
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
35 app.register_blueprint(foodtruck_bp, url_prefix=url_prefix)
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
36
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
37 # ---------------
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
38 # Debugging stuff
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
39 @app.errorhandler(404)
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
40 def page_not_found(e):
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
41 from flask import request, url_for
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
42 output = []
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
43 for rule in app.url_map.iter_rules():
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
44 options = {}
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
45 for arg in rule.arguments:
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
46 options[arg] = "[{0}]".format(arg)
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
47 methods = ','.join(rule.methods)
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
48 try:
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
49 url = url_for(rule.endpoint, **options)
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
50 except:
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
51 url = '???'
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
52 line = ("{:50s} {:20s} {}".format(rule.endpoint, methods, url))
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
53 output.append(line)
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
54
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
55 resp = request.path + '<br/>\n'
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
56 resp += str(request.environ) + '<br/>\n'
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
57 resp += str(app.config['FOODTRUCK_ROOT_URL']) + '<br/>\n'
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
58 resp += '<br/>\n'.join(sorted(output))
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
59 return resp, 404
7ecb946bfafd admin: Lots of fixes for running the admin panel in a WSGI server.
Ludovic Chabant <ludovic@chabant.com>
parents: 894
diff changeset
60 # ---------------
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
61
886
dcdec4b951a1 admin: Get the admin panel working again.
Ludovic Chabant <ludovic@chabant.com>
parents: 812
diff changeset
62 logger.debug("Created FoodTruck app with admin root: %s" % root_dir)
778
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
63
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
64 return app
5e91bc0e3b4d internal: Move admin panel code into the piecrust package.
Ludovic Chabant <ludovic@chabant.com>
parents:
diff changeset
65