Changeset 1505
- Timestamp:
- 25.03.2008 14:11:29 (2 months ago)
- Files:
-
- trunk/pylucid/PyLucid/install/BaseInstall.py (modified) (2 diffs)
- trunk/pylucid/PyLucid/middlewares/common.py (added)
- trunk/pylucid/PyLucid/middlewares/pagestats.py (modified) (4 diffs)
- trunk/pylucid/PyLucid/settings_example.py (modified) (1 diff)
- trunk/pylucid/PyLucid/templates_PyLucid/install_info.html (added)
- trunk/pylucid/tests/install_section.py (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/pylucid/PyLucid/install/BaseInstall.py
r1449 r1505 87 87 """ 88 88 89 def 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 89 108 class BaseInstall(object): 90 109 """ … … 108 127 self.request = request 109 128 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 120 131 self.page_msg = PageMessages(self.context) 121 132 self.context["page_msg"] = self.page_msg 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 trunk/pylucid/PyLucid/settings_example.py
r1503 r1505 83 83 # response phase the middleware will be applied in reverse order. 84 84 # 85 # !!! IMPORTANT !!!86 # * In the first install phase (befor the database tables exists) the87 # 'SessionMiddleware' and 'AuthenticationMiddleware' must be deactivated!88 # * After "syncdb" you must activate 'SessionMiddleware' and89 # 'AuthenticationMiddleware'!90 # * The DebugPageCache should be *never* activated. Only for dev debugging.91 # !!! IMPORTANT !!!92 #93 85 MIDDLEWARE_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 104 92 'django.middleware.common.CommonMiddleware', 105 93 'django.middleware.doc.XViewMiddleware',
