1 | from locale import strcoll |
---|
2 | from Products.CMFCore import CMFCorePermissions |
---|
3 | from Products.CMFCore.utils import getToolByName |
---|
4 | from AccessControl import ClassSecurityInfo |
---|
5 | from Acquisition import aq_parent, aq_inner |
---|
6 | |
---|
7 | from Products.ATContentTypes.content.base import updateActions |
---|
8 | from Products.ATContentTypes.content.topic import ATTopic, IGNORED_FIELDS |
---|
9 | from Products.ATContentTypes.interfaces import IATTopic |
---|
10 | from Products.ATContentTypes.types.criteria import _criterionRegistry |
---|
11 | from Products.ATContentTypes.permission import ChangeTopics, AddTopics |
---|
12 | from Products.CMFCore.permissions import View |
---|
13 | from Products.ATContentTypes.content.topic import ATTopicSchema |
---|
14 | from Products.ATContentTypes.interfaces import IATTopicSearchCriterion, IATTopicSortCriterion |
---|
15 | from Products.Archetypes.public import * |
---|
16 | from config import * |
---|
17 | from Products.CMFPlone.PloneBatch import Batch |
---|
18 | from Products.ATContentTypes.config import TOOLNAME |
---|
19 | from StringIO import StringIO |
---|
20 | from csv import DictWriter |
---|
21 | import csv |
---|
22 | |
---|
23 | qTopic_schema = ATTopicSchema.copy() + Schema(( |
---|
24 | StringField("catalog", |
---|
25 | default = "portal_catalog", |
---|
26 | vocabulary = "getCatalogList", |
---|
27 | widget = SelectionWidget(label="Catalog", |
---|
28 | label_msgid="label_catalog", |
---|
29 | description="Select catalog to query", |
---|
30 | description_msgid="description_catalog") |
---|
31 | ), |
---|
32 | BooleanField("showHeader", |
---|
33 | schemata = "export", |
---|
34 | default = 1, |
---|
35 | widget = BooleanWidget(label="Print field headers", |
---|
36 | label_msgid="label_print_headers", |
---|
37 | description="Check if headers need to be printed", |
---|
38 | description_msgid="description_print_headers") |
---|
39 | ), |
---|
40 | StringField("delimiter", |
---|
41 | default = ";", |
---|
42 | schemata = "export", |
---|
43 | widget = StringWidget(label="Values delimiter", |
---|
44 | label_msgid="label_delimiter", |
---|
45 | description="Select delimiter to be used in CSV", |
---|
46 | description_msgid="description_delimiter", |
---|
47 | size = 4) |
---|
48 | ), |
---|
49 | )) |
---|
50 | qTopic_schema["customViewFields"].schemata = "export" |
---|
51 | qTopic_schema["customViewFields"].vocabulary = "listMetaDataFields" |
---|
52 | qTopic_schema["customViewFields"].default=("id","getFullName","getEmail") |
---|
53 | |
---|
54 | class qTopic(ATTopic): |
---|
55 | """A topic folder""" |
---|
56 | meta_type = "qTopic" |
---|
57 | portal_type = "qTopic" |
---|
58 | archetype_name = "qTopic" |
---|
59 | typeDescription= ("qTopic is the same topic but with "+ |
---|
60 | "option of catlog selection") |
---|
61 | typeDescMsgId = "description_edit_topic" |
---|
62 | |
---|
63 | schema = qTopic_schema |
---|
64 | security = ClassSecurityInfo() |
---|
65 | actions = updateActions(ATTopic, |
---|
66 | ( |
---|
67 | { |
---|
68 | "id" : "export_csv", |
---|
69 | "name" : "Export in CSV", |
---|
70 | "action" : "string:${folder_url}/result_csv", |
---|
71 | "permissions" : (CMFCorePermissions.View,) |
---|
72 | }, |
---|
73 | ) |
---|
74 | ) |
---|
75 | |
---|
76 | def getCatalogList(self): |
---|
77 | """ return list of catalog ids |
---|
78 | """ |
---|
79 | at_tool = getToolByName(self, "archetype_tool") |
---|
80 | catalogs = at_tool.getCatalogsInSite() |
---|
81 | return DisplayList(zip(catalogs, catalogs)) |
---|
82 | |
---|
83 | security.declareProtected(ChangeTopics, "criteriaByIndexId") |
---|
84 | def criteriaByIndexId(self, indexId): |
---|
85 | """ get createrias by index """ |
---|
86 | catalog_tool = getToolByName(self, self.getCatalog()) |
---|
87 | indexObj = catalog_tool.Indexes[indexId] |
---|
88 | results = _criterionRegistry.criteriaByIndex(indexObj.meta_type) |
---|
89 | return results |
---|
90 | |
---|
91 | security.declareProtected(View, 'allowedCriteriaForField') |
---|
92 | def allowedCriteriaForField(self, field, display_list=False): |
---|
93 | """ Return all valid criteria for a given field. Optionally include |
---|
94 | descriptions in list in format [(desc1, val1) , (desc2, val2)] for |
---|
95 | javascript selector.""" |
---|
96 | tool = getToolByName(self, self.getCatalog()) |
---|
97 | criteria = _criterionRegistry.listTypes() |
---|
98 | allowed = [crit for crit in criteria |
---|
99 | if crit in self.criteriaByIndexId(field)] |
---|
100 | if display_list: |
---|
101 | flat = [] |
---|
102 | for a in allowed: |
---|
103 | desc = _criterionRegistry[a].shortDesc |
---|
104 | flat.append((a,desc)) |
---|
105 | allowed = DisplayList(flat) |
---|
106 | return allowed |
---|
107 | |
---|
108 | security.declareProtected(ChangeTopics, "listFields") |
---|
109 | def listFields(self): |
---|
110 | """Return a list of fields from portal_catalog. |
---|
111 | """ |
---|
112 | tool = getToolByName(self, TOOLNAME) |
---|
113 | return tool.getEnabledFields(catalog_name=self.getCatalog()) |
---|
114 | |
---|
115 | security.declareProtected(ChangeTopics, 'listAvailableFields') |
---|
116 | def listAvailableFields(self): |
---|
117 | """Return a list of available fields for new criteria. |
---|
118 | """ |
---|
119 | return self.listFields() |
---|
120 | |
---|
121 | security.declareProtected(View, 'listMetaDataFields') |
---|
122 | def listMetaDataFields(self, exclude=True): |
---|
123 | """Return a list of metadata fields from portal_catalog. |
---|
124 | """ |
---|
125 | tool = getToolByName(self, TOOLNAME) |
---|
126 | catalog_name=self.getCatalog() |
---|
127 | return tool.getMetadataDisplay(exclude,catalog_name=catalog_name) |
---|
128 | |
---|
129 | security.declareProtected(CMFCorePermissions.View, "queryCatalog") |
---|
130 | def queryCatalog(self, REQUEST=None, batch=False, b_size=None, |
---|
131 | full_objects=False, **kw): |
---|
132 | """Invoke the catalog using our criteria to augment any passed |
---|
133 | in query before calling the catalog. |
---|
134 | """ |
---|
135 | if REQUEST is None: |
---|
136 | REQUEST = getattr(self, 'REQUEST', {}) |
---|
137 | b_start = REQUEST.get('b_start', 0) |
---|
138 | |
---|
139 | q = self.buildQuery() |
---|
140 | if q is None: |
---|
141 | # empty query - do not show anything |
---|
142 | if batch: |
---|
143 | return Batch([], 20, int(b_start), orphan=0) |
---|
144 | return [] |
---|
145 | # Allow parameters to further limit existing criterias |
---|
146 | for k,v in q.items(): |
---|
147 | if kw.has_key(k): |
---|
148 | arg = kw.get(k) |
---|
149 | if isinstance(arg, (ListType,TupleType)) and isinstance(v, (ListType,TupleType)): |
---|
150 | kw[k] = [x for x in arg if x in v] |
---|
151 | elif isinstance(arg, StringType) and isinstance(v, (ListType,TupleType)) and arg in v: |
---|
152 | kw[k] = [arg] |
---|
153 | else: |
---|
154 | kw[k]=v |
---|
155 | else: |
---|
156 | kw[k]=v |
---|
157 | #kw.update(q) |
---|
158 | pcatalog = getToolByName(self, self.getCatalog()) |
---|
159 | limit = self.getLimitNumber() |
---|
160 | max_items = self.getItemCount() |
---|
161 | # Batch based on limit size if b_szie is unspecified |
---|
162 | if max_items and b_size is None: |
---|
163 | b_size = int(max_items) |
---|
164 | else: |
---|
165 | b_size = 20 |
---|
166 | if limit and max_items and self.hasSortCriterion(): |
---|
167 | # Sort limit helps Zope 2.6.1+ to do a faster query |
---|
168 | # sorting when sort is involved |
---|
169 | # See: http://zope.org/Members/Caseman/ZCatalog_for_2.6.1 |
---|
170 | kw.setdefault('sort_limit', max_items) |
---|
171 | __traceback_info__ = (self, kw,) |
---|
172 | results = pcatalog.searchResults(REQUEST, **kw) |
---|
173 | if full_objects and not limit: |
---|
174 | results = [b.getObject() for b in results] |
---|
175 | if batch: |
---|
176 | batch = Batch(results, b_size, int(b_start), orphan=0) |
---|
177 | return batch |
---|
178 | if limit: |
---|
179 | if full_objects: |
---|
180 | return [b.getObject() for b in results[:max_items]] |
---|
181 | return results[:max_items] |
---|
182 | return results |
---|
183 | |
---|
184 | def toCSV(self, fields, data): |
---|
185 | dialect = csv.excel() |
---|
186 | dialect.delimiter = self.getDelimiter() |
---|
187 | buffer = StringIO() |
---|
188 | writer = DictWriter(buffer, fieldnames=fields, dialect=dialect) |
---|
189 | if self.getShowHeader(): |
---|
190 | writer.writerow(dict(zip(fields, fields))) |
---|
191 | writer.writerows(data) |
---|
192 | return buffer.getvalue() |
---|
193 | |
---|
194 | |
---|
195 | registerType(qTopic, PROJECTNAME) |
---|
196 | |
---|
197 | def modify_fti(fti): |
---|
198 | """Remove folderlisting action |
---|
199 | """ |
---|
200 | actions = [] |
---|
201 | fti['allowed_content_types'] = ('qTopic',) |
---|
202 | fti['filter_content_types'] = 1 |
---|
203 | for action in fti["actions"]: |
---|
204 | if action["id"] == "folderlisting": |
---|
205 | action["visible"] = False |
---|
206 | #actions.append(action) |
---|
207 | #fti["actions"] = tuple(actions) |
---|