7d693db9f6aac1dca9dc369fca02a3d4c23138f0
[Plinn.git] / Topic.py
1 #######################################################################################
2 # Plinn - http://plinn.org #
3 # Copyright © 2005-2009 Benoît PIN <benoit.pin@ensmp.fr> #
4 # #
5 # This program is free software; you can redistribute it and/or #
6 # modify it under the terms of the GNU General Public License #
7 # as published by the Free Software Foundation; either version 2 #
8 # of the License, or (at your option) any later version. #
9 # #
10 # This program is distributed in the hope that it will be useful, #
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
13 # GNU General Public License for more details. #
14 # #
15 # You should have received a copy of the GNU General Public License #
16 # along with this program; if not, write to the Free Software #
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #
18 #######################################################################################
19 """ Plinn Topic
20
21 $Id: Topic.py 1518 2009-07-01 14:55:37Z pin $
22 $URL: http://svn.cri.ensmp.fr/svn/Plinn/branches/CMF-2.1/Topic.py $
23 """
24
25 from Globals import InitializeClass
26 from zope.component.factory import Factory
27 from AccessControl.SecurityInfo import ClassSecurityInfo
28 from Products.CMFCore.permissions import View, ListFolderContents
29 from Products.CMFTopic.permissions import AddTopics, ChangeTopics
30 from Products.CMFTopic.Topic import Topic as BaseTopic
31 from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
32 from Products.CMFCore.utils import getToolByName
33 from Folder import PlinnFolder
34 from types import ListType, TupleType, StringTypes
35
36 from sets import Set
37
38
39 class Topic(BaseTopic, PlinnFolder):
40 """ CMF Topic with Dublin core metadata support """
41
42 security = ClassSecurityInfo()
43
44 security.declareProtected(ChangeTopics, 'listAvailableFields')
45 def listAvailableFields( self ):
46 """ Return a list of available fields for new criteria.
47 """
48 fields = Set(BaseTopic.listAvailableFields(self))
49 utool = getToolByName(self, "portal_url")
50 portal = utool.getPortalObject()
51 unusedTopicFields = Set(portal.getProperty("unused_topic_fields", []))
52 return fields - unusedTopicFields
53
54 security.declareProtected(View, 'queryCatalog')
55 def queryCatalog(self, REQUEST=None, **kw) :
56 """ Invoke the catalog using our criteria.
57 remove Member Data results
58 """
59 kw.update( self.buildQuery() )
60
61 ttool = getToolByName(self, 'portal_types')
62 if not kw.has_key('portal_type') :
63 kw['portal_type'] = ttool.objectIds()
64 else :
65 if type(kw['portal_type']) == ListType :
66 try : kw['portal_type'].remove('Member Data')
67 except : pass
68 if type(kw['portal_type']) == TupleType :
69 ptypes = list(kw['portal_type'])
70 try :
71 ptypes.remove('Member Data')
72 kw['portal_type'] = tuple(ptypes)
73 except : pass
74 elif kw['portal_type'] == 'Member Data' :
75 kw['portal_type'] = ttool.objectIds()
76
77 portal_catalog = getToolByName( self, 'portal_catalog' )
78 return portal_catalog.searchResults(REQUEST, **kw)
79
80
81 security.declareProtected(ChangeTopics, 'loadSearchQuery')
82 def loadSearchQuery(self, query):
83 sort_on = query.pop('sort_on')
84 sort_order = query.pop('sort_order')
85 ctool = getToolByName(self, 'portal_catalog')
86 hasindex = ctool._catalog.indexes.has_key
87
88 for k, v in query.items() :
89 if not hasindex(k) : continue
90 if isinstance(v, StringTypes) :
91 self.addCriterion(k, 'String Criterion')
92 crit = self.getCriterion(k)
93 crit.edit(v)
94 elif isinstance(v, (ListType, TupleType)) :
95 if len(v) == 1 :
96 self.addCriterion(k, 'String Criterion')
97 crit = self.getCriterion(k)
98 crit.edit(v[0])
99 else :
100 self.addCriterion(k, 'List Criterion')
101 crit = self.getCriterion(k)
102 crit.edit(value=v, operator='or')
103 elif isinstance(v, dict) :
104 self.addCriterion(k, v.pop('critType'))
105 crit = self.getCriterion(k)
106 crit.edit(**v)
107
108 self.addCriterion(sort_on, 'Sort Criterion')
109 sort_crit = self.getCriterion(sort_on)
110 if sort_order == 'reverse' :
111 sort_crit.edit(True)
112 else :
113 sort_crit.edit(False)
114
115 security.declareProtected(ChangeTopics, 'getCompatibleCriteriaFor')
116 def getCompatibleCriteriaFor(self, fieldName) :
117 """ Return a list of criteria which belong to the field """
118
119 pass
120 InitializeClass(Topic)
121 TopicFactory = Factory(Topic)
122
123
124 def addTopic(dispatcher, id, title='', REQUEST=None):
125 """ Create an empty topic.
126 """
127 topic = Topic( id )
128 topic.id = id
129 topic.title = title
130 dest = dispatcher.Destination()
131 dest._setObject( id, topic )
132
133 if REQUEST is not None:
134 REQUEST['RESPONSE'].redirect( 'manage_main' )