Changeset 1505 for trunk/pylucid/PyLucid/middlewares/pagestats.py
- Timestamp:
- 03/25/08 14:11:29 (8 months ago)
- Files:
-
- 1 modified
-
trunk/pylucid/PyLucid/middlewares/pagestats.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pylucid/PyLucid/middlewares/pagestats.py
r1503 r1505 21 21 """ 22 22 23 from operator import add24 23 from time import time 25 24 26 25 from django.db import connection 27 from django.core.exceptions import ImproperlyConfigured28 26 29 27 from PyLucid.template_addons.filters import human_duration … … 39 37 ) 40 38 39 class PageStatsMiddleware(object): 40 def process_request(self, request): 41 """ 42 save start time and database connections count. 43 """ 44 self.start_time = time() 45 # get number of db queries before we do anything 46 self.old_queries = len(connection.queries) 41 47 42 class PageStatsMiddleware(object): 43 def process_view(self, request, view_func, view_args, view_kwargs): 44 start_time = time() 45 46 # get number of db queries before we do anything 47 old_queries = len(connection.queries) 48 49 try: 50 # start the view 51 response = view_func(request, *view_args, **view_kwargs) 52 except AttributeError, e: 53 if str(e)== "'WSGIRequest' object has no attribute 'user'": 54 from django.conf import settings 55 if not 'django.contrib.sessions.middleware.SessionMiddleware' \ 56 in settings.MIDDLEWARE_CLASSES or not \ 57 'django.contrib.auth.middleware.AuthenticationMiddleware' \ 58 in settings.MIDDLEWARE_CLASSES: 59 msg = ( 60 "You must include the session middleware and the" 61 " authentication middleware in your settings.py" 62 " after a syncdb. --- The original error message" 63 " was: %s" 64 ) % e 65 raise ImproperlyConfigured(msg) 66 raise 67 48 def process_response(self, request, response): 49 """ 50 calculate the statistic and replace it into the html page. 51 """ 68 52 # Put only the statistic into HTML pages 69 53 if not "html" in response._headers["content-type"][1]: … … 73 57 # compute the db time for the queries just run 74 58 # FIXME: In my shared webhosting environment the queries is always = 0 75 queries = len(connection.queries) - old_queries59 queries = len(connection.queries) - self.old_queries 76 60 77 total_time = human_duration(time() - s tart_time)61 total_time = human_duration(time() - self.start_time) 78 62 overall_time = human_duration(time() - start_overall) 79 63 … … 85 69 } 86 70 87 try: 88 response.content = response.content.replace(TAG, stat_info) 89 except UnicodeError: 71 content = response.content 72 if not isinstance(content, unicode): 90 73 # FIXME: In my shared webhosting environment is response.content a 91 74 # string and not unicode. Why? 92 75 from django.utils.encoding import force_unicode 93 76 try: 94 response.content = force_unicode(response.content)\ 95 .replace(TAG, stat_info) 77 content = force_unicode(content) 96 78 except: 97 pass 79 return response 80 81 # insert the page statistic 82 new_content = content.replace(TAG, stat_info) 83 response.content = new_content 98 84 99 85 return response
