- Timestamp:
- 06/29/07 16:05:08 (17 months ago)
- Files:
-
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/0.8(django)/PyLucid/middlewares/additional_content.py
r1090 r1127 3 3 4 4 """ 5 PyLucid page statistics6 ~~~~~~~~~~~~~~~~~~~~~~~ 5 PyLucid additional page content 6 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 7 7 8 Replace some Tags after the cms page is completly created: 9 10 ADD_DATA_TAG 11 ~~~~~~~~~~~~ 12 The Stylesheet and JavaScript data from the internal pages are stored in 13 a list: context["js_data"] and context["css_data"] 14 The page_style Plugin insert the ADD_DATA_TAG into the response content. 15 Here we replace the tag with the collected CSS/JS contents. 16 17 18 PAGE_STAT_TAG 19 ~~~~~~~~~~~~~ 8 20 A small page statistic middleware. 9 -replace the > TAG< with some stats. But only in HTML pages.21 -replace the >PAGE_STAT_TAG< with some stats. But only in HTML pages. 10 22 11 23 Based on http://code.djangoproject.com/wiki/PageStatsMiddleware 24 12 25 13 26 Last commit info: … … 25 38 from django.db import connection 26 39 40 from PyLucid.db.internal_pages import get_internal_page 41 from PyLucid.tools.content_processors import render_string_template 42 27 43 start_overall = time() 28 44 29 TAG = "<!-- script_duration -->" 45 PAGE_STAT_TAG = "<!-- script_duration -->" 46 ADD_DATA_TAG = "<!-- additional_data -->" 30 47 31 48 FMT = ( … … 35 52 ) 36 53 37 class PageStatsMiddleware(object):54 class AdditionalContentMiddleware(object): 38 55 def process_view(self, request, view_func, view_args, view_kwargs): 39 56 start_time = time() … … 50 67 return response 51 68 69 content = response.content 70 71 content = self._add_data(content, request) 72 content = self._page_stat(content, start_time, old_queries) 73 74 response.content = content 75 76 return response 77 78 79 def _add_data(self, content, request): 80 """ 81 Replace the ADD_DATA_TAG 82 add CSS/JS data from the Plugins into the page. 83 Note: The ADD_DATA_TAG puts the page_style plugin into the content! 84 """ 85 try: 86 context = request.CONTEXT 87 except AttributeError: 88 # In the _install section, we need no add_data... There is no 89 # CONTEXT object in the request object. -> Do nothing 90 return content 91 92 try: 93 internal_page = get_internal_page("page_style", "add_data") 94 internal_page_content = internal_page.content_html 95 96 context = { 97 "js_data": context["js_data"], 98 "css_data": context["css_data"], 99 } 100 html = render_string_template(internal_page_content, context) 101 except Exception, msg: 102 html = "<!-- Replace the ADD_DATA_TAG error: %s -->" % msg 103 104 content = content.replace(ADD_DATA_TAG, html) 105 106 return content 107 108 109 def _page_stat(self, content, start_time, old_queries): 110 """ 111 Replace the PAGE_STAT_TAG 112 """ 52 113 # compute the db time for the queries just run 53 114 queries = len(connection.queries) - old_queries … … 60 121 } 61 122 62 response.content = response.content.replace(TAG, stat_info)123 content = content.replace(PAGE_STAT_TAG, stat_info) 63 124 64 return response125 return content
