Back to index

plone3  3.1.7
develview.py
Go to the documentation of this file.
00001 
00002 try:
00003     from Products.Five import BrowserView
00004     BrowserView
00005 except ImportError:
00006     from zope.publisher.browser import BrowserView
00007 
00008 from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
00009 from interfaces import IDevelView
00010 from zope.interface import implements
00011 from zope.traversing.interfaces import ITraverser
00012 from zope.publisher.interfaces import NotFound
00013 
00014 COOKIE_DEVELMODE = '__kss_devel'
00015 COOKIE_LOGLEVEL = '__kss_loglevel'
00016 
00017 class DevelView(BrowserView):
00018     implements(IDevelView)
00019 
00020     # Zope3 requires the implementation of
00021     # IBrowserPublisher, in order for the methods
00022     # to be traversable.
00023     #
00024     # An alternative would be:
00025     # <browser:pages class="...">
00026     #   <page name="..." attribute="..." />
00027     #   <page name="..." attribute="..." />
00028     # </browser:pages>
00029 
00030     def publishTraverse(self, request, name):
00031         try:
00032             return getattr(self, name)
00033         except AttributeError:
00034             raise NotFound(self.context, name, request)
00035 
00036     def browserDefault(self, request):
00037         # make ui the default method
00038         return self, ('ui', )
00039 
00040     # --
00041     # Methods for handling development/production mode
00042     # --
00043 
00044     def ison(self):
00045         '''Checks if running in development mode
00046 
00047         Two ways to induce development mode:
00048 
00049         - set the cookie on the request
00050 
00051         - switch portal_js tool into debug mode, this will
00052           select development mode without the cookie
00053 
00054         '''
00055         ison = COOKIE_DEVELMODE in self.request.cookies
00056 
00057         if not ison:
00058             # Check from javascript tool
00059             # XXX this should not be done from here, but I don't want to
00060             # modify other components yet.
00061             try:
00062                 from Products.CMFCore.utils import getToolByName
00063                 js_tool = getToolByName(self.context.aq_inner, 'portal_javascripts')
00064                 ison = js_tool.getDebugMode()
00065             except:
00066                 pass
00067 
00068         result = bool(ison)
00069         return result
00070 
00071     def isoff(self, REQUEST=None):
00072         'Check if running in production mode'
00073         result = not(self.ison())
00074         if REQUEST is not None:
00075             result = str(result)
00076         return result
00077 
00078     def set(self):
00079         'Sets development mode cookie'
00080         self.request.response.setCookie(COOKIE_DEVELMODE, '1', path='/')
00081 
00082     def unset(self):
00083         'Unsets development mode cookie'
00084         self.request.response.expireCookie(COOKIE_DEVELMODE, path='/')
00085 
00086     # --
00087     # Methods for detecting some browser versions
00088     # --
00089 
00090     def needs_old_cssquery(self):
00091         user_agent = self.request.get_header('User-Agent', '')
00092         safari_3_1 = 'Version/3.1' in user_agent and 'Safari' in user_agent
00093         # on Safari 3_1 the current base2 is broken, this check makes
00094         # it possible to serve the old cssquery instead of base2
00095         # on this version.
00096         # """User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_2; en-us) AppleWebKit/525.13 (KHTML, like Gecko) Version/3.1 Safari/525.13"""
00097         return safari_3_1
00098 
00099     # --
00100     # Methods for handling loglevel
00101     # --
00102 
00103     def getLogLevel(self, REQUEST=None):
00104         'Gets current log level'
00105         loglevel = self.request.cookies.get(COOKIE_LOGLEVEL, 'DEBUG').upper()
00106         return loglevel
00107 
00108     def setLogLevel(self, loglevel):
00109         'Sets loglevel cookie'
00110         self.request.response.setCookie(COOKIE_LOGLEVEL, loglevel, path='/')
00111 
00112     # --
00113     # User interface
00114     # --
00115 
00116     _ui = ViewPageTemplateFile('develui.pt', content_type='text/html;charset=utf-8')
00117 
00118     def ui(self):
00119         'User interface for interactive switching'
00120         options = {}
00121         # Handle development/production mode
00122         if 'devel' in self.request.form:
00123             self.set()
00124             # setting it also to have immediate effect in the page
00125             options['devel_mode'] = True
00126         if 'prod' in self.request.form:
00127             self.unset()
00128             if COOKIE_DEVELMODE in self.request.cookies:
00129                 # setting it also to have immediate effect in the page
00130                 options['devel_mode'] = False
00131         # Handle loglevel
00132         if 'loglevel' in self.request.form:
00133             loglevel =  self.request.form['loglevel']
00134             self.setLogLevel(loglevel)
00135             # setting it also to have immediate effect in the page
00136             options['loglevel'] = loglevel
00137         # Return the rendered template
00138         return self._ui(**options)
00139 
00140     def ui_js(self):
00141         'Javascript needed for the ui'
00142         resource = ITraverser(self.context).traverse('++resource++kss_devel_ui.js', 
00143                 request=self.request)
00144         cooked = resource.GET()
00145         return cooked
00146 
00147     def ui_css(self):
00148         'CSS needed for the ui'
00149         resource = ITraverser(self.context).traverse('++resource++kss_devel_ui.css', 
00150                 request=self.request)
00151         cooked = resource.GET()
00152         return cooked
00153 
00154     def ui_kss(self):
00155         'KSS needed for the ui'
00156         resource = ITraverser(self.context).traverse('++resource++kss_devel_ui.kss', 
00157                 request=self.request)
00158         cooked = resource.GET()
00159         return cooked