Show
Ignore:
Timestamp:
03/25/08 14:11:29 (8 months ago)
Author:
JensDiemer
Message:

work-a-round for ticket:87 with the new PyLucidCommonMiddleware?.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/middlewares/pagestats.py

    r1503 r1505  
    2121""" 
    2222 
    23 from operator import add 
    2423from time import time 
    2524 
    2625from django.db import connection 
    27 from django.core.exceptions import ImproperlyConfigured 
    2826 
    2927from PyLucid.template_addons.filters import human_duration 
     
    3937) 
    4038 
     39class 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) 
    4147 
    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        """ 
    6852        # Put only the statistic into HTML pages 
    6953        if not "html" in response._headers["content-type"][1]: 
     
    7357        # compute the db time for the queries just run 
    7458        # FIXME: In my shared webhosting environment the queries is always = 0 
    75         queries = len(connection.queries) - old_queries 
     59        queries = len(connection.queries) - self.old_queries 
    7660 
    77         total_time = human_duration(time() - start_time) 
     61        total_time = human_duration(time() - self.start_time) 
    7862        overall_time = human_duration(time() - start_overall) 
    7963 
     
    8569        } 
    8670 
    87         try: 
    88             response.content = response.content.replace(TAG, stat_info) 
    89         except UnicodeError: 
     71        content = response.content 
     72        if not isinstance(content, unicode): 
    9073            # FIXME: In my shared webhosting environment is response.content a 
    9174            # string and not unicode. Why? 
    9275            from django.utils.encoding import force_unicode 
    9376            try: 
    94                 response.content = force_unicode(response.content)\ 
    95                                                     .replace(TAG, stat_info) 
     77                content = force_unicode(content) 
    9678            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 
    9884 
    9985        return response