| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | """ |
|---|
| 4 | PyLucid page cache debug |
|---|
| 5 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 6 | |
|---|
| 7 | A hackish debugger for django CacheMiddleware. |
|---|
| 8 | |
|---|
| 9 | Append the cache update variable request._cache_update_cache into every |
|---|
| 10 | response. |
|---|
| 11 | |
|---|
| 12 | Should be only used for dev debugging and not for production ;) |
|---|
| 13 | |
|---|
| 14 | Last commit info: |
|---|
| 15 | ~~~~~~~~~~~~~~~~~ |
|---|
| 16 | $LastChangedDate: $ |
|---|
| 17 | $Rev: $ |
|---|
| 18 | $Author: $ |
|---|
| 19 | |
|---|
| 20 | :copyleft: 2007 by the PyLucid team, see AUTHORS for more details. |
|---|
| 21 | :license: GNU GPL v3 or above, see LICENSE for more details. |
|---|
| 22 | """ |
|---|
| 23 | |
|---|
| 24 | class DebugPageCache(object): |
|---|
| 25 | """ |
|---|
| 26 | Debug django CacheMiddleware |
|---|
| 27 | """ |
|---|
| 28 | def __init__(self): |
|---|
| 29 | self.func_name = None |
|---|
| 30 | self.__cache_update_cache = None |
|---|
| 31 | |
|---|
| 32 | def process_view(self, request, view_func, view_args, view_kwargs): |
|---|
| 33 | """ |
|---|
| 34 | Save the view function name. |
|---|
| 35 | """ |
|---|
| 36 | self.func_name = view_func.func_name |
|---|
| 37 | |
|---|
| 38 | def process_response(self, request, response): |
|---|
| 39 | """ |
|---|
| 40 | Append the repr() of request._cache_update_cache |
|---|
| 41 | """ |
|---|
| 42 | if self.func_name == None: |
|---|
| 43 | # Not a normal response (e.g. Redirect) -> do nothing. |
|---|
| 44 | return response |
|---|
| 45 | |
|---|
| 46 | if hasattr(request, '_cache_update_cache'): |
|---|
| 47 | self.__cache_update_cache = request._cache_update_cache |
|---|
| 48 | |
|---|
| 49 | info = "request._cache_update_cache: '%s' ('%s' view) " % ( |
|---|
| 50 | repr(self.__cache_update_cache), self.func_name |
|---|
| 51 | ) |
|---|
| 52 | |
|---|
| 53 | content = response.content |
|---|
| 54 | if "html" in response._headers["content-type"][1]: |
|---|
| 55 | # Try to insert the info into a html valid way. |
|---|
| 56 | old_content = content |
|---|
| 57 | content = content.replace("</body>", "<h1>%s</h1></body>" % info) |
|---|
| 58 | if content == old_content: |
|---|
| 59 | # replacement not successful -> append it. |
|---|
| 60 | content += "\n\n" + info |
|---|
| 61 | else: |
|---|
| 62 | # Not a html reponse -> Append the info |
|---|
| 63 | content += "\n\n" + info |
|---|
| 64 | |
|---|
| 65 | response.content = content |
|---|
| 66 | |
|---|
| 67 | return response |
|---|