Commits

Andriy Kornatskyy  committed 990dfab

Initial demo for wheezy.web integrattion.

  • Participants
  • Parent commits 9a21e3d

Comments (0)

Files changed (3)

File demos/app.py

-"""
-"""
-
-from wheezy.http import HTTPResponse
-from wheezy.http import WSGIApplication
-from wheezy.http import accept_method
-from wheezy.http import bootstrap_http_defaults
-from wheezy.http import not_found
-from wheezy.http.middleware import http_cache_middleware_factory
-
-from shared import cache
-from shared import captcha
-from shared import captcha_handler
-
-
-@accept_method(('GET', 'POST'))
-def welcome(request):
-    message, error = '', ''
-    if request.method == 'POST':
-        errors = {}
-        if not captcha.validate(request, errors, gettext=lambda s: s):
-            error = errors['turing_number'][-1]
-        else:
-            message = 'Well done!'
-    challenge_code = captcha.get_challenge_code(request)
-    response = HTTPResponse()
-    response.write("""
-<html><body><form method="post">
-<h2>Captcha Verification</h2>
-<span style="color: green"><b>%s</b></span>
-<p>Please enter the text from image:</p>
-<p>
-    <label for="turing_number">
-        <img id="captcha" src="/captcha.jpg?c=%s"
-            style="display:block; width:200; height:75"
-            onclick="this.src='/captcha.jpg?c=%s&r=' + \
-                   Math.floor(Math.random()*100+1)"/>
-        <input type="hidden" name="c" value="%s" />
-    </label>
-    <input id="turing_number" name="turing_number" type="text"
-        maxlength="4" style="width:200;text-transform: uppercase;"
-        autocomplete="off" />
-    <span style="color:red">%s</span>
-</p>
-<p><input type="submit" value="Verify"></p>
-</form></body></html>
-    """ % (message, challenge_code, challenge_code, challenge_code, error))
-    return response
-
-
-def router_middleware(request, following):
-    path = request.path
-    if path == '/':
-        response = welcome(request)
-    elif path.startswith('/captcha.jpg'):
-        response = captcha_handler(request)
-    else:
-        response = not_found()
-    return response
-
-
-options = {
-    'http_cache': cache
-}
-main = WSGIApplication([
-    bootstrap_http_defaults,
-    http_cache_middleware_factory,
-    lambda ignore: router_middleware
-], options)
-
-
-if __name__ == '__main__':
-    from wsgiref.simple_server import make_server
-    try:
-        print('Visit http://localhost:8080/')
-        make_server('', 8080, main).serve_forever()
-    except KeyboardInterrupt:
-        pass
-    print('\nThanks!')

File demos/app_http.py

+"""
+"""
+
+from wheezy.http import HTTPResponse
+from wheezy.http import WSGIApplication
+from wheezy.http import accept_method
+from wheezy.http import bootstrap_http_defaults
+from wheezy.http import not_found
+from wheezy.http.middleware import http_cache_middleware_factory
+
+from shared import cache
+from shared import captcha
+from shared import captcha_handler
+
+
+@accept_method(('GET', 'POST'))
+def welcome(request):
+    message, error = '', ''
+    if request.method == 'POST':
+        errors = {}
+        if not captcha.validate(request, errors, gettext=lambda s: s):
+            error = errors['turing_number'][-1]
+        else:
+            message = 'Well done!'
+    challenge_code = captcha.get_challenge_code(request)
+    response = HTTPResponse()
+    response.write("""
+<html><body><form method="post">
+<h2>Captcha Verification</h2>
+<span style="color: green"><b>%s</b></span>
+<p>Please enter the text from image:</p>
+<p>
+    <label for="turing_number">
+        <img id="captcha" src="/captcha.jpg?c=%s"
+            style="display:block; width:200; height:75"
+            onclick="this.src='/captcha.jpg?c=%s&r=' + \
+                   Math.floor(Math.random()*100+1)"/>
+        <input type="hidden" name="c" value="%s" />
+    </label>
+    <input id="turing_number" name="turing_number" type="text"
+        maxlength="4" style="width:200;text-transform: uppercase;"
+        autocomplete="off" />
+    <span style="color:red">%s</span>
+</p>
+<p><input type="submit" value="Verify"></p>
+</form></body></html>
+    """ % (message, challenge_code, challenge_code, challenge_code, error))
+    return response
+
+
+def router_middleware(request, following):
+    path = request.path
+    if path == '/':
+        response = welcome(request)
+    elif path.startswith('/captcha.jpg'):
+        response = captcha_handler(request)
+    else:
+        response = not_found()
+    return response
+
+
+options = {
+    'http_cache': cache
+}
+main = WSGIApplication([
+    bootstrap_http_defaults,
+    http_cache_middleware_factory,
+    lambda ignore: router_middleware
+], options)
+
+
+if __name__ == '__main__':
+    from wsgiref.simple_server import make_server
+    try:
+        print('Visit http://localhost:8080/')
+        make_server('', 8080, main).serve_forever()
+    except KeyboardInterrupt:
+        pass
+    print('\nThanks!')

File demos/app_web.py

+"""
+"""
+
+from wheezy.http import HTTPResponse
+from wheezy.http import WSGIApplication
+from wheezy.web.handlers.base import BaseHandler
+from wheezy.http.middleware import http_cache_middleware_factory
+from wheezy.web.middleware import bootstrap_defaults
+from wheezy.web.middleware import http_error_middleware_factory
+from wheezy.web.middleware import path_routing_middleware_factory
+
+
+from shared import cache
+from shared import captcha
+from shared import captcha_handler
+
+
+class WelcomeHandler(BaseHandler):
+
+    def get(self, message='', error=''):
+        challenge_code = captcha.get_challenge_code(self.request)
+        response = HTTPResponse()
+        response.write("""
+    <html><body><form method="post">
+    <h2>Captcha Verification</h2>
+    <span style="color: green"><b>%s</b></span>
+    <p>Please enter the text from image:</p>
+    <p>
+        <label for="turing_number">
+            <img id="captcha" src="/captcha.jpg?c=%s"
+                style="display:block; width:200; height:75"
+                onclick="this.src='/captcha.jpg?c=%s&r=' + \
+                       Math.floor(Math.random()*100+1)"/>
+            <input type="hidden" name="c" value="%s" />
+        </label>
+        <input id="turing_number" name="turing_number" type="text"
+            maxlength="4" style="width:200;text-transform: uppercase;"
+            autocomplete="off" />
+        <span style="color:red">%s</span>
+    </p>
+    <p><input type="submit" value="Verify"></p>
+    </form></body></html>
+        """ % (message, challenge_code, challenge_code, challenge_code, error))
+        return response
+
+    def post(self):
+        if not captcha.validate(self.request, self.errors,
+                                gettext=lambda s: s):
+            return self.get(error=self.errors['turing_number'][-1])
+        else:
+            return self.get('Well done!')
+
+
+all_urls = [
+    ('', WelcomeHandler),
+    ('captcha.jpg', captcha_handler)
+]
+
+options = {
+    'http_cache': cache
+}
+main = WSGIApplication([
+    bootstrap_defaults(url_mapping=all_urls),
+    http_cache_middleware_factory,
+    http_error_middleware_factory,
+    path_routing_middleware_factory
+], options)
+
+
+if __name__ == '__main__':
+    from wsgiref.simple_server import make_server
+    try:
+        print('Visit http://localhost:8080/')
+        make_server('', 8080, main).serve_forever()
+    except KeyboardInterrupt:
+        pass
+    print('\nThanks!')