Commits

Ian Lewis committed e7ed16e Merge

Merged AppstatsMiddleware

  • Participants
  • Parent commits eb6e6b7, 920381c

Comments (0)

Files changed (5)

File kay/ext/appstats/__init__.py

Empty file added.

File kay/ext/appstats/middleware.py

+#!/usr/bin/env python2.5
+# -*- coding:utf-8 -*-
+
+"""
+AppStatsMiddleware adapted to Kay framework.
+
+:Copyright: (c) 2010 Ian Lewis <ianmlewis@gmail.com>,
+:license: BSD, see LICENSE for more details.
+"""
+
+class AppStatsMiddleware(object):
+  """
+  Middleware to enable appstats recording.
+
+  Based off of the the AppstatsDjangoMiddleware in the 
+  Appengine SDK
+  """
+
+  def process_request(self, request):
+    """
+    Called by Kay before deciding which view to execute.
+    """
+    from google.appengine.ext.appstats.recording import start_recording
+    start_recording()
+
+  def process_response(self, request, response):
+    """
+    Stops recording. Optionally sets some extension data for
+    FirePython.
+    """
+    from google.appengine.ext.appstats.recording import end_recording
+
+    firepython_set_extension_data = getattr(
+      request,
+      'firepython_set_extension_data',
+      None)
+    end_recording(response.status_code, firepython_set_extension_data)
+    return response

File kay/tests/__init__.py

-from formtest import *
+hfrom formtest import *
 from decorator_test import *
 from utils_test import *
 from session_test import *
 from auth_test import *
 from rest_test import *
 from jinja2_test import *
+from appstats_test import *

File kay/tests/appstats_settings.py

+# -*- coding: utf-8 -*-
+
+"""
+A sample of kay settings.
+
+:Copyright: (c) 2009 Accense Technology, Inc. 
+                     Takashi Matsuo <tmatsuo@candit.jp>,
+                     All rights reserved.
+:license: BSD, see LICENSE for more details.
+"""
+
+DEBUG = False
+ROOT_URL_MODULE = 'kay.tests.globalurls'
+
+INSTALLED_APPS = (
+  'kay.tests',
+)
+
+APP_MOUNT_POINTS = {
+  'kay.tests': '/',
+}
+
+# You can remove following settings if unnecessary.
+CONTEXT_PROCESSORS = (
+  'kay.context_processors.request',
+  'kay.context_processors.url_functions',
+  'kay.context_processors.media_url',
+)
+
+MIDDLEWARE_CLASSES = (
+  'kay.ext.appstats.middleware.AppStatsMiddleware',
+)

File kay/tests/appstats_test.py

+# -*- coding: utf-8 -*-
+
+
+from werkzeug import (
+  BaseResponse, Client, Request
+)
+
+from kay.app import get_application
+from kay.conf import LazySettings
+from kay.ext.testutils.gae_test_base import GAETestBase
+from kay.ext.appstats.middleware import AppStatsMiddleware
+
+class AppStatsMiddlewareTestCase(GAETestBase):
+  KIND_NAME_UNSWAPPED = False
+  USE_PRODUCTION_STUBS = True
+  CLEANUP_USED_KIND = True
+
+  def setUp(self):
+    s = LazySettings(settings_module='kay.tests.appstats_settings')
+    app = get_application(settings=s)
+    self.client = Client(app, BaseResponse)
+
+  def tearDown(self):
+    pass
+
+  def test_appstats_middleware(self):
+    from google.appengine.api import apiproxy_stub_map
+
+    request = Request({})
+    middleware = AppStatsMiddleware()
+
+    r = middleware.process_request(request)
+    self.assertTrue(r is None)
+
+    r = middleware.process_response(request, BaseResponse("", 200))
+    self.assertTrue(isinstance(r, BaseResponse))
+
+    memcache = apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map['memcache']._the_cache
+    self.assertTrue('__appstats__' in memcache)
+
+  def test_appstats_middleware_request(self):
+    from google.appengine.api import apiproxy_stub_map
+    response = self.client.get('/no_decorator')
+    memcache = apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map['memcache']._the_cache
+    self.assertTrue('__appstats__' in memcache)