/CMFEditions/tests/FAQ/Extensions/Install.py

https://github.com/dtgit/dtedu
Python | 170 lines | 118 code | 25 blank | 27 comment | 21 complexity | 4312828eb45595738c402db91083baac MD5 | raw file
  1. from Products.CMFCore.utils import getToolByName
  2. from Products.CMFCore.DirectoryView import addDirectoryViews
  3. from Products.ExternalMethod.ExternalMethod import ExternalMethod
  4. from Products.Archetypes.Extensions.utils import installTypes
  5. from Products.Archetypes import listTypes
  6. from Products.FAQ import PROJECTNAME,product_globals
  7. from zExceptions import NotFound
  8. from StringIO import StringIO
  9. import sys
  10. class PloneSkinRegistrar:
  11. """
  12. Controls (un)registering of a layer in the skins tool:
  13. - the layer in the content of the skin tool
  14. - the layer in the path of all skins
  15. @author: U{Gilles Lenfant <glenfant@bigfoot.com>}
  16. @version: 0.1.0
  17. @ivar _layer: Name of the Product's subdirectory that contains
  18. the various layers for the Product.
  19. @type _layer: string
  20. @ivar _prodglobals: Globals from this Product.
  21. @type _propglobals: mapping object
  22. """
  23. def __init__(self, skinsdir, prodglobals):
  24. """Constructor
  25. @param skinsdir: Name of the Product's subdirectory that
  26. contains the various layers for the Product.
  27. @type skinsdir: string
  28. @param prodglobals: Globals from this Product.
  29. should be provided by Product's C{__init__.py} like this:
  30. C{product_globals = globals()}
  31. @type propglobals: mapping object
  32. @return: None
  33. """
  34. self._skinsdir = skinsdir
  35. self._prodglobals = prodglobals
  36. return
  37. def install(self, aq_obj,position=None,mode='after',layerName=None):
  38. """Installs and registers the skin resources
  39. @param aq_obj: object from which cmf site object is acquired
  40. @type aq_obj: any Zope object in the CMF
  41. @return: Installation log
  42. @rtype: string
  43. """
  44. rpt = '=> Installing and registering layers from directory %s\n' % self._skinsdir
  45. skinstool = getToolByName(aq_obj, 'portal_skins')
  46. # Create the layer in portal_skins
  47. try:
  48. if self._skinsdir not in skinstool.objectIds():
  49. addDirectoryViews(skinstool, self._skinsdir, self._prodglobals)
  50. rpt += 'Added "%s" directory view to portal_skins\n' % self._skinsdir
  51. else:
  52. rpt += 'Warning: directory view "%s" already added to portal_skins\n' % self._skinsdir
  53. except:
  54. # ugh, but i am in stress
  55. rpt += 'Warning: directory view "%s" already added to portal_skins\n' % self._skinsdir
  56. # Insert the layer in all skins
  57. # XXX FIXME: Actually assumes only one layer directory with the name of the Product
  58. # (should be replaced by a smarter solution that finds individual Product's layers)
  59. if not layerName:
  60. layerName = self._prodglobals['__name__'].split('.')[-1]
  61. skins = skinstool.getSkinSelections()
  62. for skin in skins:
  63. layers = skinstool.getSkinPath(skin)
  64. layers = [layer.strip() for layer in layers.split(',')]
  65. if layerName not in layers:
  66. try:
  67. pos=layers.index(position)
  68. if mode=='after': pos=pos+1
  69. except ValueError:
  70. pos=len(layers)
  71. layers.insert(pos, layerName)
  72. layers = ','.join(layers)
  73. skinstool.addSkinSelection(skin, layers)
  74. rpt += 'Added "%s" to "%s" skin\n' % (layerName, skin)
  75. else:
  76. rpt += '! Warning: skipping "%s" skin, "%s" is already set up\n' % (skin, type)
  77. return rpt
  78. def uninstall(self, aq_obj):
  79. """Uninstalls and unregisters the skin resources
  80. @param aq_obj: object from which cmf site object is acquired
  81. @type aq_obj: any Zope object in the CMF
  82. @return: Uninstallation log
  83. @rtype: string
  84. """
  85. rpt = '=> Uninstalling and unregistering %s layer\n' % self._skinsdir
  86. skinstool = getToolByName(aq_obj, 'portal_skins')
  87. # Removing layer from portal_skins
  88. # XXX FIXME: Actually assumes only one layer directory with the name of the Product
  89. # (should be replaced by a smarter solution that finds individual Product's layers)
  90. if not layerName:
  91. layerName = self._prodglobals['__name__'].split('.')[-1]
  92. if layerName in skinstool.objectIds():
  93. skinstool.manage_delObjects([layerName])
  94. rpt += 'Removed "%s" directory view from portal_skins\n' % layerName
  95. else:
  96. rpt += '! Warning: directory view "%s" already removed from portal_skins\n' % layerName
  97. # Removing from skins selection
  98. skins = skinstool.getSkinSelections()
  99. for skin in skins:
  100. layers = skinstool.getSkinPath(skin)
  101. layers = [layer.strip() for layer in layers.split(',')]
  102. if layerName in layers:
  103. layers.remove(layerName)
  104. layers = ','.join(layers)
  105. skinstool.addSkinSelection(skin, layers)
  106. rpt += 'Removed "%s" to "%s" skin\n' % (layerName, skin)
  107. else:
  108. rpt += 'Skipping "%s" skin, "%s" is already removed\n' % (skin, layerName)
  109. return rpt
  110. # /class PloneSkinRegistrar
  111. def install(self):
  112. portal=getToolByName(self,'portal_url').getPortalObject()
  113. out = StringIO()
  114. classes=listTypes(PROJECTNAME)
  115. installTypes(self, out,
  116. classes,
  117. PROJECTNAME)
  118. print >> out, "Successfully installed %s." % PROJECTNAME
  119. sr = PloneSkinRegistrar('skins', product_globals)
  120. print >> out,sr.install(self)
  121. print >> out,sr.install(self,position='custom',mode='after',layerName=PROJECTNAME+'_public')
  122. return out.getvalue()
  123. def uninstall(self):
  124. out = StringIO()
  125. classes=listTypes(PROJECTNAME)
  126. #unregister folderish classes in use_folder_contents
  127. props=getToolByName(self,'portal_properties').site_properties
  128. use_folder_tabs=list(props.use_folder_tabs)
  129. print >> out, 'removing classes from use_folder_tabs:'
  130. for cl in classes:
  131. print >> out, 'type:', cl['klass'].portal_type
  132. if cl['klass'].isPrincipiaFolderish and not cl['klass'].portal_type in []:
  133. if cl['klass'].portal_type in use_folder_tabs:
  134. use_folder_tabs.remove(cl['klass'].portal_type)
  135. props.use_folder_tabs=tuple(use_folder_tabs)
  136. return out.getvalue()