Changeset 2548

Show
Ignore:
Timestamp:
02/26/10 14:03:36 (5 months ago)
Author:
JensDiemer
Message:

Add debug mode to PyLucidRequestObjects? class to see if new request.PYLUCID attributes would be defined or existing changed.
See: http://www.pylucid.org/permalink/133/pylucid-objects#DEBUG

Location:
branches/0.9/pylucid_project
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • branches/0.9/pylucid_project/apps/pylucid/system/pylucid_objects.py

    r2330 r2548  
    1515    $Author: $ 
    1616 
    17     :copyleft: 2009 by the PyLucid team, see AUTHORS for more details. 
     17    :copyleft: 2009-2010 by the PyLucid team, see AUTHORS for more details. 
    1818    :license: GNU GPL v3 or above, see LICENSE for more details. 
    1919""" 
    2020 
    2121from django.conf import settings 
     22from django.utils.safestring import mark_safe 
    2223 
    23 from pylucid.shortcuts import failsafe_message 
    24 from pylucid.system import extrahead 
     24from pylucid_project.utils.escape import escape 
     25from pylucid_project.apps.pylucid.shortcuts import failsafe_message 
     26from pylucid_project.apps.pylucid.system import extrahead 
    2527 
     28 
     29# max value length in debug __setattr__ 
     30MAX_VALUE_LENGTH = 150 
    2631 
    2732 
    2833class PyLucidRequestObjects(object): 
    2934    """ PyLucid request objects """ 
     35    _check_setattr = False 
    3036    def __init__(self, request): 
    31         from pylucid.models import Language # FIXME: import here, against import loop. 
     37        self.request = request 
     38 
     39        # FIXME: import here, against import loop: 
     40        from pylucid_project.apps.pylucid.models import Language 
    3241 
    3342        # Client prefered language instance, use default, if not exist 
     
    5059        #self.page_template - The global page template as a string 
    5160        #self.context - The global context 
     61 
     62        self._check_setattr = settings.DEBUG 
     63 
     64    def _setattr_debug(self, name, value): 
     65        """ 
     66        debug __setattr__ to see if new attributes would be defined or existing changed. 
     67         
     68        HowTo: http://www.pylucid.org/permalink/133/pylucid-objects#DEBUG 
     69        """ 
     70        if self._check_setattr: 
     71            if hasattr(self, name): 
     72                action = "changed" 
     73            else: 
     74                action = "set" 
     75 
     76            value_preview = repr(value) 
     77            if len(value_preview) > MAX_VALUE_LENGTH - 3: 
     78                value_preview = value_preview[:MAX_VALUE_LENGTH] + "..." 
     79            value_preview = escape(value_preview) 
     80            msg = "request.PYLUCID.<strong>%s</strong> %s to: <i>%s</i> (type: %s)" % ( 
     81                name, action, value_preview, escape(repr(type(value))) 
     82            ) 
     83            self.request.page_msg(mark_safe(msg)) 
     84 
     85        super(PyLucidRequestObjects, self).__setattr__(name, value) 
     86 
     87 
     88if settings.PYLUCID_OBJECTS_DEBUG: 
     89    assert settings.DEBUG == True, "PyLucidRequestObjects works only if settings.DEBUG is on!" 
     90    PyLucidRequestObjects.__setattr__ = PyLucidRequestObjects._setattr_debug 
     91 
  • branches/0.9/pylucid_project/settings.py

    r2529 r2548  
    6262# Should allways be False. It's only for developing!  
    6363SQL_DEBUG = False 
     64 
     65# See if request.PYLUCID attributes attached or changes (Works only if DEBUG==True) 
     66# See also: http://www.pylucid.org/permalink/133/pylucid-objects#DEBUG 
     67# Should allways be False. It's only for developing!  
     68PYLUCID_OBJECTS_DEBUG = False 
    6469 
    6570#______________________________________________________________________________