[Erp5-report] r20396 - in /experimental/Experimental: Tool/HTMLIncludeTool.py __init__.py

nobody at svn.erp5.org nobody at svn.erp5.org
Wed Apr 9 17:37:30 CEST 2008


Author: bartek
Date: Wed Apr  9 17:37:29 2008
New Revision: 20396

URL: http://svn.erp5.org?rev=20396&view=rev
Log:
I think it is much better like this - with two separate tools sharing the same API

Modified:
    experimental/Experimental/Tool/HTMLIncludeTool.py
    experimental/Experimental/__init__.py

Modified: experimental/Experimental/Tool/HTMLIncludeTool.py
URL: http://svn.erp5.org/experimental/Experimental/Tool/HTMLIncludeTool.py?rev=20396&r1=20395&r2=20396&view=diff
==============================================================================
--- experimental/Experimental/Tool/HTMLIncludeTool.py (original)
+++ experimental/Experimental/Tool/HTMLIncludeTool.py Wed Apr  9 17:37:29 2008
@@ -1,7 +1,7 @@
 ##############################################################################
 #
-# Copyright (c) 2007 Nexedi SARL and Contributors. All Rights Reserved.
-#                    Jean-Paul Smets <jp at nexedi.com>
+# Copyright (c) 2008 Nexedi SARL and Contributors. All Rights Reserved.
+#                    Bartek Gorny <bartek at erp5.pl>
 #
 # WARNING: This program as such is intended to be used by professional
 # programmers who take the whole responsability of assessing all potential
@@ -34,32 +34,29 @@
 from Products.Experimental import _dtmldir
 
 
-from DateTime import DateTime
-from Acquisition import aq_base
-
-
-_marker = []  # Create a new marker object.
-
 class HTMLIncludeTool(BaseTool):
   """
     Manage JS and CSS includes
+    This prototype manages names as strings
+    and it is probably enough.
+    Activating/deactivating includes and conditional inclusion won't be necessary (IMO).
+    It will support caching and merging includes to improve performance.
+    The business template engine will have to be modified to support definition of css and js
+    on the bt5 level, with some defaults in bootstrap.
   """
