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