[ Index ]
 

Code source de Kupu-1.3.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/plone/ -> librarytool.py (source)

   1  ##############################################################################
   2  #
   3  # Copyright (c) 2003-2005 Kupu Contributors. All rights reserved.
   4  #
   5  # This software is distributed under the terms of the Kupu
   6  # License. See LICENSE.txt for license text. For a list of Kupu
   7  # Contributors see CREDITS.txt.
   8  #
   9  ##############################################################################
  10  """Kupu library tool
  11  
  12  This module contains Kupu's library tool to support drawers.
  13  
  14  $Id: librarytool.py 21720 2006-01-05 15:59:25Z paul $
  15  """
  16  import Acquisition
  17  from Acquisition import aq_parent, aq_inner, aq_base
  18  from Products.CMFCore.Expression import Expression
  19  from Products.CMFCore.Expression import createExprContext
  20  from Products.kupu.plone.interfaces import IKupuLibraryTool
  21  from Products.CMFCore.utils import getToolByName
  22  from Products.kupu.python.spellcheck import SpellChecker, format_result
  23  
  24  class KupuError(Exception): pass
  25  
  26  class KupuLibraryTool(Acquisition.Implicit):
  27      """A tool to aid Kupu libraries"""
  28  
  29      __implements__ = IKupuLibraryTool
  30  
  31      def __init__(self):
  32          self._libraries = []
  33          self._res_types = {}
  34  
  35      def _getExpressionContext(self, object):
  36          portal = aq_parent(aq_inner(self))
  37          if object is None or not hasattr(object, 'aq_base'):
  38              folder = portal
  39          else:
  40              folder = object
  41              # Search up the containment hierarchy until we find an
  42              # object that claims it's a folder.
  43              while folder is not None:
  44                  if getattr(aq_base(folder), 'isPrincipiaFolderish', 0):
  45                      # found it.
  46                      break
  47                  else:
  48                      folder = aq_parent(aq_inner(folder))
  49          ec = createExprContext(folder, portal, object)
  50          return ec
  51  
  52      def addLibrary(self, id, title, uri, src, icon):
  53          """See ILibraryManager"""
  54          lib = dict(id=id, title=title, uri=uri, src=src, icon=icon)
  55          for key, value in lib.items():
  56              if key=='id':
  57                  lib[key] = value
  58              else:
  59                  if not(value.startswith('string:') or value.startswith('python:')):
  60                      value = 'string:' + value
  61                  lib[key] = Expression(value)
  62          self._libraries.append(lib)
  63  
  64      def getLibraries(self, context):
  65          """See ILibraryManager"""
  66          expr_context = self._getExpressionContext(context)
  67          libraries = []
  68          for library in self._libraries:
  69              lib = {}
  70              for key in library.keys():
  71                  if isinstance(library[key], str):
  72                      lib[key] = library[key]
  73                  else:
  74                      # Automatic migration from old version.
  75                      if key=='id':
  76                          lib[key] = library[key] = library[key].text
  77                      else:
  78                          lib[key] = library[key](expr_context)
  79              libraries.append(lib)
  80          return tuple(libraries)
  81  
  82      def deleteLibraries(self, indices):
  83          """See ILibraryManager"""
  84          indices.sort()
  85          indices.reverse()
  86          for index in indices:
  87              del self._libraries[index]
  88  
  89      def updateLibraries(self, libraries):
  90          """See ILibraryManager"""
  91          for index, lib in enumerate(self._libraries):
  92              dic = libraries[index]
  93              for key in lib.keys():
  94                  if dic.has_key(key):
  95                      value = dic[key]
  96                      if key=='id':
  97                          lib[key] = value
  98                      else:
  99                          if not(value.startswith('string:') or
 100                                 value.startswith('python:')):
 101                              value = 'string:' + value
 102                          lib[key] = Expression(value)
 103              self._libraries[index] = lib
 104  
 105      def moveUp(self, indices):
 106          """See ILibraryManager"""
 107          indices.sort()
 108          libraries = self._libraries[:]
 109          for index in indices:
 110              new_index = index - 1
 111              libraries[index], libraries[new_index] = \
 112                                libraries[new_index], libraries[index]
 113          self._libraries = libraries
 114  
 115      def moveDown(self, indices):
 116          """See ILibraryManager"""
 117          indices.sort()
 118          indices.reverse()
 119          libraries = self._libraries[:]
 120          for index in indices:
 121              new_index = index + 1
 122              if new_index >= len(libraries):
 123                  new_index = 0
 124                  #new_index = ((index + 1) % len(libraries)) - 1
 125              libraries[index], libraries[new_index] = \
 126                                libraries[new_index], libraries[index]
 127          self._libraries = libraries
 128  
 129      def getPortalTypesForResourceType(self, resource_type):
 130          """See IResourceTypeMapper"""
 131          return self._res_types[resource_type][:]
 132  
 133      def queryPortalTypesForResourceType(self, resource_type, default=None):
 134          """See IResourceTypeMapper"""
 135          if not self._res_types.has_key(resource_type):
 136              return default
 137          return self._res_types[resource_type][:]
 138  
 139      def _validate_portal_types(self, resource_type, portal_types):
 140          typetool = getToolByName(self, 'portal_types')
 141          all_portal_types = dict([ (t.id, 1) for t in typetool.listTypeInfo()])
 142  
 143          portal_types = [ptype.strip() for ptype in portal_types if ptype]
 144          for p in portal_types:
 145              if p not in all_portal_types:
 146                  raise KupuError, "Resource type: %s, invalid type: %s" % (resource_type, p)
 147          return portal_types
 148  
 149      def addResourceType(self, resource_type, portal_types):
 150          """See IResourceTypeMapper"""
 151          portal_types = self._validate_portal_types(resource_type, portal_types)
 152          self._res_types[resource_type] = tuple(portal_types)
 153  
 154      def updateResourceTypes(self, type_info):
 155          """See IResourceTypeMapper"""
 156          type_map = self._res_types
 157          for type in type_info:
 158              resource_type = type['resource_type']
 159              portal_types = self._validate_portal_types(resource_type, type['portal_types'])
 160              del type_map[type['old_type']]
 161              type_map[resource_type] = tuple(portal_types)
 162  
 163      def deleteResourceTypes(self, resource_types):
 164          """See IResourceTypeMapper"""
 165          for type in resource_types:
 166              del self._res_types[type]
 167  
 168      def spellcheck(self, REQUEST, RESPONSE):
 169          """Spellchecker button support fucntion"""
 170          data = REQUEST.form.get('text')
 171          c = SpellChecker()
 172          result = c.check(data)
 173          if result == None:
 174              result = ''
 175          else:
 176              result = format_result(result)
 177  
 178          RESPONSE.setHeader('Content-Type', 'text/xml,charset=UTF-8')
 179          RESPONSE.setHeader('Content-Length', len(result))
 180          RESPONSE.write(result)


Généré le : Sun Feb 25 15:30:41 2007 par Balluche grâce à PHPXref 0.7