Changeset 1821

Show
Ignore:
Timestamp:
02/11/09 16:32:47 (12 months ago)
Author:
JensDiemer
Message:

NEW: Overwrite the used stylesheet/template...

Location:
trunk/pylucid_project
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/pylucid_project/PyLucid/index.py

    r1760 r1821  
    3838                                                        render_string_template 
    3939from PyLucid.plugins_internal.page_style.page_style import replace_add_data 
     40 
     41# ToDo: get_template should be move into a better place: 
     42from PyLucid.plugins_internal.page_style.page_style import get_template 
    4043 
    4144 
     
    128131    context["PAGE"].content = page_content 
    129132 
    130     template = current_page.template 
     133    # Get the template instance: consider the overwrite from page_style.switch() 
     134    # or get the template from the current page object. 
     135    template = get_template(request) 
     136     
     137    # Get the content from the model instance 
    131138    template_content = template.content 
    132139 
  • trunk/pylucid_project/PyLucid/plugins_internal/page_style/page_style.py

    r1634 r1821  
    3535import sys, os, datetime 
    3636 
     37from django import forms 
     38from django.conf import settings 
     39from django.http import HttpResponse, Http404 
    3740from django.utils.translation import ugettext as _ 
    38 from django.http import HttpResponse, Http404 
    39 from django.conf import settings 
    40  
    41 from PyLucid.models import Style 
     41 
     42from PyLucid.models import Style, Template 
    4243from PyLucid.system.BasePlugin import PyLucidBasePlugin 
    4344from PyLucid.tools.content_processors import render_string_template 
    4445 
     46 
     47class SwitchForm(forms.Form): 
     48    """ 
     49    Select style/template for overwriting. 
     50    """ 
     51    style = forms.ModelChoiceField(queryset=Style.objects.all()) 
     52    template = forms.ModelChoiceField(queryset=Template.objects.all()) 
     53 
     54 
     55 
     56#------------------------------------------------------------------------------ 
     57# ToDo: move the "get style/template" stuff out from here... 
     58 
     59 
     60def tolerant_delete(obj, key): 
     61    """ 
     62    Usefull for e.g.: 
     63    tolerant_delete(request.session, "style") 
     64    tolerant_delete(request.session, "template") 
     65    """ 
     66    try: 
     67        del obj[key] 
     68    except KeyError: 
     69        pass 
     70 
     71 
     72 
     73 
     74 
     75class StyleTemplate(object): 
     76    """ 
     77    Generic class for getting template/style instance. 
     78     
     79    1. Use the information in request.session["style" / "template"] and 
     80        get the instance from database. 
     81    2. Get style/template from the current page object 
     82    """ 
     83    def __init__(self, request, Model, key): 
     84        self.request = request 
     85        self.Model = Model # PyLucid.models.Style or PyLucid.models.Template 
     86        self.key = key # "style" or "template" 
     87 
     88    def get_from_session(self): 
     89        """ 
     90        Get style/template instance from database, if the name of it are stored 
     91        in the session. 
     92        """ 
     93        item_name = self.request.session.get(self.key, None) 
     94        if not item_name: 
     95            # No override saved in the session data 
     96            return 
     97         
     98        # Try to get the item from the database 
     99        try: 
     100            instance = self.Model.objects.get(name=item_name) 
     101        except: 
     102            # ToDo: self.Model.DoesNotExist should work here, isn't it? 
     103            tolerant_delete(self.request.session, self.key) 
     104            return 
     105        else: 
     106#            self.request.page_msg( 
     107#                "Info: Use %r as %s" % (instance.name, self.Model.__name__) 
     108#            ) 
     109            return instance 
     110 
     111    def get(self): 
     112        """ 
     113        return the instance 
     114        """ 
     115        instance = self.get_from_session() 
     116        if instance: 
     117            return instance 
     118         
     119        # Get style/template from the current page object 
     120        context = self.request.CONTEXT  
     121        current_page = context["PAGE"] 
     122        instance = getattr(current_page, self.key) 
     123        return instance 
     124 
     125 
     126def get_template(request): 
     127    return StyleTemplate(request, Template, "template").get() 
     128 
     129def get_style(request): 
     130    return StyleTemplate(request, Style, "style").get() 
     131 
     132 
     133 
     134#------------------------------------------------------------------------------ 
     135 
     136 
     137 
    45138class page_style(PyLucidBasePlugin): 
    46139 
     
    52145        self.response.write(settings.ADD_DATA_TAG) 
    53146 
    54         current_page = self.context["PAGE"] 
    55         current_style = current_page.style 
     147        current_style = get_style(self.request) 
    56148 
    57149        style_filepath = current_style.get_filepath() 
     
    81173        """ 
    82174        self.response.write(settings.ADD_DATA_TAG) 
    83  
    84         current_page = self.context["PAGE"] 
    85         stylesheet = current_page.style 
     175         
     176        current_style = get_style(self.request) 
    86177 
    87178        context = { 
    88             "content": stylesheet.content, 
     179            "content": current_style.content, 
    89180        } 
    90181        self._render_template("write_styles", context)#, debug=True) 
     
    114205 
    115206        return response 
     207 
     208 
     209    def switch(self, url_args=None): 
     210        """ 
     211        Switch the associated page template/stylesheet and save these info 
     212        into the session data. 
     213        """ 
     214        if url_args: 
     215            tolerant_delete(self.request.session, "style") 
     216            tolerant_delete(self.request.session, "template") 
     217            self.page_msg("Delete saved style/template.") 
     218                     
     219        if self.request.method == 'POST': 
     220            form = SwitchForm(self.request.POST) 
     221            if form.is_valid(): 
     222                selected_style = form.cleaned_data['style'] 
     223                selected_template = form.cleaned_data['template'] 
     224                self.request.session['style'] = selected_style.name 
     225                self.request.session['template'] = selected_template.name 
     226                self.page_msg("Style/Template, saved.") 
     227        else: 
     228            form = SwitchForm({ 
     229                "style": self.current_page.style.pk, 
     230                "template": self.current_page.template.pk, 
     231            }) 
     232         
     233        context = { 
     234            "current_page": self.current_page, 
     235            "reset_url": self.URLs.methodLink("switch", "reset"), 
     236            "form": form, 
     237        } 
     238        self._render_template("switch", context) 
     239 
     240 
    116241 
    117242from PyLucid.system.internal_page import get_internal_page, InternalPageNotFound 
  • trunk/pylucid_project/PyLucid/plugins_internal/page_style/page_style_cfg.py

    r1634 r1821  
    11# -*- coding: utf-8 -*- 
     2 
     3from django.utils.translation import gettext_lazy as _ 
    24 
    35#_____________________________________________________________________________ 
     
    2123    "print_current_style" : global_rights, 
    2224    "sendStyle" : global_rights, 
     25    "switch" : { 
     26        "must_login"    : True, 
     27        "must_admin"    : False, 
     28        "admin_sub_menu": { 
     29            "section"       : _("edit look"), 
     30            "title"         : _("Template/Style switcher"), 
     31            "help_text"     : _( 
     32                "Overwrite the associated template/stylesheet" 
     33                " (Good for testing new templates/styles)." 
     34            ), 
     35            "open_in_window": False, 
     36            "weight" : -5, 
     37        }, 
     38    }, 
    2339}