diff piecrust/admin/blueprint.py @ 935:7ecb946bfafd

admin: Lots of fixes for running the admin panel in a WSGI server. - Use new source APIs in the dashboard to open WIP files. - Fixed broken/outdated code in some views. - Fixed cases when Flask is not running at the root URL by using the `SCRIPT_NAME` variable somewhat more properly.
author Ludovic Chabant <ludovic@chabant.com>
date Wed, 04 Oct 2017 09:15:16 -0700
parents 2b0fa2e4c12f
children 94fd4f07da83
line wrap: on
line diff
--- a/piecrust/admin/blueprint.py	Wed Oct 04 09:11:58 2017 -0700
+++ b/piecrust/admin/blueprint.py	Wed Oct 04 09:15:16 2017 -0700
@@ -18,22 +18,24 @@
 
 
 def load_user(user_id):
-    admin_id = g.config.get('security/username')
+    admin_id = current_app.config.get('USERNAME')
     if admin_id == user_id:
-        admin_pwd = g.config.get('security/password')
+        admin_pwd = current_app.config.get('PASSWORD')
         return User(admin_id, admin_pwd)
     return None
 
 
-login_manager = LoginManager()
-login_manager.login_view = 'FoodTruck.login'
-login_manager.user_loader(load_user)
+def record_login_manager(state):
+    login_manager = LoginManager()
+    login_manager.login_view = 'FoodTruck.login'
+    login_manager.user_loader(load_user)
 
-
-def record_login_manager(state):
     if state.app.config['SECRET_KEY'] == 'temp-key':
         def _handler():
-            raise Exception("No secret key has been set!")
+            from flask import render_template
+            return render_template(
+                'error.html',
+                error="No secret key has been set!")
 
         logger.debug("No secret key found, disabling website login.")
         login_manager.unauthorized_handler(_handler)
@@ -91,9 +93,8 @@
 @foodtruck_bp.before_request
 def _setup_foodtruck_globals():
     def _get_site():
-        root_dir = current_app.config['FOODTRUCK_ROOT']
-        url_prefix = current_app.config['FOODTRUCK_URL_PREFIX']
-        return SiteInfo(root_dir, url_prefix, debug=current_app.debug)
+        root_dir = current_app.config['FOODTRUCK_ROOT_DIR']
+        return SiteInfo(root_dir, debug=current_app.debug)
 
     g.site = LazySomething(_get_site)
 
@@ -131,5 +132,3 @@
 import piecrust.admin.views.preview  # NOQA
 import piecrust.admin.views.publish  # NOQA
 import piecrust.admin.views.sources  # NOQA
-
-