changeset 174:298b9f5ccdac

Common code for fallback when Bcrypt is not available. Quick workaround (changing current working directory) for correctly initializing the Flask application. WSGI helper to easily bootstrap a website for production.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 28 Jan 2014 20:34:11 -0800
parents bf2c79779800
children a971deb123d7
files wikked/bcryptfallback.py wikked/commands/users.py wikked/commands/web.py wikked/web.py wikked/wsgiutil.py
diffstat 5 files changed, 46 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wikked/bcryptfallback.py	Tue Jan 28 20:34:11 2014 -0800
@@ -0,0 +1,26 @@
+import logging
+
+logger = logging.getLogger(__name__)
+
+try:
+    from flaskext.bcrypt import Bcrypt, generate_password_hash
+
+except ImportError:
+    logger.warning("Bcrypt not available... falling back to SHA512.")
+
+    import hashlib
+
+    def generate_password_hash(password):
+        return hashlib.sha512(password).hexdigest()
+
+    def check_password_hash(self, reference, check):
+        check_hash = hashlib.sha512(check).hexdigest()
+        return check_hash == reference
+
+    class SHA512Fallback(object):
+        def __init__(self, app=None):
+            self.generate_password_hash = generate_password_hash
+            self.check_password_hash = check_password_hash
+
+    Bcrypt = SHA512Fallback
+
--- a/wikked/commands/users.py	Tue Jan 28 08:17:39 2014 -0800
+++ b/wikked/commands/users.py	Tue Jan 28 20:34:11 2014 -0800
@@ -1,6 +1,6 @@
 import logging
 from flask.ext.script import prompt_pass
-from flask.ext.bcrypt import generate_password_hash
+from wikked.bcryptfallback import generate_password_hash
 from wikked.commands.base import WikkedCommand, register_command
 
 
--- a/wikked/commands/web.py	Tue Jan 28 08:17:39 2014 -0800
+++ b/wikked/commands/web.py	Tue Jan 28 20:34:11 2014 -0800
@@ -24,7 +24,13 @@
                 action='store_true')
 
     def run(self, ctx):
+        # Change working directory because the Flask app can currently
+        # only initialize itself relative to that...
+        # TODO: make the Flask initialization more clever.
+        os.chdir(ctx.params.root)
+
         from wikked.web import app
+        app.wiki_params = ctx.params
 
         if bool(app.config.get('UPDATE_WIKI_ON_START')):
             ctx.wiki.update()
@@ -34,7 +40,6 @@
         if ctx.args.debug:
             app.config['DEBUG'] = True
 
-        app.wiki_params = ctx.params
         app.run(
                 host=ctx.args.host,
                 port=ctx.args.port,
--- a/wikked/web.py	Tue Jan 28 08:17:39 2014 -0800
+++ b/wikked/web.py	Tue Jan 28 20:34:11 2014 -0800
@@ -97,23 +97,8 @@
 
 
 # Bcrypt extension.
-try:
-    from flaskext.bcrypt import Bcrypt
-    app.bcrypt = Bcrypt(app)
-except ImportError:
-    app.logger.warning("Bcrypt not available... falling back to SHA512.")
-
-    import hashlib
-
-    class SHA512Fallback(object):
-        def check_password_hash(self, reference, check):
-            check_hash = hashlib.sha512(check).hexdigest()
-            return check_hash == reference
-
-        def generate_password_hash(self, password):
-            return hashlib.sha512(password).hexdigest()
-
-    app.bcrypt = SHA512Fallback()
+from wikked.bcryptfallback import Bcrypt
+app.bcrypt = Bcrypt(app)
 
 
 # Import the views.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wikked/wsgiutil.py	Tue Jan 28 20:34:11 2014 -0800
@@ -0,0 +1,11 @@
+
+def get_wsgi_app(wiki_root):
+    import os
+    os.chdir(wiki_root)
+
+    from wikked.web import app
+    from wikked.wiki import WikiParameters
+    app.wiki_params = WikiParameters(wiki_root)
+
+    return app
+