Back to index

plone3  3.1.7
Install.py
Go to the documentation of this file.
00001 from Products.CMFCore.utils import getToolByName
00002 from Products.CMFCore.DirectoryView import addDirectoryViews
00003 from Products.ExternalMethod.ExternalMethod import ExternalMethod
00004 
00005 from Products.Archetypes.Extensions.utils import installTypes
00006 from Products.Archetypes import listTypes
00007 from Products.FAQ import PROJECTNAME,product_globals
00008 
00009 from zExceptions import NotFound
00010 
00011 from StringIO import StringIO
00012 import sys
00013 
00014 class PloneSkinRegistrar:
00015     """
00016     Controls (un)registering of a layer in the skins tool:
00017      - the layer in the content of the skin tool
00018      - the layer in the path of all skins
00019     @author: U{Gilles Lenfant <glenfant@bigfoot.com>}
00020     @version: 0.1.0
00021     @ivar _layer: Name of the Product's subdirectory that contains
00022         the various layers for the Product.
00023     @type _layer: string
00024     @ivar _prodglobals: Globals from this Product.
00025     @type _propglobals: mapping object
00026     """
00027 
00028     def __init__(self, skinsdir, prodglobals):
00029         """Constructor
00030         @param skinsdir: Name of the Product's subdirectory that
00031             contains the various layers for the Product.
00032         @type skinsdir: string
00033         @param prodglobals: Globals from this Product.
00034 
00035             should be provided by Product's C{__init__.py} like this:
00036 
00037             C{product_globals = globals()}
00038 
00039         @type propglobals: mapping object
00040         @return: None
00041         """
00042 
00043         self._skinsdir = skinsdir
00044         self._prodglobals = prodglobals
00045         return
00046 
00047     def install(self, aq_obj,position=None,mode='after',layerName=None):
00048         """Installs and registers the skin resources
00049         @param aq_obj: object from which cmf site object is acquired
00050         @type aq_obj: any Zope object in the CMF
00051         @return: Installation log
00052         @rtype: string
00053         """
00054 
00055         rpt = '=> Installing and registering layers from directory %s\n' % self._skinsdir
00056         skinstool = getToolByName(aq_obj, 'portal_skins')
00057 
00058         # Create the layer in portal_skins
00059 
00060         try:
00061             if self._skinsdir not in skinstool.objectIds():
00062                 addDirectoryViews(skinstool, self._skinsdir, self._prodglobals)
00063                 rpt += 'Added "%s" directory view to portal_skins\n' % self._skinsdir
00064             else:
00065                 rpt += 'Warning: directory view "%s" already added to portal_skins\n' % self._skinsdir
00066         except:
00067             # ugh, but i am in stress
00068             rpt += 'Warning: directory view "%s" already added to portal_skins\n' % self._skinsdir
00069 
00070 
00071         # Insert the layer in all skins
00072         # XXX FIXME: Actually assumes only one layer directory with the name of the Product
00073         # (should be replaced by a smarter solution that finds individual Product's layers)
00074 
00075         if not layerName:
00076             layerName = self._prodglobals['__name__'].split('.')[-1]
00077 
00078         skins = skinstool.getSkinSelections()
00079 
00080         for skin in skins:
00081             layers = skinstool.getSkinPath(skin)
00082             layers = [layer.strip() for layer in layers.split(',')]
00083             if layerName not in layers:
00084                 try:
00085                     pos=layers.index(position)
00086                     if mode=='after': pos=pos+1
00087                 except ValueError:
00088                     pos=len(layers)
00089 
00090                 layers.insert(pos, layerName)
00091 
00092                 layers = ','.join(layers)
00093                 skinstool.addSkinSelection(skin, layers)
00094                 rpt += 'Added "%s" to "%s" skin\n' % (layerName, skin)
00095             else:
00096                 rpt += '! Warning: skipping "%s" skin, "%s" is already set up\n' % (skin, type)
00097         return rpt
00098 
00099     def uninstall(self, aq_obj):
00100         """Uninstalls and unregisters the skin resources
00101         @param aq_obj: object from which cmf site object is acquired
00102         @type aq_obj: any Zope object in the CMF
00103         @return: Uninstallation log
00104         @rtype: string
00105         """
00106 
00107         rpt = '=> Uninstalling and unregistering %s layer\n' % self._skinsdir
00108         skinstool = getToolByName(aq_obj, 'portal_skins')
00109 
00110         # Removing layer from portal_skins
00111         # XXX FIXME: Actually assumes only one layer directory with the name of the Product
00112         # (should be replaced by a smarter solution that finds individual Product's layers)
00113 
00114         if not layerName:
00115             layerName = self._prodglobals['__name__'].split('.')[-1]
00116 
00117         if layerName in skinstool.objectIds():
00118             skinstool.manage_delObjects([layerName])
00119             rpt += 'Removed "%s" directory view from portal_skins\n' % layerName
00120         else:
00121             rpt += '! Warning: directory view "%s" already removed from portal_skins\n' % layerName
00122 
00123         # Removing from skins selection
00124 
00125         skins = skinstool.getSkinSelections()
00126         for skin in skins:
00127             layers = skinstool.getSkinPath(skin)
00128             layers = [layer.strip() for layer in layers.split(',')]
00129             if layerName in layers:
00130                 layers.remove(layerName)
00131                 layers = ','.join(layers)
00132                 skinstool.addSkinSelection(skin, layers)
00133                 rpt += 'Removed "%s" to "%s" skin\n' % (layerName, skin)
00134             else:
00135                 rpt += 'Skipping "%s" skin, "%s" is already removed\n' % (skin, layerName)
00136         return rpt
00137 # /class PloneSkinRegistrar
00138 
00139 def install(self):
00140     portal=getToolByName(self,'portal_url').getPortalObject()
00141     out = StringIO()
00142     classes=listTypes(PROJECTNAME)
00143     installTypes(self, out,
00144                  classes,
00145                  PROJECTNAME)
00146 
00147     print >> out, "Successfully installed %s." % PROJECTNAME
00148     sr = PloneSkinRegistrar('skins', product_globals)
00149     print >> out,sr.install(self)
00150     print >> out,sr.install(self,position='custom',mode='after',layerName=PROJECTNAME+'_public')
00151 
00152     return out.getvalue()
00153 
00154 def uninstall(self):
00155     out = StringIO()
00156     classes=listTypes(PROJECTNAME)
00157 
00158     #unregister folderish classes in use_folder_contents
00159     props=getToolByName(self,'portal_properties').site_properties
00160     use_folder_tabs=list(props.use_folder_tabs)
00161     print >> out, 'removing classes from use_folder_tabs:'
00162     for cl in classes:
00163         print >> out,  'type:', cl['klass'].portal_type
00164         if cl['klass'].isPrincipiaFolderish and not cl['klass'].portal_type in []:
00165             if cl['klass'].portal_type in use_folder_tabs:
00166                 use_folder_tabs.remove(cl['klass'].portal_type)
00167 
00168     props.use_folder_tabs=tuple(use_folder_tabs)
00169 
00170     return out.getvalue()