-  title = 'HTML Include Tool'
-  id = 'portal_html_includes'
-  meta_type = 'ERP5 HTML Include Tool'
-  portal_type = 'HTML Include Tool'
 
   _properties = (
-      { 'id'            : 'javascript',
-        'description'   : 'a list of JS to be included',
+      { 'id'            : 'file',
+        'description'   : 'a list of files to be included',
         'type'          : 'lines',
         'mode'          : 'w'
         },
-      { 'id'            : 'css',
-        'description'   : 'a list of css to be included',
-        'type'          : 'lines',
+      { 'id'            : 'compress',
+        'description'   : 'return everything as-is (for development), or compress&cache to improve performance (not implemented)',
+        'type'          : 'boolean',
         'mode'          : 'w'
         },
+
       )
 
   # Declarative Security
@@ -68,23 +65,55 @@
   security.declareProtected(Permissions.ManagePortal, 'manage_overview' )
   manage_overview = DTMLFile( 'explainHtmlIncludeTool', _dtmldir )
 
-  security.declareProtected(Permissions.ManagePortal, 'addJavascript')
-  def addJavascript(self, name):
+  security.declareProtected(Permissions.View, 'registerFile')
+  def registerFile(self, name, REQUEST=None):
     """
-      add javascript
+      add javascript or stylesheet at runtime
+      it will be appended to what is set in the tool itself
     """
-    js_list = self.getProperty('javascript')
-    if name not in js_list:
-      js_list.append(name)
+    if REQUEST is None:
+      raise Exception("include files are registered per request - required argument missing")
+    file_list = self.getPropertyList('file')
+    if name in file_list: # XXX room for optimization
+      return
+    temp_file_list = REQUEST.get(self.id, [])
+    if name not in temp_file_list:
+      temp_file_list.append(name)
+      REQUEST.set(self.id, temp_file_list)
 
-  security.declareProtected(Permissions.ManagePortal, 'addCSS')
-  def addCSS(self, name):
+  # XXX some aliases - we have to pick the best one
+  security.declareProtected(Permissions.View, 'register')
+  register = registerFile # a handy alias
+  security.declareProtected(Permissions.View, 'append')
+  append = registerFile # another alias - which is best?
+
+  security.declareProtected(Permissions.View, 'extend')
+  def extend(self, file_list, REQUEST=None):
     """
-      addd CSS
+      to register many files at the same time
     """
-    css_list = self.getProperty('css')
-    if name not in css_list:
-      css_list.append(name)
+    if REQUEST is None:
+      raise Exception("include files are registered per request - required argument missing")
+    for name in file_list:
+      self.registerFile(name, REQUEST)
+
+  security.declareProtected(Permissions.View, 'getRegisteredFileList')
+  def getRegisteredFileList(self, REQUEST=None):
+    """
+      Return a list of files, permanent and temporary
+    """
+    file_list = self.getPropertyList('file')
+    if REQUEST is not None:
+      temp_file_list = REQUEST.get(self.id, [])
+      return file_list + temp_file_list
+    return file_list
+
+  security.declareProtected(Permissions.View, 'prependFile')
+  def prepend(self, name, REQUEST=None):
+    """
+      Add JS/CSS file making sure it will be at the top of the list
+    """
+    pass # XXX do we need it?
 
   security.declareProtected(Permissions.ManagePortal, 'moveUp')
   def moveUp(self, name):
@@ -94,4 +123,69 @@
   def moveDown(self, name):
     pass
 
+  security.declareProtected(Permissions.View, 'registerCode')
+  def registerCode(self, code, REQUEST=None):
+    """
+      add javascript or stylesheet code at runtime
+      it will be appended to what is set in the tool itself
+    """
+    key = self.id + '_code'
+    if REQUEST is None:
+      raise Exception("included code is registered per request - required argument missing")
+    temp_code_list = REQUEST.get(key, [])
+    temp_code_list.append(code)
+    REQUEST.set(key, temp_code_list)
+
+  security.declareProtected(Permissions.View, 'getRegisteredCodeList')
+  def getRegisteredCodeList(self, REQUEST=None):
+    """
+      Return a list of codes
+    """
+    key = self.id + '_code'
+    return REQUEST.get(key, [])
+
+  security.declareProtected(Permissions.View, 'getIncludeCode')
+  def getIncludeCode(self, REQUEST=None):
+    """
+      Return a formatted list of entries (permanent and temporary)
+      for inclusion in html template
+    """
+    base_url = self.getPortalObject().absolute_url()
+    file_include_code = '\n'.join([self.tpl % (base_url+'/'+name) for name in self.getRegisteredFileList(REQUEST)])
+    # TODO merging and caching file includes here
+    # the file_include_code will be like <script src="portal_javascript/merged?key=aofiqfkadfkaj"/>
+    code_include_code = '\n'.join([self.tpl_code % code for code in self.getRegisteredCodeList(REQUEST)])
+    return file_include_code + '\n' + code_include_code
+
 InitializeClass(HTMLIncludeTool)
+
+
+class JavascriptRegistryTool(HTMLIncludeTool):
+  """
+    Manage Javascript
+  """
+
+  title = 'Javascript Registry Tool'
+  id = 'portal_javascript'
+  meta_type = 'ERP5 Javascript Registry Tool'
+  portal_type = 'Javascript Registry Tool'
+  tpl = '<script src="%s"/>'
+  tpl_code = '<script>%s</script>'
+
+InitializeClass(JavascriptRegistryTool)
+
+
+class CSSRegistryTool(HTMLIncludeTool):
+  """
+    Manage CSS
+  """
+
+  title = 'CSS Registry Tool'
+  id = 'portal_css'
+  meta_type = 'ERP5 CSS Registry Tool'
+  portal_type = 'CSS Registry Tool'
+  tpl = '<link type="text/css" rel="stylesheet" href="%s" />'
+  tpl_code = '<style>%s</style>'
+
+InitializeClass(CSSRegistryTool)
+

Modified: experimental/Experimental/__init__.py
URL: http://svn.erp5.org/experimental/Experimental/__init__.py?rev=20396&r1=20395&r2=20396&view=diff
==============================================================================
--- experimental/Experimental/__init__.py (original)
+++ experimental/Experimental/__init__.py Wed Apr  9 17:37:29 2008
@@ -42,7 +42,7 @@
 
 # Define object classes and tools
 object_classes = ()
-portal_tools = (HTMLIncludeTool.HTMLIncludeTool,)
+portal_tools = (HTMLIncludeTool.JavascriptRegistryTool, HTMLIncludeTool.CSSRegistryTool)
 content_classes = ()
 content_constructors = ()
 




More information about the Erp5-report mailing list