Changeset 1505

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

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

Location:
trunk/pylucid
Files:
3 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid/PyLucid/install/BaseInstall.py

    r1449 r1505  
    8787""" 
    8888 
     89def get_base_context(request): 
     90    """ 
     91    Build a base context. 
     92    Used in BaseInstall and PyLucidCommonMiddleware. 
     93    """ 
     94    media_url = posixpath.join( 
     95        settings.MEDIA_URL, settings.PYLUCID_MEDIA_DIR, "" 
     96    ) # With appended slash, for backward compatible 
     97    context = { 
     98        "output": "", 
     99        "request": request, 
     100        "PyLucid_media_url": media_url, 
     101        "version": PYLUCID_VERSION_STRING, 
     102        "current_working_dir": os.getcwd(), 
     103    } 
     104    return context 
     105 
     106 
     107 
    89108class BaseInstall(object): 
    90109    """ 
     
    108127        self.request = request 
    109128 
    110         media_url = posixpath.join( 
    111             settings.MEDIA_URL, settings.PYLUCID_MEDIA_DIR, "" 
    112         ) # With appended slash, for backward compatible 
    113         self.context = { 
    114             "output": "", 
    115             "request": request, 
    116             "PyLucid_media_url": media_url, 
    117             "version": PYLUCID_VERSION_STRING, 
    118             "current_working_dir": os.getcwd(), 
    119         } 
     129        self.context = get_base_context(request) 
     130 
    120131        self.page_msg = PageMessages(self.context) 
    121132        self.context["page_msg"] = self.page_msg 
  • 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 
  • trunk/pylucid/PyLucid/settings_example.py

    r1503 r1505  
    8383# response phase the middleware will be applied in reverse order. 
    8484# 
    85 # !!! IMPORTANT !!! 
    86 #  * In the first install phase (befor the database tables exists) the 
    87 #    'SessionMiddleware' and 'AuthenticationMiddleware' must be deactivated! 
    88 #  * After "syncdb" you must activate 'SessionMiddleware' and 
    89 #    'AuthenticationMiddleware'! 
    90 #  * The DebugPageCache should be *never* activated. Only for dev debugging. 
    91 # !!! IMPORTANT !!! 
    92 # 
    9385MIDDLEWARE_CLASSES = ( 
    94     # DebugPageCache normaly not used. 
    95 #    'PyLucid.middlewares.page_cache_debug.DebugPageCache', 
    96  
    97     # Activate Session- and Authentication-Middleware after 'syncdb' : 
    98     # ------------------------------------------------------------------------- 
    99 #    'django.contrib.sessions.middleware.SessionMiddleware', 
    100 #    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    101     # ------------------------------------------------------------------------- 
    102  
    103     'django.middleware.locale.LocaleMiddleware', 
     86    # PyLucidCommonMiddleware loads the django middlewares: 
     87    #    - 'django.contrib.sessions.middleware.SessionMiddleware' 
     88    #    - 'django.contrib.auth.middleware.AuthenticationMiddleware' 
     89    #    - 'django.middleware.locale.LocaleMiddleware' 
     90    'PyLucid.middlewares.common.PyLucidCommonMiddleware', 
     91 
    10492    'django.middleware.common.CommonMiddleware', 
    10593    'django.middleware.doc.XViewMiddleware',