Back to index

plone3  3.1.7
__init__.py
Go to the documentation of this file.
00001 ##############################################################################
00002 #
00003 # ATContentTypes http://plone.org/products/atcontenttypes/
00004 # Archetypes reimplementation of the CMF core types
00005 #
00006 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
00007 # Copyright (c) 2003-2006 AT Content Types development team
00008 #
00009 # This software is subject to the provisions of the Zope Public License,
00010 # Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
00011 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
00012 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00013 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
00014 # FOR A PARTICULAR PURPOSE
00015 #
00016 ##############################################################################
00017 """ Topic:
00018 
00019 
00020 """
00021 
00022 __author__  = 'Christian Heimes <tiran@cheimes.de>'
00023 __docformat__ = 'restructuredtext'
00024 __old_name__ = 'Products.ATContentTypes.types.criteria'
00025 
00026 from UserDict import UserDict
00027 from Products.Archetypes.atapi import registerType
00028 from Products.Archetypes.ClassGen import generateClass
00029 from Products.ATContentTypes.config import PROJECTNAME
00030 from types import StringType
00031 
00032 from Products.ATContentTypes.interfaces import IATTopicCriterion
00033 from Products.ATContentTypes.interfaces import IATTopicSearchCriterion
00034 from Products.ATContentTypes.interfaces import IATTopicSortCriterion
00035 
00036 ALL_INDICES = ('DateIndex', 'DateRangeIndex', 'FieldIndex', 'KeywordIndex',
00037                'PathIndex', 'TextIndex', 'TextIndexNG2', 'TextIndexNG3',
00038                'TopicIndex', 'ZCTextIndex', 'NavtreeIndexNG', 
00039                'ExtendedPathIndex')
00040 
00041 SORT_INDICES = ('DateIndex', 'DateRangeIndex', 'FieldIndex', 'KeywordIndex')
00042 # TextIndex, PathIndex, TopicIndex, ZCTextIndex, TextIndexNG2, NavtreeIndexNG
00043 # are not usable to sort
00044 # as they do not have 'keyForDocument' attribute
00045 
00046 DATE_INDICES = ('DateIndex', 'DateRangeIndex', 'FieldIndex')
00047 
00048 # Indices that can take a list of values
00049 LIST_INDICES = ('FieldIndex', 'KeywordIndex', 'PathIndex', 'NavtreeIndexNG',
00050                   'ExtendedPathIndex', 'TopicIndex')
00051 
00052 TEXT_INDICES = ('TextIndex', 'TextIndexNG2', 'TextIndexNG3', 'ZCTextIndex')
00053 
00054 # Indices that can take a simple string
00055 STRING_INDICES = LIST_INDICES + TEXT_INDICES
00056 
00057 # Indices that may hold AT reference data
00058 REFERENCE_INDICES = ('FieldIndex', 'KeywordIndex')
00059 FIELD_INDICES = ('FieldIndex',)
00060 PATH_INDICES = ('PathIndex','ExtendedPathIndex')
00061 
00062 class _CriterionRegistry(UserDict):
00063     """Registry for criteria """
00064 
00065     def __init__(self, *args, **kwargs):
00066         UserDict.__init__(self, *args, **kwargs)
00067         self.index2criterion = {}
00068         self.criterion2index = {}
00069         self.portaltypes = {}
00070 
00071     def register(self, criterion, indices):
00072         if type(indices) is StringType:
00073             indices = (indices,)
00074         indices = tuple(indices)
00075 
00076         if indices == ():
00077             indices = ALL_INDICES
00078 
00079         assert IATTopicCriterion.isImplementedByInstancesOf(criterion)
00080         registerType(criterion, PROJECTNAME)
00081 
00082         crit_id = criterion.meta_type
00083         self[crit_id] = criterion
00084         self.portaltypes[criterion.portal_type] = criterion
00085 
00086         self.criterion2index[crit_id] = indices
00087         for index in indices:
00088             value = self.index2criterion.get(index, ())
00089             self.index2criterion[index] = value + (crit_id,)
00090 
00091 
00092     def unregister(self, criterion):
00093         crit_id = criterion.meta_type
00094         self.pop(crit_id)
00095         self.criterion2index.pop(crit_id)
00096         for (index, value) in self.index2criterion.items():
00097             if id in value:
00098                 valuelist = list(value)
00099                 del valuelist[valuelist.index(crit_id)]
00100                 self.index2criterion[index] = tuple(valuelist)
00101 
00102     def listTypes(self):
00103         return self.keys()
00104 
00105     def listSortTypes(self):
00106         return [key for key in self.keys()
00107                     if IATTopicSortCriterion.isImplementedByInstancesOf(self[key])]
00108 
00109     def listSearchTypes(self):
00110         return [key for key in self.keys()
00111                     if IATTopicSearchCriterion.isImplementedByInstancesOf(self[key])]
00112 
00113     def listCriteria(self):
00114         return self.values()
00115 
00116     def indicesByCriterion(self, criterion):
00117         return self.criterion2index[criterion]
00118 
00119     def criteriaByIndex(self, index):
00120         try:
00121             return self.index2criterion[index]
00122         except KeyError:
00123             return ()
00124     
00125     def getPortalTypes(self):
00126         return tuple(self.portaltypes.keys())
00127 
00128 _criterionRegistry = _CriterionRegistry()
00129 registerCriterion = _criterionRegistry.register
00130 unregisterCriterion = _criterionRegistry.unregister
00131 
00132 __all__ = ('registerCriterion', 'ALL_INDICES', 'DATE_INDICES', 'STRING_INDICES',
00133            'LIST_INDICES', 'SORT_INDICES', )
00134 
00135 # criteria
00136 from Products.ATContentTypes.criteria.boolean import ATBooleanCriterion
00137 from Products.ATContentTypes.criteria.date import ATDateCriteria
00138 from Products.ATContentTypes.criteria.daterange import ATDateRangeCriterion
00139 from Products.ATContentTypes.criteria.list import ATListCriterion
00140 from Products.ATContentTypes.criteria.portaltype import ATPortalTypeCriterion
00141 from Products.ATContentTypes.criteria.reference import ATReferenceCriterion
00142 from Products.ATContentTypes.criteria.selection import ATSelectionCriterion
00143 from Products.ATContentTypes.criteria.simpleint import ATSimpleIntCriterion
00144 from Products.ATContentTypes.criteria.simplestring import ATSimpleStringCriterion
00145 from Products.ATContentTypes.criteria.sort import ATSortCriterion
00146 from Products.ATContentTypes.criteria.currentauthor import ATCurrentAuthorCriterion
00147 from Products.ATContentTypes.criteria.path import ATPathCriterion
00148 from Products.ATContentTypes.criteria.relativepath import ATRelativePathCriterion