Changeset 2037

Show
Ignore:
Timestamp:
06/17/09 09:19:14 (9 months ago)
Author:
JensDiemer
Message:
  • change the way extra head content would be stored: So ajax view can use {% extrahead %}.
  • merge some JavaScript? code
  • add needfull shortcut for "ajax+normal response" views.
Location:
branches/0.9/pylucid_project
Files:
2 added
12 modified

Legend:

Unmodified
Added
Removed
  • branches/0.9/pylucid_project/apps/pylucid/defaulttags/extraheadBlock.py

    r2022 r2037  
    66     
    77    Simple django template block tag. "Redirect" extra head content 
    8     into pylucid_plugin.head_files.context_middleware 
     8    into request.PYLUCID.extrahead 
     9    This data would be inserted with pylucid_plugin.extrahead.context_middleware 
    910     
    1011    PyLucid plugins should use {% extrahead %} block tag in plugin template for 
     
    4041            raise RuntimeError("Plugin must use RequestContext() or request.PYLUCID.context !") 
    4142          
    42         head_files = request.PYLUCID.context["context_middlewares"]["extrahead"] 
    43         head_files.add_content(output) 
     43        extrahead = request.PYLUCID.extrahead 
     44        extrahead.append(output) 
    4445        return u"" 
  • branches/0.9/pylucid_project/apps/pylucid/system/pylucid_objects.py

    r2027 r2037  
    11# coding:utf-8 
    22 
     3""" 
     4    PyLucid objects 
     5    ~~~~~~~~~~~~~~~ 
     6     
     7    Some need full objects attached to the current request object in pylucid.views. 
     8     
     9    see also: http://www.pylucid.org/_goto/187/PyLucid-objects/ 
     10 
     11    Last commit info: 
     12    ~~~~~~~~~~~~~~~~~ 
     13    $LastChangedDate: $ 
     14    $Rev: $ 
     15    $Author: $ 
     16 
     17    :copyleft: 2009 by the PyLucid team, see AUTHORS for more details. 
     18    :license: GNU GPL v3 or above, see LICENSE for more details. 
     19""" 
     20 
    321from pylucid.models import Language 
     22from pylucid.system import extrahead 
    423from pylucid.preference_forms import SystemPreferencesForm 
    524 
    625class PyLucidRequestObjects(object): 
    7     """ 
    8     PyLucid request objects 
    9     see also: http://www.pylucid.org/_goto/187/PyLucid-objects/ 
    10     """ 
     26    """ PyLucid request objects """ 
    1127    def __init__(self): 
    1228        self.system_preferences = SystemPreferencesForm().get_preferences() 
     
    1430        self.default_lang_code = default_lang_code 
    1531        self.default_lang_entry = Language.objects.get(code=default_lang_code) 
     32         
     33        # Storing extra html head code from plugins, used in: 
     34        # pylucid.defaulttags.extraheadBlock - redirect {% extrahead %} block tag content 
     35        # pylucid_plugin.extrahead.context_middleware - insert the data into the global page 
     36        self.extrahead = extrahead.ExtraHead() 
     37         
    1638        # objects witch will be set later: 
    1739        #self.pagetree - The current PageTree model instance 
  • branches/0.9/pylucid_project/media/PyLucid/pylucid_js_tools.js

    r2034 r2037  
    1616 
    1717 
     18function pylucid_ajax_form_view(form_id) { 
     19    /************************************************************************* 
     20    PyLucid ajax form view. 
     21     
     22    Don't send the form and get a complete new rendered page. Send the form 
     23    via ajax post and replace the #page_content with the html response. 
     24     
     25    usage e.g.: 
     26    ---------------------------------------------------------------------- 
     27            $(document).ready(function(){ 
     28                // simply bind the form with the id: 
     29                pylucid_ajax_form_view('#form_id'); 
     30            }); 
     31    ---------------------------------------------------------------------- 
     32    *************************************************************************/ 
     33    $(form_id).bind('submit', function() { 
     34         
     35        $("#page_content").html('<h2>loading...</h2>'); 
     36        $("#page_content").animate({opacity: 0.3}, 500 ); 
     37 
     38        var form = $(this); 
     39        var form_data = form.serialize(); 
     40        log("form data:" + form_data); 
     41         
     42        var url = encodeURI(form.attr('action')); 
     43        log("send form to url:" + url); 
     44         
     45        var load_normal_link = true; 
     46         
     47        $.ajax({ 
     48                async: false, 
     49            type: "POST", 
     50            url: url, 
     51            data: form_data, 
     52            dataType: "html", 
     53             
     54            success: function(form_html){ 
     55                        log("ajax post response success."); 
     56                $("#page_content").html(form_html); 
     57                $("#page_content").animate({opacity: 1}, 500 ); 
     58                load_normal_link = false; 
     59            }, 
     60            error: function(XMLHttpRequest){ 
     61                log("ajax get response error!"); 
     62                // Display the complete Traceback html page 
     63                log(XMLHttpRequest); 
     64                var response_text = XMLHttpRequest.responseText; 
     65                log("response_text: '" + response_text + "'"); 
     66                if (!response_text) { 
     67                        document.write("<h1>ajax response error</h1>"); 
     68                } else { 
     69                        document.write(response_text); 
     70                } 
     71                load_normal_link = true; 
     72            } 
     73        });        
     74        return load_normal_link; // <-- important: Don't send the form in normal way. 
     75    });  
     76} 
     77 
     78 
    1879 
    1980function get_pylucid_ajax_view(url) { 
    2081    /************************************************************************* 
    21         PyLucid ajax replace function 
    22          
    23         usage e.g.: 
    24         ---------------------------------------------------------------------- 
    25             $(document).ready(function(){ 
    26                 $("#link_id").click(function(){ 
    27                     return get_pylucid_ajax_view("{{ ajax_get_view_url }}"); 
    28                 }); 
     82    PyLucid ajax get view replace. 
     83     
     84    Don't render the complete page again. Simply get the new content via ajax 
     85    and replace #page_content with it. 
     86     
     87    usage e.g.: 
     88    ---------------------------------------------------------------------- 
     89        $(document).ready(function(){ 
     90            $("#link_id").click(function(){ 
     91                return get_pylucid_ajax_view("{{ ajax_get_view_url }}"); 
    2992            }); 
    30         ---------------------------------------------------------------------- 
    31         or: 
    32         ---------------------------------------------------------------------- 
    33         <a href="{{ url }}" onclick="return get_pylucid_ajax_view('{{ ajax_url }}');">foo</a> 
    34         ---------------------------------------------------------------------- 
     93        }); 
     94    ---------------------------------------------------------------------- 
     95    or: 
     96    ---------------------------------------------------------------------- 
     97    <a href="{{ url }}" onclick="return get_pylucid_ajax_view('{{ ajax_url }}');">foo</a> 
     98    ---------------------------------------------------------------------- 
    3599    *************************************************************************/ 
    36100    $("#page_content").html('<h2>loading...</h2>'); 
  • branches/0.9/pylucid_project/pylucid_plugins/admin_menu/templates/admin_menu/admin_top_menu.html

    r2034 r2037  
    99<script type="text/javascript" src="{{ PyLucid_media_url }}superfish/superfish.js" onerror="JavaScript:alert('{{ PyLucid_media_url }}superfish/superfish.js');" ></script> 
    1010<script type="text/javascript" src="{{ PyLucid_media_url }}superfish/pylucid_superfish_init.js" onerror="JavaScript:alert('{{ PyLucid_media_url }}superfish/pylucid_superfish_init.js');" ></script> 
    11 <script type="text/javascript" src="{{ PyLucid_media_url }}pylucid_js_tools.js" onerror="JavaScript:alert('{{ PyLucid_media_url }}pylucid_js_tools.js');" ></script> 
    1211<style type="text/css"> 
    1312<!-- 
     
    2625    <li class="current">{% lucidTag auth %}</li> 
    2726    <li> 
    28         <li><a href="{{ edit_page_link }}" title="edit inline" id="edit_page" onclick="return get_pylucid_ajax_view('{{ edit_page_ajax}}');">{% trans 'edit page' %}</a></li> 
     27        <li><a href="{{ edit_page_link }}" title="edit inline" id="edit_page" onclick="return get_pylucid_ajax_view('{{ edit_page_link }}');">{% trans 'edit page' %}</a></li> 
    2928        <ul> 
    3029            <li><a href="{{ edit_admin_panel_link }}">edit in admin panel</a></li> 
  • branches/0.9/pylucid_project/pylucid_plugins/admin_menu/views.py

    r2029 r2037  
    3131         
    3232        "edit_page_link": "?page_admin=inline_edit", 
    33         "edit_page_ajax": "?page_admin=get_ajax_form", 
    3433         
    3534        "edit_admin_panel_link": edit_admin_panel_link, 
  • branches/0.9/pylucid_project/pylucid_plugins/auth/templates/auth/input_username.html

    r2034 r2037  
    22 
    33{% block plugin_content %} 
    4 {# We can't use extrahead block here, because it's used in ajax view, too. #} 
     4 
     5{% extrahead %} 
    56<script type="text/javascript"> 
    6  
    77$(document).ready(function(){ 
    88    log("input username ready"); 
    99    $("#id_username").focus(); 
    1010     
    11     $('#username_form').bind('submit', function() { 
    12         // get the input password form via ajax 
    13         $("#page_content").html('<h2>loading...</h2>'); 
    14         $("#page_content").animate({opacity: 0.3}, 500 ); 
    15      
    16         log("continue login..."); 
    17  
    18         var form = $(this); 
    19         var form_data = form.serialize(); 
    20         log("form data:" + form_data); 
    21          
    22         var url = encodeURI(form.attr('action')); 
    23         log("form url:" + url); 
    24          
    25             $.post(url, form_data, 
    26                 function(form_html){ 
    27                     log("ajax get success."); 
    28                     $("#page_content").html(form_html); 
    29                     $("#page_content").animate({opacity: 1}, 500 ); 
    30                 } 
    31             ); 
    32          
    33         return false; // <-- important! 
    34     });     
     11    pylucid_ajax_form_view('#username_form'); // send username form and get the input password form via ajax 
    3512}); 
    3613</script> 
     14{% endextrahead %} 
    3715 
    3816<fieldset><legend>PyLucid <a href="http://www.pylucid.org/_goto/8/JS-SHA-Login/" title="What is the PyLucid JS-SHA-LogIn?">JS-SHA-LogIn</a> - step 1</legend> 
  • branches/0.9/pylucid_project/pylucid_plugins/auth/templates/auth/login_link.html

    r2034 r2037  
    22 
    33{% block plugin_content %} 
    4 <a href="{{ login_url }}" id="login_link" onclick="return get_pylucid_ajax_view('{{ login_url_ajax }}');">{% trans 'Log in' %}</a> 
     4<a href="{{ url }}" id="login_link" onclick="return get_pylucid_ajax_view('{{ url }}');">{% trans 'Log in' %}</a> 
    55{% endblock %} 
  • branches/0.9/pylucid_project/pylucid_plugins/auth/templates/auth/logout_link.html

    r2034 r2037  
    1 <a href="{{ logout_url }}">{% trans 'Log out' %} [{{ request.user.username }}]</a> 
     1<a href="{{ url }}">{% trans 'Log out' %} [{{ request.user.username }}]</a> 
  • branches/0.9/pylucid_project/pylucid_plugins/auth/views.py

    r2034 r2037  
    55from django.template import RequestContext 
    66from django.http import HttpResponseRedirect 
    7 from django.shortcuts import render_to_response 
    87from django.utils.translation import ugettext as _ 
    98from django.template.loader import render_to_string 
     9 
     10from pylucid.shortcuts import render_pylucid_response 
    1011 
    1112from pylucid_project.utils import crypt 
     
    2526 
    2627 
     28 
     29 
     30 
    2731def lucidTag(request): 
    28     # TODO: Use internal SHA-Login plugin views, if implemented: 
     32    """ Create login/logout link """ 
    2933    if request.user.is_authenticated(): 
    3034        # admin_logout reverse is still broken in django, see: 
     
    3438        #url = reverse("admin_index") + "logout/" # TODO: Update this if django is bugfixed 
    3539        template_name = "auth/logout_link.html" 
    36         context = {"logout_url": "?auth=logout"} 
     40        url = "?auth=logout" 
    3741    else: 
    3842        template_name = "auth/login_link.html" 
    39         #url = reverse("admin_index") # django admin panel index page 
    40         context = { 
    41             "login_url": "?auth=login", 
    42             "login_url_ajax": "?auth=get_login_form", 
    43         } 
    44      
    45     return render_to_string(template_name, context, context_instance=RequestContext(request)) 
     43        url = "?auth=login" 
     44            
     45    return render_to_string(template_name, {"url": url}, context_instance=RequestContext(request)) 
    4646 
    4747 
     
    6666 
    6767 
    68 def _plaintext_login(request, context, username, next_url, render_function): 
     68def _plaintext_login(request, context, username, next_url): 
    6969    """ input the password and login if auth ok """ 
    7070    if "password" in request.POST: 
     
    7979     
    8080    # return a string for replacing the normal cms page content 
    81     return render_function('auth/plaintext_login.html', context) 
    82  
    83  
    84      
    85 def _sha_login(request, context, user, next_url, render_function): 
     81    return render_pylucid_response(request, 'auth/plaintext_login.html', context) 
     82 
     83 
     84     
     85def _sha_login(request, context, user, next_url): 
    8686    """ 
    8787    Login via JS-SHA-Login. 
     
    145145 
    146146    # return a string for replacing the normal cms page content 
    147     return render_function('auth/input_password.html', context, context_instance=RequestContext(request)) 
    148  
    149  
    150 def _login_view(request, form_url, next_url, render_function): 
     147    return render_pylucid_response(request, 'auth/input_password.html', context, 
     148        context_instance=RequestContext(request) 
     149    ) 
     150 
     151 
     152def _login_view(request, form_url, next_url): 
    151153    if DEBUG: 
    152154        request.page_msg( 
     
    168170             
    169171            if "plaintext_login" in request.POST: 
    170                 return _plaintext_login(request, context, user.username, next_url, render_function) 
     172                return _plaintext_login(request, context, user.username, next_url) 
    171173            else: 
    172                 return _sha_login(request, context, user, next_url, render_function) 
     174                return _sha_login(request, context, user, next_url) 
    173175    else: 
    174176        username_form = UsernameForm() 
     
    177179     
    178180    # return a string for replacing the normal cms page content 
    179     return render_function('auth/input_username.html', context) 
     181    return render_pylucid_response(request, 'auth/input_username.html', context) 
    180182 
    181183 
     
    185187    """ 
    186188    next_url = request.path 
    187 #    import time 
    188 #    time.sleep(0.5) 
    189189    action = request.GET["auth"] 
    190190    if action=="login": 
    191191        form_url = request.path + "?auth=login" # FIXME: How can we add the GET Parameter? 
    192         return _login_view(request, form_url, next_url, render_function=render_to_string) 
    193     elif action=="get_login_form": 
    194         form_url = request.path + "?auth=get_login_form" # FIXME: How can we add the GET Parameter? 
    195         return _login_view(request, form_url, next_url, render_function=render_to_response) 
     192        return _login_view(request, form_url, next_url) 
    196193    elif action=="logout": 
    197194        return _logout_view(request, next_url) 
  • branches/0.9/pylucid_project/pylucid_plugins/extrahead/context_middleware.py

    r2034 r2037  
    55    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    66 
    7     Stores extra html head content via PyLucid context middleware. 
    8     It's used by pylucid.defaulttags.extraheadBlock. 
     7    replace <!-- ContextMiddleware extrahead --> in the global page template with 
     8    all extra head html code, stored in request.PYLUCID.extrahead (pylucid.system.extrahead) 
    99     
    10     PyLucid plugins should use {% extrahead %} block tag in plugin template for 
    11     insert e.g. CSS/JS file links into html head. 
     10    Add all headfile links from pagetree.design.headfiles m2m. 
     11     
     12    PyLucid plugins should use {% extrahead %} block tag (pylucid.defaulttags.extraheadBlock) 
     13    in plugin template for insert e.g. CSS/JS file links into html head. 
    1214 
    1315    Last commit info: 
     
    2931 
    3032 
    31 FILEPATH_SPLIT = "pylucid_project" 
    32  
    33 SKIP_MODULES = [ 
    34     os.path.join("django", "template"), 
    35     os.path.join("django", "shortcuts"), 
    36     os.path.join("extrahead", "context_middleware") 
    37 ] 
    38  
    39 DEBUG_INFO = """\ 
    40 <!-- extrahead from %(fileinfo)s - START --> 
    41 %(content)s 
    42 <!-- extrahead from %(fileinfo)s - END -->""" 
    43  
    44  
    4533class ContextMiddleware(object): 
    46     """ 
    47     Simple store extra html head content from plugins. 
    48     """ 
     34    """ replace <!-- ContextMiddleware extrahead --> in the global page template """ 
    4935    def __init__(self, request, context): 
    5036        self.request = request 
    51         if settings.DEBUG: 
    52             # Turn debug mode in JavaScript on 
    53             self.data = DEBUG_INFO % { 
    54                 "fileinfo": os.path.basename(__file__), 
    55                 "content": '<script type="text/javascript">var debug=true;log("debug is on");</script>' 
    56             } 
    57             self.data += "\n"  
    58         else: 
    59             self.data = "" 
    60          
    61     def add_content(self, content): 
    62         content = content.strip() 
    63         if settings.DEBUG: 
    64             # Add debug info around content. 
    65             fileinfo = self._get_fileinfo() 
    66             content = DEBUG_INFO % {"fileinfo": fileinfo, "content": content} 
    67              
    68         self.data += content + "\n" 
    69          
     37        self.extrahead = request.PYLUCID.extrahead # pylucid.system.extrahead 
     38                             
    7039    def _add_pagetree_headfiles(self): 
     40        """ add all headfile links used in the current design. """ 
    7141        pagetree = self.request.PYLUCID.pagetree 
    7242        design = pagetree.design 
     
    7444        headfiles = design.headfiles.all() 
    7545         
    76         headfilelinks = [] 
    7746        for headfile in headfiles: 
    7847            # Get a instance from pylucid.system.headfile.HeadfileLink(): 
    7948            headfilelink = headfile.get_headfilelink() 
    8049            head_tag = headfilelink.get_head_tag() 
    81             headfilelinks.append(head_tag) 
    82          
    83         self.add_content("\n".join(headfilelinks)) 
     50            self.extrahead.append(head_tag) 
    8451         
    8552    def render(self): 
     53        """ return all extra head content with all headfiles from current used design """ 
    8654        self._add_pagetree_headfiles() 
    87         return self.data 
    88      
    89     def _get_fileinfo(self): 
    90         """ 
    91         return fileinfo: Where from the announcement comes? 
    92         """ 
    93         def skip(filepath): 
    94             for ignore_path in SKIP_MODULES: 
    95                 if ignore_path in filepath: 
    96                     return True 
    97             return False 
    98          
    99         try:     
    100             fileinfo = [] 
    101             step = 0 
    102             for stack_frame in inspect.stack(): 
    103                 filepath = stack_frame[1]               
    104                 lineno = stack_frame[2] 
     55        return "\n".join(self.extrahead) 
    10556 
    106                 if skip(filepath) or FILEPATH_SPLIT not in filepath: 
    107                     continue 
    108                  
    109                 filepath = "..." + filepath.split(FILEPATH_SPLIT,1)[1] 
    110                  
    111                 fileinfo.append("%s line %s" % (filepath, lineno)) 
    112                 if step>=1: 
    113                     break 
    114                 step += 1 
    115             return " | ".join(fileinfo) 
    116         except Exception, e: 
    117             return "(inspect Error: %s)" % e 
  • branches/0.9/pylucid_project/pylucid_plugins/page_admin/templates/page_admin/edit_page_form.html

    r2034 r2037  
    11{% extends "pylucid/css_anchor_div.html" %} 
     2 
    23 
    34{% block plugin_content %} 
    45 
    5 {# We can't use extrahead block here, because it's used in ajax view, too. #} 
     6{% extrahead %} 
    67<script type="text/javascript"> 
    78$(document).ready(function(){ 
    89    $("#preview_fieldset").hide(); 
    910     
     11    pylucid_ajax_form_view('#edit_page_form'); 
     12         
    1013    $("#preview").click(function() { 
    1114        $("#edit_page_preview").html("<h2>loading...</h2>"); 
     
    2225             
    2326        }); 
    24       return false; // The browser submit the form, if true 
     27      return false; // <-- important: Don't send the form in normal way. 
    2528    }); 
    2629}); 
    2730</script> 
     31{% endextrahead %} 
    2832 
    2933<fieldset id="preview_fieldset"> 
  • branches/0.9/pylucid_project/pylucid_plugins/page_admin/views.py

    r2034 r2037  
    55from django.conf import settings 
    66from django.template import RequestContext 
    7 from django.shortcuts import render_to_response 
    87from django.utils.translation import ugettext as _ 
    98from django.core.exceptions import PermissionDenied 
    10 from django.template.loader import render_to_string 
    119from django.http import HttpResponse, HttpResponseRedirect 
    1210 
    1311from pylucid.markup.converter import apply_markup 
     12from pylucid.shortcuts import render_pylucid_response 
    1413 
    1514from page_admin.forms import EditPageForm 
     
    3534 
    3635 
    37 def _edit_page(request, render_function): 
     36def _edit_page(request, form_url): 
    3837    check_permissions(request, EDIT_PERMISSIONS) 
    3938     
     
    5352     
    5453    context = { 
    55         "form_url": "%s?page_admin=inline_edit" % request.path, 
     54        "form_url": form_url, 
    5655        "abort_url": request.path, 
    5756        "preview_url": "%s?page_admin=preview" % request.path, 
     
    6463        "pagemeta_instance": pagemeta_instance, 
    6564    } 
    66     return render_function('page_admin/edit_page_form.html', context,  
     65    return render_pylucid_response(request, 'page_admin/edit_page_form.html', context,  
    6766        context_instance=RequestContext(request) 
    6867    ) 
     
    8988    if action=="inline_edit": 
    9089        # replace the page content with the edit page form 
    91         return _edit_page(request, render_function=render_to_string) 
    92     elif action=="get_ajax_form": 
    93         # return the edit page form only, for inserting it via jQuery 
    94         return _edit_page(request, render_function=render_to_response) 
     90        form_url = "%s?page_admin=inline_edit" % request.path 
     91        return _edit_page(request, form_url) 
    9592    elif action=="preview": 
    9693        # preview via jQuery