Back to index

plone3  3.1.7
topic.py
Go to the documentation of this file.
00001 from Globals import InitializeClass
00002 from Products.CMFCore.utils import getToolByName
00003 from Products.ATContentTypes.criteria import _criterionRegistry
00004 from AccessControl import ClassSecurityInfo
00005 from Persistence import Persistent
00006 from OFS.SimpleItem import SimpleItem
00007 from ExtensionClass import Base
00008 
00009 from Products.ATContentTypes.config import TOOLNAME
00010 from Products.ATContentTypes.interfaces import IATCTTopicsTool
00011 from Products.Archetypes.atapi import DisplayList
00012 from Products.CMFCore.permissions import ManagePortal
00013 from Products.CMFPlone.CatalogTool import CatalogTool
00014 
00015 class TopicIndex(SimpleItem, Persistent):
00016 
00017     def __init__(self, index, friendlyName='', description='', enabled=False, criteria=()):
00018         self.index = index
00019         self.friendlyName=friendlyName
00020         self.description = description
00021         self.enabled=enabled
00022         self.criteria=tuple(criteria)
00023 
00024 
00025 class ATTopicsTool(Base):
00026     """This tool makes it possible to manage the indexes that are used
00027        inside topics and allows you to enter a friendly name instead of
00028        cryptic indexes.
00029     """
00030     __implements__ = (IATCTTopicsTool,)
00031 
00032     security       = ClassSecurityInfo()
00033 
00034     # is used in ATPortalTypeCriterion to control which types are allowed to search with
00035     # until this is fixed in CMF or whatever, this is the way to go.
00036     allowed_portal_types = []
00037 
00038     id = TOOLNAME
00039     meta_type= 'ATTopics Tool'
00040     title = 'ATTopics Tool'
00041     plone_tool = True
00042 
00043     def __init__(self):
00044         self.topic_indexes = {}
00045         self.topic_metadata = {}
00046         self.allowed_portal_types = []
00047 
00048     def getCriteriaForIndex(self, index, as_dict=False):
00049         """ Returns the valid criteria for a given index """
00050         catalog_tool = getToolByName(self, CatalogTool.id)
00051         try:
00052             indexObj = catalog_tool.Indexes[index]
00053         except KeyError:
00054             return ()
00055         criteria = tuple(_criterionRegistry.criteriaByIndex(indexObj.meta_type))
00056         search_criteria = _criterionRegistry.listSearchTypes()
00057         if as_dict:
00058             criteria = [{'name': a, 'description': _criterionRegistry[a].shortDesc}
00059                                 for a in criteria if a in search_criteria]
00060         else:
00061             criteria = [a for a in criteria if a in search_criteria]
00062         criteria.sort()
00063         return criteria
00064 
00065     security.declareProtected(ManagePortal,'addIndex')
00066     def addIndex(self, index, friendlyName='', description='', enabled=False, criteria=None):
00067         """ Add a new index along with descriptive information to the index
00068             registry """
00069         if criteria is None:
00070             criteria = self.getCriteriaForIndex(index)
00071         if self.topic_indexes.has_key(index):
00072             objIndex = self.topic_indexes[index]
00073             objIndex.friendlyName=friendlyName
00074             objIndex.description=description
00075             objIndex.enabled=enabled
00076             objIndex.criteria=tuple(criteria)
00077         else:
00078             objIndex = TopicIndex(index, friendlyName, description, enabled, criteria)
00079 
00080         self.topic_indexes[index] = objIndex
00081         self._p_changed = True
00082 
00083     security.declareProtected(ManagePortal,'addMetadata')
00084     def addMetadata(self, metadata, friendlyName='', description='', enabled=False):
00085         """ Add a new metadata field along with descriptive information to the
00086             metadata registry """
00087         if self.topic_metadata.has_key(metadata):
00088             objMeta = self.topic_metadata[metadata]
00089             objMeta.friendlyName=friendlyName
00090             objMeta.description=description
00091             objMeta.enabled=enabled
00092         else:
00093             objMeta = TopicIndex(metadata, friendlyName, description, enabled)
00094 
00095         self.topic_metadata[metadata] = objMeta
00096         self._p_changed = True
00097 
00098     security.declareProtected(ManagePortal,'updateIndex')
00099     def updateIndex(self, index, friendlyName=None, description=None, enabled=None, criteria=None):
00100         """ Updates an existing index in the registry, unrecognized values are
00101             added """
00102         indexes = self.topic_indexes
00103         if friendlyName == None:
00104             friendlyName = indexes[index].friendlyName
00105         if description == None:
00106             description = indexes[index].description
00107         if enabled == None:
00108             enabled = indexes[index].enabled
00109         if criteria == None:
00110             criteria = indexes[index].criteria
00111 
00112         self.addIndex(index, friendlyName, description, enabled, criteria)
00113 
00114     security.declareProtected(ManagePortal,'updateMetadata')
00115     def updateMetadata(self, metadata, friendlyName=None, description=None, enabled=None):
00116         """ Updates an existing metadata field in the registry, unrecognized values are
00117             added """
00118         meta = self.topic_metadata
00119         if friendlyName == None:
00120             friendlyName = meta[metadata].friendlyName
00121         if description == None:
00122             description = meta[metadata].description
00123         if enabled == None:
00124             enabled = meta[metadata].enabled
00125         self.addMetadata(metadata, friendlyName, description, enabled)
00126 
00127     security.declareProtected(ManagePortal,'removeIndex')
00128     def removeIndex(self, index):
00129         """ Removes an existing index from the registry """
00130         if self.topic_indexes.has_key(index):
00131             del self.topic_indexes[index]
00132             self._p_changed = True
00133 
00134     security.declareProtected(ManagePortal,'removeMetadata')
00135     def removeMetadata(self, metadata):
00136         """ Removes an existing metadata field from the registry """
00137         if self.topic_metadata.has_key(metadata):
00138             del self.topic_metadata[metadata]
00139             self._p_changed = True
00140 
00141     security.declareProtected(ManagePortal,'createInitialIndexes')
00142     def createInitialIndexes(self):
00143         """ create indexes for all indexes in the catalog """
00144         indexes = self.listCatalogFields()
00145         for i in indexes:
00146             if not self.topic_indexes.has_key(i):
00147                 enabled = False
00148                 self.addIndex(i, friendlyName='', enabled=enabled)
00149         return True
00150 
00151     security.declareProtected(ManagePortal,'createInitialMetadata')
00152     def createInitialMetadata(self):
00153         """ create metadata for all indexes in the catalog """
00154         metas = self.listCatalogMetadata()
00155         for i in metas:
00156             if not self.topic_metadata.has_key(i):
00157                 enabled = False
00158                 self.addMetadata(i, friendlyName='', enabled=enabled)
00159         return True
00160 
00161     security.declarePrivate('listCatalogFields')
00162     def listCatalogFields(self):
00163         """ Return a list of fields from portal_catalog. """
00164         pcatalog = getToolByName( self,  CatalogTool.id )
00165         available = pcatalog.indexes()
00166         val = [ field for field in available ]
00167         val.sort()
00168         return val
00169 
00170     security.declarePrivate('listCatalogMetadata')
00171     def listCatalogMetadata(self):
00172         """ Return a list of columns from portal_catalog. """
00173         pcatalog = getToolByName( self,  CatalogTool.id )
00174         available = pcatalog.schema()
00175         val = [ field for field in available ]
00176         val.sort()
00177         return val
00178 
00179     def getAllPortalTypes(self):
00180         """ returns a list of (id, title)-tuples for each type """
00181         types_tool = getToolByName(self, 'portal_types')
00182         types = types_tool.listTypeInfo()
00183 
00184         all_types=[(t.id, t.title or t.id) for t in types]
00185         return all_types
00186 
00187     def getAllowedPortalTypes(self, populate_for_end_usage=1):
00188         """ Return all portal_types as an (id,title) tuple that are allowed
00189             to search with """
00190         all_types = self.getAllPortalTypes()
00191 
00192         if populate_for_end_usage==0:
00193             # return whatever is in self.allowed_portal_types and make it a (id, title) tuple
00194             return [t for t in all_types if t[0] in self.allowed_portal_types]
00195 
00196         if self.allowed_portal_types==[]:
00197             filtered_types = all_types
00198         else:
00199             filtered_types = [type for type in all_types if type[0] in self.allowed_portal_types]
00200 
00201         return filtered_types
00202 
00203     def getEnabledIndexes(self):
00204         """ Returns all TopicIndex objects for enabled indexes """
00205         indexes = self.topic_indexes
00206         results = [i for i in indexes.values() if i.enabled]
00207         return results
00208 
00209     def getEnabledMetadata(self):
00210         """ Returns all TopicIndex objects for enabled metadata """
00211         meta = self.topic_metadata
00212         results = [i for i in meta.values() if i.enabled]
00213         return results
00214 
00215     def getIndexDisplay(self, enabled=True):
00216         """ Return DisplayList of Indexes and their friendly names """
00217         if enabled:
00218             index_names = self.getIndexes(True)
00219         else:
00220             index_names = self.getIndexes()
00221         index_dict = self.topic_indexes
00222         indexes = [index_dict[i] for i in index_names]
00223         field_list=[(f.index, f.friendlyName or f.index) for f in indexes]
00224         return DisplayList(field_list)
00225 
00226     def getMetadataDisplay(self, enabled=True):
00227         """ Return DisplayList of Metadata and their friendly names """
00228         if enabled:
00229             meta_names = self.getAllMetadata(True)
00230         else:
00231             meta_names = self.getAllMetadata()
00232         meta_dict = self.topic_metadata
00233         meta = [meta_dict[i] for i in meta_names]
00234         field_list=[(f.index, f.friendlyName or f.index) for f in meta]
00235         return DisplayList(field_list)
00236 
00237     def getEnabledFields(self):
00238         """ Returns a list of tuples containing the index name, friendly name,
00239             and description for each enabled index. """
00240         enabledIndexes = self.getEnabledIndexes()
00241         dec_fields = [(i.friendlyName.lower() or \
00242                        i.index.lower(), i.index, i.friendlyName or \
00243                        i.index, i.description) for i in enabledIndexes]
00244         dec_fields.sort()
00245         fields = [(a[1],a[2],a[3]) for a in dec_fields]
00246         return fields
00247 
00248     def getFriendlyName(self, index):
00249         """ Returns the friendly name for a given index name, or the given
00250             index if the firendlyname is empty or the index is not recognized
00251         """
00252         if self.topic_indexes.has_key(index):
00253             return self.getIndex(index).friendlyName or index
00254         else:
00255             return index
00256 
00257     security.declareProtected(ManagePortal,'getIndexes')
00258     def getIndexes(self, enabledOnly=False):
00259         """ Returns the full list of available indexes, optionally filtering
00260             out those that are not marked enabled """
00261         if enabledOnly:
00262             indexes_dec = [(i.index.lower(), i.index) for i in self.getEnabledIndexes()]
00263         else:
00264             self.createInitialIndexes() # update in case of new catalogue indexes
00265             indexes_dec = [(i.lower(), i) for i in self.topic_indexes.keys()]
00266 
00267         indexes_dec.sort()
00268         indexes = [i[1] for i in indexes_dec]
00269         return indexes
00270 
00271     security.declareProtected(ManagePortal,'getAllMetadata')
00272     def getAllMetadata(self, enabledOnly=False):
00273         """ Returns the full list of available metadata fields, optionally
00274             filtering out those that are not marked enabled """
00275         if enabledOnly:
00276             meta_dec = [(i.index.lower(), i.index) for i in self.getEnabledMetadata()]
00277         else:
00278             self.createInitialMetadata() # update in case of new catalogue metadata
00279             meta_dec = [(i.lower(), i) for i in self.topic_metadata.keys()]
00280 
00281         meta_dec.sort()
00282         metadata = [i[1] for i in meta_dec]
00283         return metadata
00284 
00285     def getIndex(self, index):
00286         """ Returns the TopicIndex object for a given index name """
00287         if self.topic_indexes.has_key(index):
00288             return self.topic_indexes[index]
00289         else:
00290             raise AttributeError('Index ' + str(index) + ' not found')
00291 
00292     def getMetadata(self, metadata):
00293         """ Returns the TopicIndex object for a given metadata name """
00294         if self.topic_metadata.has_key(metadata):
00295             return self.topic_metadata[metadata]
00296         else:
00297             raise AttributeError('Metadata ' + str(metadata) + ' not found')
00298 
00299     security.declareProtected(ManagePortal,'manage_saveTopicSetup')
00300     def manage_saveTopicSetup(self, REQUEST=None):
00301         """ Set indexes and metadata from form """
00302         if REQUEST==None:
00303             return  'Nothing saved.'
00304 
00305         data = REQUEST.get('index', [])
00306         for index in data:
00307             enabled = index.has_key('enabled')
00308             criteria = index.get('criteria', ())
00309             self.updateIndex(index['index'], index['friendlyName'], index['description'], enabled, criteria)
00310 
00311         meta = REQUEST.get('metadata', [])
00312         for metadata in meta:
00313             enabled = metadata.has_key('enabled')
00314             self.updateMetadata(metadata['index'], metadata['friendlyName'], metadata['description'], enabled)
00315         return True
00316 
00317     security.declareProtected(ManagePortal,'manage_saveTopicSetupTypes')
00318     def manage_saveTopicSetupTypes(self, REQUEST=None):
00319         """ Set portal types from form """
00320         if REQUEST==None:
00321             return  'Nothing saved.'
00322 
00323         self.allowed_portal_types = REQUEST.get('allowed_types', [])
00324         return True
00325 
00326 InitializeClass(ATTopicsTool)
00327