Show
Ignore:
Timestamp:
06/29/07 16:05:08 (17 months ago)
Author:
JensDiemer
Message:

* new additional Stylesheet/JavaScript handling. A middleware puts the collected data into the page.
* middleware "pagestats" renamed to "additional_content".
* some code cleanup

Files:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/0.8(django)/PyLucid/middlewares/additional_content.py

    r1090 r1127  
    33 
    44""" 
    5     PyLucid page statistics 
    6     ~~~~~~~~~~~~~~~~~~~~~~~ 
     5    PyLucid additional page content 
     6    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    77 
     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    ~~~~~~~~~~~~~ 
    820    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. 
    1022 
    1123    Based on http://code.djangoproject.com/wiki/PageStatsMiddleware 
     24 
    1225 
    1326    Last commit info: 
     
    2538from django.db import connection 
    2639 
     40from PyLucid.db.internal_pages import get_internal_page 
     41from PyLucid.tools.content_processors import render_string_template 
     42 
    2743start_overall = time() 
    2844 
    29 TAG = "<!-- script_duration -->" 
     45PAGE_STAT_TAG = "<!-- script_duration -->" 
     46ADD_DATA_TAG = "<!-- additional_data -->" 
    3047 
    3148FMT = ( 
     
    3552) 
    3653 
    37 class PageStatsMiddleware(object): 
     54class AdditionalContentMiddleware(object): 
    3855    def process_view(self, request, view_func, view_args, view_kwargs): 
    3956        start_time = time() 
     
    5067            return response 
    5168 
     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        """ 
    52113        # compute the db time for the queries just run 
    53114        queries = len(connection.queries) - old_queries 
     
    60121        } 
    61122 
    62         response.content = response.content.replace(TAG, stat_info) 
     123        content = content.replace(PAGE_STAT_TAG, stat_info) 
    63124 
    64         return response 
     125        return content