Changeset 1127

Show
Ignore:
Timestamp:
06/29/07 16:05:08 (3 years 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

Location:
branches/0.8(django)
Files:
3 added
12 modified
1 moved

Legend:

Unmodified
Added
Removed
  • branches/0.8(django)/dev_scripts/local_dev_tests/newform_select_user.py

    r1104 r1127  
    5151users_form = UsersForm(users) 
    5252 
     53for field in users_form: 
     54    for i in dir(field): 
     55        print i 
     56    print field.auto_id 
     57    print field.html_name 
     58 
    5359html = users_form.as_p() 
    5460print html 
  • branches/0.8(django)/index.cgi

    r1051 r1127  
    5555        -global header_send variable 
    5656        """ 
    57         oldFileinfo = "" 
    58         header_send = False 
     57        def __init__(self, out): 
     58            self.out = out 
     59            self.oldFileinfo = "" 
     60            self.header_send = False 
    5961 
    6062        def _get_fileinfo(self): 
     
    8789            message comes from. 
    8890            """ 
    89             stack = inspect.stack()[1] 
    90 #            fileinfo = (stack[1].split("/")[-1][-40:], stack[2]) 
    9191            fileinfo = self._get_fileinfo() 
    9292 
     
    110110        be send. 
    111111        """ 
    112         def __init__(self, out): 
    113             self.out = out 
    114  
    115112        def check(self, txt): 
    116113            txt_lower = txt.lower() 
     
    135132 
    136133        def wrong_header_info(self): 
    137             # Angaben zur Datei, Zeilennummer, aus dem die Nachricht stammt 
    138134            self.out.write("Content-type: text/html; charset=utf-8\r\n\r\n") 
    139135            self.out.write("Wrong Header!!!\n") 
     
    146142        Sends a header, if the header were not already sent. 
    147143        """ 
    148         def __init__(self, out): 
    149             self.out = out 
    150             self.header_send = False 
    151  
    152144        def write(self, *txt): 
    153145            txt = " ".join([i for i in txt]) 
  • branches/0.8(django)/PyLucid/defaulttags/lucidTag.py

    r1087 r1127  
     1#!/usr/bin/python 
     2# -*- coding: UTF-8 -*- 
    13 
    24""" 
     
    8082            raise AssertionError(msg) 
    8183 
    82 #        print content 
    83 #        print "---" 
    84  
    8584        return content 
    8685 
  • branches/0.8(django)/PyLucid/index.py

    r1121 r1127  
    4040from PyLucid.system.URLs import URLs 
    4141 
    42 from PyLucid.tools.content_processors import apply_markup, render_template 
     42from PyLucid.tools.content_processors import apply_markup, \ 
     43                                                        render_string_template 
    4344 
    4445 
     
    6364        ) 
    6465 
    65     current_page.content = render_template(current_page.content, context) 
     66    current_page.content = render_string_template(current_page.content, context) 
    6667 
    6768    template = current_page.template 
    6869    template_content = template.content 
    6970 
    70     html = render_template(template_content, context) 
    71  
    72 #    import cgi, pprint 
    73 #    print context 
    74 #    debug = "<hr/><pre>%s</pre></html>" % cgi.escape(pprint.pformat(context)) 
    75 #    html = html.replace("</html>", debug) 
    76  
     71    html = render_string_template(template_content, context) 
    7772    return HttpResponse(html) 
    7873 
     
    107102    # For additional JavaScript and StyleSheet information. 
    108103    # JS+CSS from internal_pages or CSS data for pygments 
     104    # Add into the context object. Would be integraged in the page with the 
     105    # additional_content middleware. 
    109106    context["js_data"] = [] 
    110107    context["css_data"] = [] 
     108 
     109    # Add the context to the reponse object. 
     110    # Used in PyLucid.middlewares.additional_content 
     111    request.CONTEXT = context 
    111112 
    112113    return context 
  • 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 
  • branches/0.8(django)/PyLucid/plugins_internal/admin_menu/admin_menu.py

    r1116 r1127  
    22# -*- coding: UTF-8 -*- 
    33 
    4 __author__  = "Jens Diemer (www.jensdiemer.de)" 
    5 __license__ = "GNU General Public License (GPL)" 
    6 __url__     = "http://www.PyLucid.org" 
     4""" 
     5    PyLucid admin menu 
     6    ~~~~~~~~~~~~~~~~~~ 
    77 
    8 """ 
    9 the administration top menu 
     8    The administration top and sub menu. 
    109 
    11 TODO: edit_page_link should use pageadmin.edit_page - a inline editing 
     10    Last commit info: 
     11    ~~~~~~~~~~~~~~~~~ 
     12    $LastChangedDate: 2007-06-26 10:37:35 +0200 (Di, 26 Jun 2007) $ 
     13    $Rev: 1111 $ 
     14    $Author: JensDiemer $ 
     15 
     16    :copyright: 2007 by Jens Diemer 
     17    :license: GNU GPL v2 or above, see LICENSE for more details 
    1218""" 
    1319 
     20 
    1421from PyLucid.system.BaseModule import PyLucidBaseModule 
     22 
    1523 
    1624class admin_menu(PyLucidBaseModule): 
     
    1826    def lucidTag(self): 
    1927        """ 
    20         Front menu anzeigen 
     28        Render the front menu 
    2129        """ 
    2230        current_page_id  = self.current_page.id 
     
    2432 
    2533        context = { 
     34            "login_link"    : self.context["login_link"], 
    2635            "edit_page_link": self.URLs.commandLink("page_admin", "edit_page"), 
    27 #            "edit_page_link": edit_link, 
    28 #            "new_page_link": self.URLs.adminLink("PyLucid/page/add/"), 
    29             "new_page_link": self.URLs.commandLink("page_admin", "new_page"), 
    30             "sub_menu_link": self.URLs.commandLink("admin_menu", "sub_menu"), 
     36            "new_page_link" : self.URLs.commandLink("page_admin", "new_page"), 
     37            "sub_menu_link" : self.URLs.methodLink("sub_menu"), 
    3138        } 
    32         self._render_template("top_menu", context) 
     39        self._render_template("top_menu", context)#, debug=True) 
    3340 
    3441    def sub_menu(self): 
     42        """ 
     43        render the sub menu 
     44        """ 
    3545        context = { 
    3646            "commandURLprefix": self.URLs.get_command_base(), 
    3747            "adminURLprefix": self.URLs["adminBase"], 
    3848        } 
    39 #        self.page_msg(context) 
    40  
    41         self._render_template("sub_menu", context) 
     49        self._render_template("sub_menu", context)#, debug=True) 
    4250 
    4351 
  • branches/0.8(django)/PyLucid/plugins_internal/page_style/page_style.py

    r1087 r1127  
    88    - Put the css html tag into the cms page. 
    99    - Send the current stylesheet directly to the client. 
     10 
     11    Note: 
     12    The page_style.lucidTag() method adds the additional_content ADD_DATA_TAG 
     13    into the page. 
     14    The middleware PyLucid.middlewares.additional_content replace the tag and 
     15    puts the collected CSS/JS contents into the page. 
    1016 
    1117    Last commit info: 
     
    2733from django.http import HttpResponse 
    2834 
     35from PyLucid.middlewares.additional_content import ADD_DATA_TAG 
    2936from PyLucid.models import Style 
    30  
    3137from PyLucid.system.BaseModule import PyLucidBaseModule 
    3238 
    3339class page_style(PyLucidBaseModule): 
    3440 
    35     #~ def __init__(self, *args, **kwargs): 
    36         #~ super(page_style, self).__init__(*args, **kwargs) 
    37  
    3841    def lucidTag(self): 
    39         # Schreibt den addCode-Tag, damit am Ende noch die CSS/JS Daten 
    40         # von Modulen eingefügt werden können 
    41         #~ self.response.write(self.response.addCode.tag) 
     42        """ 
     43        -Put a link to sendStyle into the page. 
     44        -Insert the ADD_DATA_TAG for the additional_content middleware 
     45        """ 
     46        self.response.write(ADD_DATA_TAG) 
    4247 
    4348        current_page = self.context["PAGE"] 
     
    4954        ) 
    5055        cssTag = '<link rel="stylesheet" type="text/css" href="%s" />\n' % url 
     56        self.response.write(cssTag) 
    5157 
    52         self.response.write(cssTag) 
    5358 
    5459    def print_current_style(self): 
    5560        """ 
    56         CSS direkt in die Seite einfügen 
     61        -Write the stylesheet directly into the page. 
     62        -Insert the ADD_DATA_TAG for the additional_content middleware 
     63        Used with the tag: {% lucidTag page_style.print_current_style %} 
    5764        """ 
    58         # Schreibt den addCode-Tag, damit am Ende noch die CSS/JS Daten 
    59         # von Modulen eingefügt werden können 
    60         self.response.write(self.response.addCode.tag) 
     65        self.response.write(ADD_DATA_TAG) 
    6166 
    62         page_id = self.session["page_id"] 
    63         getItems = ["content"] 
    64         css = self.db.side_style_by_id(page_id, getItems) 
    65         css = css["content"] 
     67        current_page = self.context["PAGE"] 
     68        stylesheet = current_page.style 
    6669 
    67         self.response.write('<style type="text/css">') 
    68         self.response.write(css) 
    69         self.response.write('</style>') 
     70        context = { 
     71            "content": stylesheet.content, 
     72        } 
     73        self._render_template("write_styles", context)#, debug=True) 
     74 
    7075 
    7176    def sendStyle(self, css_filename): 
    7277        """ 
    73         Sendet das CSS als Datei, da in die Seite nur ein Link eingefügt, der 
    74         jetzt "ausgeführt" wird. 
    75         Dabei wird eine "Browsercache-Anfrage" berücksichtigt. 
     78        send the stylesheet as a file to the client. 
     79        It's the request started with the link tag from self.lucidTag() ;) 
     80        TODO: Should insert some Headers for the browser cache. 
    7681        """ 
    7782        css_name = css_filename.split(".",1)[0] 
     
    9095        return response 
    9196 
    92         """ 
    93         timeFormat = "%a, %d %b %Y %H:%M:%S GMT" 
    94  
    95         # Ein "wirklich" frisches response-Object nehmen: 
    96         response = HttpResponse() 
    97         response.headers['Content-Type'] = 'text/css; charset=utf-8' 
    98  
    99         # Hop-by-hop Headers ist mit wsgiref nicht erlaubt: 
    100         #~ response.headers['Connection'] = "Keep-Alive" 
    101  
    102         page_id = self.session["page_id"] 
    103         getItems = ["id", "content", "lastupdatetime"] 
    104         cssData = self.db.side_style_by_id(page_id, getItems) 
    105  
    106         lastupdatetime = cssData["lastupdatetime"] # datetime-Object! 
    107  
    108         #~ raise str(repr(lastupdatetime)) 
    109  
    110         lastModified = lastupdatetime.strftime(timeFormat) 
    111         #~ print "lastModified:", lastModified 
    112  
    113         # 1Tag * 60Min * 60Sec = 3600Sec 
    114         response.headers['Cache-Control'] = 'max-age=3600' 
    115         delta = datetime.timedelta(days=1) 
    116         expires = lastupdatetime + delta 
    117         expires = expires.strftime(timeFormat) 
    118         #~ print "expires:", expires 
    119         response.headers['Expires'] = expires 
    120  
    121         # ID damit der Browser das Style eindeutige Identifizieren kann: 
    122         eTag = '"style-%s"' % cssData["id"] 
    123         response.headers['Etag'] = eTag 
    124  
    125         # Chaching im Browser überprüfen: 
    126         if "HTTP_IF_MODIFIED_SINCE" in self.environ and \ 
    127                                         "HTTP_IF_NONE_MATCH" in self.environ: 
    128             # Der Browser fragt nach, ob es die Daten aus seinem Chache nehmen 
    129             # soll. 
    130             send_modified = self.environ["HTTP_IF_MODIFIED_SINCE"] 
    131             send_eTag = self.environ["HTTP_IF_NONE_MATCH"] 
    132             if send_eTag == eTag and send_modified == lastModified: 
    133                 # CSS Daten haben sich nicht geändert! 
    134                 response.status = 304 # HTTP/1.x 304 Not Modified 
    135                 return response 
    136  
    137         # Die CSS Daten werden zum ersten mal gesendet oder diese haben 
    138         # sich seit dem letzten Aufruf geändert. 
    139  
    140         cssContent = cssData["content"] 
    141  
    142         # Content-Length kann nur in UTF8 und nicht richtig in Unicode 
    143         # ermittelt werden!!! 
    144         cssContent = cssContent.encode("utf8") 
    145         contentLen = len(cssContent) 
    146         response.headers['Content-Length'] = '%s' % contentLen 
    147  
    148         response.headers['Last-Modified'] = lastModified 
    149         response.headers['Content-Transfer-Encoding'] = '8bit' #'binary' 
    150         response.headers['Content-Type'] = \ 
    151             'text/css; charset=utf-8' 
    152  
    153         response.write(cssContent) 
    154  
    155         # force Windows input/output to binary 
    156         if sys.platform == "win32": 
    157             try: 
    158                 import msvcrt 
    159                 msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY) 
    160                 msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY) 
    161             except: 
    162                 pass 
    163  
    164         return response 
    165  
    166         """ 
    167  
    168  
    169  
    170  
    171  
  • branches/0.8(django)/PyLucid/plugins_internal/page_style/page_style_cfg.py

    r1103 r1127  
    1818        "must_login"    : False, 
    1919        "must_admin"    : False, 
     20        "internal_page_info" : { 
     21            # Note: This internal page is not used in page_style.lucidTag() 
     22            # The data used in PyLucid.middlewares.additional_content! 
     23            "name"              : "add_data", 
     24            "description"       : "Template for adding CSS/JS from plugins.", 
     25            "markup"            : None 
     26        }, 
    2027    }, 
    2128    "print_current_style" : { 
    2229        "must_login"    : False, 
    2330        "must_admin"    : False, 
     31        "internal_page_info" : { 
     32            "name"              : "write_styles", 
     33            "description"       : "Insert the stylesheets directly.", 
     34            "markup"            : None 
     35        }, 
    2436    }, 
    2537    "sendStyle" : { 
  • branches/0.8(django)/PyLucid/plugins_internal/page_update_list/page_update_list.py

    r1123 r1127  
    4141        self._render_template("PageUpdateTable", context)#, debug=True) 
    4242 
    43  
    44  
    45  
    46  
    47  
    48  
    49  
    50  
    51  
    52  
    53  
    54  
  • branches/0.8(django)/PyLucid/settings_example.py

    r1116 r1127  
    221221    'django.middleware.common.CommonMiddleware', 
    222222    'django.middleware.doc.XViewMiddleware', 
    223     'PyLucid.middlewares.pagestats.PageStatsMiddleware', 
     223 
     224    'PyLucid.middlewares.additional_content.AdditionalContentMiddleware', 
    224225) 
    225226 
  • branches/0.8(django)/PyLucid/system/BaseModule.py

    r1111 r1127  
    33 
    44""" 
    5 Basis Modul von den andere Module erben können 
     5    PyLucid BaseModule 
     6    ~~~~~~~~~~~~~~~~~~ 
    67 
    7 Bsp.: 
     8    The base Plugin object. Every Plugin can inherit. 
    89 
    9 from PyLucid.system.BaseModule import PyLucidBaseModule 
     10    e.g.: 
    1011 
    11 class Bsp(PyLucidBaseModule): 
    12     def __init__(self, *args, **kwargs): 
    13         super(Bsp, self).__init__(*args, **kwargs) 
     12        from PyLucid.system.BaseModule import PyLucidBaseModule 
     13 
     14        class Bsp(PyLucidBaseModule): 
     15            def __init__(self, *args, **kwargs): 
     16                super(Bsp, self).__init__(*args, **kwargs) 
     17 
     18 
     19    Last commit info: 
     20    ~~~~~~~~~~~~~~~~~ 
     21    $LastChangedDate$ 
     22    $Rev$ 
     23    $Author$ 
     24 
     25    :copyright: 2007 by Jens Diemer 
     26    :license: GNU GPL, see LICENSE for more details 
     27""" 
    1428 
    1529 
    1630 
    17 Last commit info: 
    18 ---------------------------------- 
    19 $LastChangedDate$ 
    20 $Rev$ 
    21 $Author$ 
    22  
    23 Created by Jens Diemer 
    24  
    25 license: 
    26     GNU General Public License v2 or above 
    27     http://www.opensource.org/licenses/gpl-license.php 
    28 """ 
    29  
    30 from PyLucid.models import PagesInternal 
    31 from PyLucid.tools.content_processors import apply_markup, render_template 
    32  
    33  
    34 #______________________________________________________________________________ 
     31from PyLucid.db.internal_pages import get_internal_page 
     32from PyLucid.tools.content_processors import apply_markup, \ 
     33                                                        render_string_template 
    3534 
    3635 
     
    6564 
    6665    def _get_template(self, internal_page_name): 
    67         plugin_name = self.__class__.__name__ # Get the superior class name 
    68  
    69         internal_page_name = ".".join([plugin_name, internal_page_name]) 
    70  
    71         # django bug work-a-round 
    72         # http://groups.google.com/group/django-developers/browse_thread/thread/e1ed7f81e54e724a 
    73         internal_page_name = internal_page_name.replace("_", " ") 
    74  
    75         try: 
    76             return PagesInternal.objects.get(name = internal_page_name) 
    77         except PagesInternal.DoesNotExist, err: 
    78             msg = "internal page '%s' not found! (%s)" % (internal_page_name, err) 
    79             raise PagesInternal.DoesNotExist(msg) 
    80  
     66        """ 
     67        retuned the internal page object 
     68        Get the plugin name throu the superior class name 
     69        """ 
     70        plugin_name = self.__class__.__name__ 
     71        internal_page = get_internal_page(plugin_name, internal_page_name) 
     72        return internal_page 
    8173 
    8274    def _add_js_css_data(self, internal_page): 
     
    134126        """ 
    135127        html = self.__render(template, context, debug) 
    136  
    137128        self.response.write(html) 
    138129 
    139130    def __render(self, content, context, debug=False): 
    140131        """ 
    141         render the string with the given context 
     132        render the content string with the given context and returned it. 
    142133        -debug the context, if debug is on. 
    143         -prepare the context 
    144         -retunted the rendered page 
    145134        """ 
    146135        if debug: 
    147136            self._debug_context(context, content) 
    148137 
    149         html = render_template(content, self.context, context) 
     138        html = render_string_template(content, context) 
    150139        return html 
    151140 
  • branches/0.8(django)/PyLucid/system/plugin_manager.py

    r1110 r1127  
    146146 
    147147    output = unbound_method(*url_args, **method_kwargs) 
     148 
    148149    return output 
    149150 
  • branches/0.8(django)/PyLucid/tools/content_processors.py

    r1101 r1127  
     1#!/usr/bin/python 
     2# -*- coding: UTF-8 -*- 
    13 
    24""" 
     
    9395 
    9496 
    95 def render_string_template(template, context): 
    96     """ 
    97     Render a string-template with the given context 
    98     """ 
    99     context = Context(context) 
    100     template = Template(template) 
    101     html = template.render(context) 
    102     return html 
    103  
    104 def render_template(content, global_context, local_context={}): 
     97def render_string_template(content, context): 
    10598    """ 
    10699    Render a template. 
    107     1. put all local context items in the global context 
    108     2. render with the merged context 
    109     Note: 
    110     - The local_context are content for a internal page. 
    111     - We merged the local- and global-context together, so every internal 
    112     page can display something from the global context, like page name... 
    113100    """ 
    114     global_context.update(local_context) 
    115  
    116     html = render_string_template(content, global_context) 
    117  
     101    context2 = Context(context) 
     102    template = Template(content) 
     103    html = template.render(context2) 
    118104    return html