/
vocabulary.py
391 lines (348 loc) · 13.4 KB
/
vocabulary.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from AccessControl import getSecurityManager
from Acquisition import aq_base
from html import unescape
from logging import getLogger
from plone.app.content.utils import json_dumps
from plone.app.content.utils import json_loads
from plone.app.querystring import queryparser
from plone.app.z3cform.interfaces import IFieldPermissionChecker
from plone.autoform.interfaces import WRITE_PERMISSIONS_KEY
from plone.base import PloneMessageFactory as _
from plone.base.interfaces.siteroot import INavigationRoot
from plone.base.navigationroot import get_navigation_root
from plone.base.utils import safe_text
from plone.memoize.view import memoize
from plone.supermodel.utils import mergedTaggedValueDict
from Products.CMFCore.utils import getToolByName
from Products.Five import BrowserView
from Products.MimetypesRegistry.MimeTypeItem import guess_icon_path
from Products.MimetypesRegistry.MimeTypeItem import PREFIX
from Products.PortalTransforms.transforms.safe_html import SafeHTML
from types import FunctionType
from z3c.form.interfaces import IAddForm
from z3c.form.interfaces import ISubForm
from zope.component import getUtility
from zope.component import queryAdapter
from zope.component import queryUtility
from zope.deprecation import deprecated
from zope.i18n import translate
from zope.schema.interfaces import ICollection
from zope.schema.interfaces import IVocabularyFactory
from zope.security.interfaces import IPermission
import inspect
import itertools
logger = getLogger(__name__)
MAX_BATCH_SIZE = 500 # prevent overloading server
DEFAULT_PERMISSION = "View"
DEFAULT_PERMISSION_SECURE = "Modify portal content"
PERMISSIONS = {
"plone.app.vocabularies.Catalog": "View",
"plone.app.vocabularies.Keywords": "Modify portal content",
"plone.app.vocabularies.SyndicatableFeedItems": "Modify portal content",
"plone.app.vocabularies.Users": "Modify portal content",
"plone.app.multilingual.RootCatalog": "View",
}
TRANSLATED_IGNORED = [
"author_name",
"cmf_uid",
"commentators",
"created",
"CreationDate",
"Creator",
"Date",
"Description",
"effective",
"EffectiveDate",
"end",
"exclude_from_nav",
"ExpirationDate",
"expires",
"getIcon",
"getMimeIcon",
"getId",
"getObjSize",
"getRemoteUrl",
"getURL",
"id",
"in_response_to",
"is_folderish",
"last_comment_date",
"listCreators",
"location",
"meta_type",
"ModificationDate",
"modified",
"path",
"portal_type",
"review_state",
"start",
"Subject",
"sync_uid",
"Title",
"total_comments" "UID",
]
_permissions = PERMISSIONS
deprecated("_permissions", "Use PERMISSIONS variable instead.")
def _parseJSON(s):
# XXX this should be changed to a try loads except return s
if isinstance(s, str):
s = s.strip()
if (s.startswith("{") and s.endswith("}")) or (
s.startswith("[") and s.endswith("]")
): # detect if json
return json_loads(s)
return s
_unsafe_metadata = [
"author_name",
"commentors",
"Creator",
"listCreators",
]
_safe_callable_metadata = [
"getIcon",
"getPath",
"getURL",
"is_folderish",
"review_state",
]
class VocabLookupException(Exception):
pass
class BaseVocabularyView(BrowserView):
def get_translated_ignored(self):
return TRANSLATED_IGNORED
def get_base_path(self, context):
return get_navigation_root(context)
def __call__(self):
"""
Accepts GET parameters of:
name: Name of the vocabulary
field: Name of the field the vocabulary is being retrieved for
query: string or json object of criteria and options.
json value consists of a structure:
{
criteria: object,
sort_on: index,
sort_order: (asc|reversed)
}
attributes: comma separated, or json object list
batch: {
page: 1-based page of results,
size: size of paged results
}
"""
context = self.get_context()
self.request.response.setHeader(
"Content-Type", "application/json; charset=utf-8"
)
try:
vocabulary = self.get_vocabulary()
except VocabLookupException as e:
return json_dumps({"error": e.args[0]})
results_are_brains = False
if hasattr(vocabulary, "search_catalog"):
query = self.parsed_query()
results = vocabulary.search_catalog(query)
results_are_brains = True
elif hasattr(vocabulary, "search"):
try:
query = self.parsed_query()["SearchableText"]["query"]
except KeyError:
results = iter(vocabulary)
else:
results = vocabulary.search(query)
else:
results = vocabulary
try:
total = len(results)
except TypeError:
# do not error if object does not support __len__
# we'll check again later if we can figure some size
# out
total = 0
# get batch
batch = _parseJSON(self.request.get("batch", ""))
if batch and ("size" not in batch or "page" not in batch):
batch = None # batching not providing correct options
if batch:
# must be sliceable for batching support
page = int(batch["page"])
size = int(batch["size"])
if size > MAX_BATCH_SIZE:
raise Exception("Max batch size is 500")
# page is being passed in is 1-based
start = (max(page - 1, 0)) * size
end = start + size
# Try __getitem__-based slice, then iterator slice.
# The iterator slice has to consume the iterator through
# to the desired slice, but that shouldn't be the end
# of the world because at some point the user will hopefully
# give up scrolling and search instead.
try:
results = results[start:end]
except TypeError:
results = itertools.islice(results, start, end)
# build result items
items = []
attributes = _parseJSON(self.request.get("attributes", ""))
if isinstance(attributes, str) and attributes:
attributes = attributes.split(",")
translate_ignored = self.get_translated_ignored()
transform = SafeHTML()
if attributes:
base_path = self.get_base_path(context)
sm = getSecurityManager()
can_edit = sm.checkPermission(DEFAULT_PERMISSION_SECURE, context)
mtt = getToolByName(self.context, "mimetypes_registry")
for vocab_item in results:
if not results_are_brains:
vocab_item = vocab_item.value
item = {}
for attr in attributes:
key = attr
if ":" in attr:
key, attr = attr.split(":", 1)
if attr in _unsafe_metadata and not can_edit:
continue
if key == "path":
attr = "getPath"
val = getattr(vocab_item, attr, None)
if callable(val):
if attr in _safe_callable_metadata:
val = val()
else:
continue
if key == "path" and val is not None:
val = val[len(base_path) :]
if key not in translate_ignored and isinstance(val, str):
val = translate(_(safe_text(val)), context=self.request)
item[key] = val
if key == "getMimeIcon":
item[key] = None
# get mime type icon url from mimetype registry'
contenttype = aq_base(getattr(vocab_item, "mime_type", None))
if contenttype:
ctype = mtt.lookup(contenttype)
if ctype:
item[key] = "/".join(
[base_path, guess_icon_path(ctype[0])]
)
else:
item[key] = "/".join(
[
base_path,
PREFIX.rstrip("/"),
"unknown.png",
]
)
items.append(item)
else:
items = [
{
"id": unescape(transform.scrub_html(item.value)),
"text": (
unescape(transform.scrub_html(item.title)) if item.title else ""
),
}
for item in results
]
if total == 0:
total = len(items)
return json_dumps({"results": items, "total": total})
def parsed_query(
self,
):
query = _parseJSON(self.request.get("query", ""))
if isinstance(query, str):
query = {"SearchableText": {"query": query}}
elif query:
parsed = queryparser.parseFormquery(self.get_context(), query["criteria"])
if "sort_on" in query:
parsed["sort_on"] = query["sort_on"]
if "sort_order" in query:
parsed["sort_order"] = str(query["sort_order"])
query = parsed
else:
query = {}
return query
class VocabularyView(BaseVocabularyView):
"""Queries a named vocabulary and returns JSON-formatted results."""
def get_context(self):
return self.context
def get_vocabulary(self):
# Look up named vocabulary and check permission.
context = self.context
factory_name = self.request.get("name", None)
field_name = self.request.get("field", None)
if not factory_name:
raise VocabLookupException("No factory provided.")
authorized = None
sm = getSecurityManager()
if factory_name not in PERMISSIONS or not INavigationRoot.providedBy(context):
# Check field specific permission
if field_name:
permission_checker = queryAdapter(context, IFieldPermissionChecker)
if permission_checker is not None:
authorized = permission_checker.validate(field_name, factory_name)
elif sm.checkPermission(
PERMISSIONS.get(factory_name, DEFAULT_PERMISSION), context
):
# If no checker, fall back to checking the global registry
authorized = True
if not authorized:
raise VocabLookupException("Vocabulary lookup not allowed")
# Short circuit if we are on the site root and permission is
# in global registry
elif not sm.checkPermission(
PERMISSIONS.get(factory_name, DEFAULT_PERMISSION), context
):
raise VocabLookupException("Vocabulary lookup not allowed")
factory = queryUtility(IVocabularyFactory, factory_name)
if not factory:
raise VocabLookupException(
'No factory with name "%s" exists.' % factory_name
)
# This part is for backwards-compatibility with the first
# generation of vocabularies created for plone.app.widgets,
# which take the (unparsed) query as a parameter of the vocab
# factory rather than as a separate search method.
if isinstance(factory, FunctionType):
factory_spec = inspect.getfullargspec(factory)
else:
factory_spec = inspect.getfullargspec(factory.__call__)
query = _parseJSON(self.request.get("query", ""))
if query and "query" in factory_spec.args:
vocabulary = factory(context, query=query)
else:
# This is what is reached for non-legacy vocabularies.
vocabulary = factory(context)
return vocabulary
class SourceView(BaseVocabularyView):
"""Queries a field's source and returns JSON-formatted results."""
def get_context(self):
if ISubForm.providedBy(self.context.form):
context = self.context.form.parentForm.context
else:
context = self.context.context
return context
@property
@memoize
def default_permission(self):
if IAddForm.providedBy(self.context.form):
return "cmf.AddPortalContent"
return "cmf.ModifyPortalContent"
def get_vocabulary(self):
widget = self.context
field = widget.field.bind(widget.context)
# check field's write permission
info = mergedTaggedValueDict(field.interface, WRITE_PERMISSIONS_KEY)
permission_name = info.get(field.__name__, self.default_permission)
permission = queryUtility(IPermission, name=permission_name)
if permission is None:
permission = getUtility(IPermission, name=self.default_permission)
if not getSecurityManager().checkPermission(
permission.title, self.get_context()
):
raise VocabLookupException("Vocabulary lookup not allowed.")
if ICollection.providedBy(field):
return field.value_type.vocabulary
return field.vocabulary