Commits

Andriy Kornatskyy committed 689a6ef

Added BaseHandler.json_response; updated demo and tests

Comments (0)

Files changed (6)

demos/public/content/templates/public/about.html

     and accessable for business accounts only.
     </p>
     <p>Take a look at <a href="${path_for('widgets')}">widgets</a>
-    available.</a>
+    available.
+    </p>
+    <p><a id="st" href="${path_for('now')}">Click</a> to
+    make an ajax call
+    to get json response with server time. The server time is:
+    <span id="now">N/A</span>
+    </p>
 </div>
+<%block name="script">
+<script language="javascript" type="text/javascript">
+    $(document).ready(function() {
+        $("#st").click(function(e){
+            e.preventDefault();
+            $.getJSON(this.href, function(data) {
+                $.each(data, function(key, val) {
+                    $("#" + key).text(val)
+                })
+            });
+        });
+    })
+</script>
+</%block>

demos/public/src/public/web/tests/benchmark_views.py

         p = Benchmark((
             self.test_home,
             self.test_about,
+            self.test_now,
             self.test_widgets,
             self.test_widgets_with_errors,
             self.test_static_files,
         p.report('public', baselines={
                 'test_home': 1.0,
                 'test_about': 0.89,
+                'test_now': 3.34,
                 'test_widgets': 0.52,
                 'test_widgets_with_errors': 0.51,
                 'test_static_files': 1.655,

demos/public/src/public/web/tests/test_views.py

         assert 200 == self.client.get('/en/about')
         assert '- About</title>' in self.client.content
 
+    def test_now(self):
+        """
+        """
+        assert 200 == self.client.get('/en/now', environ={
+            'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'
+        })
+        assert 'now' in self.client.content
+
+    def test_now_bad_request(self):
+        """
+        """
+        self.client.get('/en/now')
+        assert 400 == self.client.follow()
+
     def test_widgets(self):
         """ Ensure widgets page is rendered.
         """

demos/public/src/public/web/urls.py

 
 from public.web.views import about
 from public.web.views import home
+from public.web.views import NowHandler
 from public.web.views import WidgetsHandler
 
 
 public_urls = [
     url('home', home, name='home'),
     url('about', about, name='about'),
+    url('now', NowHandler, name='now'),
     url('widgets-with-errors', WidgetsHandler,
         kwargs={'mode': 'errors'}, name='widgets-with-errors'),
     url('widgets', WidgetsHandler,

demos/public/src/public/web/views.py

 """
 """
 
+from datetime import datetime
+
 from wheezy.core.collections import attrdict
 from wheezy.core.descriptors import attribute
+from wheezy.http import bad_request
 from wheezy.web.handlers import BaseHandler
 from wheezy.web.handlers import template_handler
 
 about = template_handler('public/about.html')
 
 
+class NowHandler(BaseHandler):
+
+    def get(self):
+        if not self.request.ajax:
+            return bad_request()
+        return self.json_response({'now': datetime.now()})
+
+
 class WidgetsHandler(BaseHandler):
 
     @attribute

src/wheezy/web/handlers/base.py

 from wheezy.core.descriptors import attribute
 from wheezy.core.i18n import null_translations
 from wheezy.core.i18n import ref_gettext
+from wheezy.core.json import json_encode
 from wheezy.core.url import urlparts
 from wheezy.core.uuid import UUID_EMPTY
 from wheezy.core.uuid import parse_uuid
             '_': self._,
             'absolute_url_for': self.absolute_url_for,
             'errors': self.errors,
-            'handler': self,
             'path_for': self.path_for,
             'principal': self.principal,
             'resubmission': self.resubmission_widget,
             **kwargs))
         return response
 
+    # region: json
+
+    def json_response(self, obj):
+        encoding = self.options['ENCODING']
+        response = HTTPResponse(
+                'application/json; charset=' + encoding,
+                encoding)
+        response.write_bytes(json_encode(obj).encode(encoding))
+        return response
+
     # region: authentication
 
     @attribute