Back to index

plone3  3.1.7
SQLStorageConfig.py
Go to the documentation of this file.
00001 """ SQL Storage Configuration for Archetypes.
00002 """
00003 from Products.Archetypes.config import TOOL_NAME
00004 from Products.Archetypes.interfaces.storage import ISQLStorage
00005 from Products.Archetypes.interfaces.field import IObjectField
00006 
00007 from AccessControl import ClassSecurityInfo
00008 from Acquisition import aq_base
00009 from Globals import InitializeClass, PersistentMapping, DTMLFile
00010 from OFS.SimpleItem import SimpleItem
00011 from Products.CMFCore.permissions import ManagePortal
00012 from Products.CMFCore.utils import getToolByName
00013 
00014 class SQLStorageConfig (SimpleItem):
00015 
00016     """ Map Archetypes to SQL Database Connections.
00017     """
00018 
00019     meta_type = 'SQL Storage Config'
00020 
00021     _conn_by_type = None  # PersistentMapping
00022     _default_conn = None
00023 
00024     security = ClassSecurityInfo()
00025 
00026     manage_options = (({ 'label' : 'Connections'
00027                          , 'action' : 'manage_selectConnections'
00028                          }
00029                        ),
00030                       )
00031 
00032     #
00033     #   ZMI methods
00034     #
00035 
00036 
00037     _manage_selectConnections = DTMLFile('www/selectConnections', globals())
00038 
00039     security.declareProtected( ManagePortal, 'manage_selectConnections')
00040     def manage_selectConnections(self, REQUEST, manage_tabs_message=None):
00041 
00042         """ Show a management screen for changing type to workflow connections.
00043         """
00044         cbt = self._conn_by_type
00045         ti = self.getConfigurableTypes()
00046         types_info = []
00047         for t in ti:
00048             id = t['name']
00049             title = None
00050             if cbt is not None and cbt.has_key(id):
00051                 conn = cbt[id]
00052             else:
00053                 conn = '(Default)'
00054             types_info.append({'id': id,
00055                                'title': title,
00056                                'conn': conn})
00057         return self._manage_selectConnections(
00058             REQUEST,
00059             default_conn=self._default_conn,
00060             types_info=types_info,
00061             management_view='Connections',
00062             manage_tabs_message=manage_tabs_message)
00063 
00064     security.declareProtected( ManagePortal, 'manage_changeConnections')
00065     def manage_changeConnections(self, default_conn, props=None, REQUEST=None):
00066         """ Changes which connectionss apply to objects of which type.
00067         """
00068         if props is None:
00069             props = REQUEST
00070         cbt = self._conn_by_type
00071         if cbt is None:
00072             self._conn_by_type = cbt = PersistentMapping()
00073         ti = self.getConfigurableTypes()
00074         types_info = []
00075         for t in ti:
00076             id = t['name']
00077             field_name = 'conn_%s' % id
00078             conn = props.get(field_name, '(Default)').strip()
00079             self.setConnForPortalTypes((id, ), conn)
00080 
00081         # Set up the default conn.
00082         self.setDefaultConn(default_conn)
00083         if REQUEST is not None:
00084             return self.manage_selectConnections(REQUEST,
00085                             manage_tabs_message='Changed.')
00086 
00087     #
00088     #   Administration methods
00089     #
00090     security.declareProtected( ManagePortal, 'setDefaultConn')
00091     def setDefaultConn(self, default_conn):
00092         """ Set the default conn for this tool
00093         """
00094         default_conn = default_conn.strip()
00095         if default_conn:
00096             if not self.getConnectionById(default_conn):
00097                 raise ValueError, (
00098                     '"%s" is not a valid SQL Connector.' % default_conn)
00099         self._default_conn = default_conn
00100 
00101     security.declarePrivate('getDefaultConnFor')
00102     def getDefaultConnFor(self, ob):
00103         """ Return the default conn, if applicable, for ob.
00104         """
00105     
00106         types_tool = getToolByName( self, 'portal_types', None )
00107         if ( types_tool is not None
00108             and types_tool.getTypeInfo( ob ) is not None ):
00109             return self._default_conn
00110         return None
00111 
00112     security.declareProtected( ManagePortal, 'getConfigurableTypes')
00113     def getConfigurableTypes(self):
00114         """ Get a list of types that can be configured for SQL Storage.
00115         """
00116         c_types = []
00117         ti = self.getInstalledTypes()
00118         for t in ti:
00119             for field in t['type'].fields():
00120                 if IObjectField.isImplementedBy(field) and \
00121                    ISQLStorage.isImplementedBy(field.getStorage()):
00122                     c_types.append(t)
00123                     break
00124         return c_types
00125 
00126 
00127     security.declareProtected( ManagePortal, 'getInstalledTypes')
00128     def getInstalledTypes(self):
00129         pt = getToolByName(self, 'portal_types', None)
00130         at = getToolByName(self, TOOL_NAME, None)
00131         if pt is None:
00132             return ()
00133         if at is None:
00134             return ()
00135         pt = pt.listTypeInfo()
00136         pt = [t.getId() for t in pt]
00137 
00138         ti = at.listRegisteredTypes()
00139 
00140         installed_types = [t for t in ti if t['name'] in pt]
00141         return installed_types
00142 
00143 
00144     security.declareProtected( ManagePortal, 'setConnForPortalTypes')
00145     def setConnForPortalTypes(self, type_names, conn):
00146         """ Set a conn for a specific portal type.
00147         """
00148         cbt = self._conn_by_type
00149 
00150         if cbt is None:
00151             self._conn_by_type = cbt = PersistentMapping()
00152 
00153         for id in type_names:
00154             if conn == '(Default)':
00155                 # Remove from cbt.
00156                 if cbt.has_key(id):
00157                     del cbt[id]
00158             else:
00159                 conn = conn.strip()
00160                 if conn:
00161                     if not self.getConnectionById(conn):
00162                         raise ValueError, (
00163                             '"%s" is not a valid SQL Connector.' % conn)
00164                 cbt[id] = conn
00165 
00166     security.declarePrivate('getConnectionById')
00167     def getConnectionById(self, conn_id):
00168         """ Retrieve a given Connection.
00169         """
00170         conn = getattr(self, conn_id, None)
00171         return conn
00172 
00173     security.declarePrivate('getConnFor')
00174     def getConnFor(self, ob):
00175         """ Returns the conn that applies to the given object.
00176             If we get a string as the ob parameter, use it as
00177             the portal_type.
00178         """
00179         cbt = self._conn_by_type
00180         if type(ob) == type(''):
00181             pt = ob
00182         elif hasattr(aq_base(ob), '_getPortalTypeName'):
00183             pt = ob._getPortalTypeName()
00184         else:
00185             pt = None
00186 
00187         if pt is None:
00188             return None
00189 
00190         conn = None
00191         if cbt is not None:
00192             conn = cbt.get(pt, None)
00193             # Note that if conn is not in cbt or has a value of
00194             # None, we use a default conn.
00195         if conn is None:
00196             conn = self.getDefaultConnFor(ob)
00197             if conn is None:
00198                 return ''
00199         return conn
00200 
00201 InitializeClass(SQLStorageConfig)