-
-
Notifications
You must be signed in to change notification settings - Fork 182
/
PloneTool.py
1258 lines (1084 loc) · 46.7 KB
/
PloneTool.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from AccessControl import ClassSecurityInfo
from AccessControl import getSecurityManager
from AccessControl import Unauthorized
from AccessControl.requestmethod import postonly
from Acquisition import aq_base
from Acquisition import aq_inner
from Acquisition import aq_parent
from App.class_init import InitializeClass
from ComputedAttribute import ComputedAttribute
from DateTime import DateTime
from email.Utils import getaddresses
from OFS.ObjectManager import bad_id
from OFS.SimpleItem import SimpleItem
from plone.registry.interfaces import IRegistry
from Products.CMFCore.interfaces import IDublinCore
from Products.CMFCore.interfaces import IMutableDublinCore
from Products.CMFCore.permissions import AccessContentsInformation
from Products.CMFCore.permissions import ManagePortal
from Products.CMFCore.permissions import ManageUsers
from Products.CMFCore.permissions import ModifyPortalContent
from Products.CMFCore.permissions import View
from Products.CMFCore.utils import getToolByName
from Products.CMFCore.utils import UniqueObject
from Products.CMFCore.WorkflowCore import WorkflowException
from Products.CMFDynamicViewFTI.interfaces import IBrowserDefault
from Products.CMFPlone import utils
from Products.CMFPlone.defaultpage import check_default_page_via_view
from Products.CMFPlone.defaultpage import get_default_page_via_view
from Products.CMFPlone.events import ReorderedEvent
from Products.CMFPlone.interfaces import INonStructuralFolder
from Products.CMFPlone.interfaces import IPloneTool
from Products.CMFPlone.interfaces import ISearchSchema
from Products.CMFPlone.interfaces import ISecuritySchema
from Products.CMFPlone.interfaces import ISiteSchema
from Products.CMFPlone.log import log_deprecated
from Products.CMFPlone.PloneBaseTool import PloneBaseTool
from Products.CMFPlone.PloneFolder import ReplaceableWrapper
from Products.CMFPlone.utils import base_hasattr
from Products.CMFPlone.utils import log
from Products.CMFPlone.utils import log_exc
from Products.CMFPlone.utils import safe_hasattr
from Products.CMFPlone.utils import transaction_note
from Products.statusmessages.interfaces import IStatusMessage
from types import UnicodeType
from ZODB.POSException import ConflictError
from zope.component import getUtility
from zope.component import queryAdapter
from zope.deprecation import deprecate
from zope.event import notify
from zope.interface import implementer
from zope.lifecycleevent import ObjectModifiedEvent
import re
import sys
import transaction
import urlparse
_marker = utils._marker
_icons = {}
CEILING_DATE = DateTime(2500, 0) # never expires
FLOOR_DATE = __FLOOR_DATE = DateTime(1970, 0) # always effective
BAD_CHARS = bad_id.__self__.findall
# max 63 chars per label in domains, see RFC1035
EMAIL_RE = re.compile(r"^(\w&.%#$&'\*+-/=?^_`{}|~]+!)*[\w&.%#$&'\*+-/=?^_`{}|~]+@(([0-9a-z]([0-9a-z-]*[0-9a-z])?\.)+[a-z]{2,63}|([0-9]{1,3}\.){3}[0-9]{1,3})$", re.IGNORECASE) # noqa
# used to find double new line (in any variant)
EMAIL_CUTOFF_RE = re.compile(r".*[\n\r][\n\r]")
# dublic core accessor name -> metadata name
METADATA_DCNAME = {
# The first two rows are handle in a special way
# 'Description' : 'description',
# 'Subject' : 'keywords',
'Description' : 'DC.description',
'Subject' : 'DC.subject',
'Creator' : 'DC.creator',
'Contributors' : 'DC.contributors',
'Publisher' : 'DC.publisher',
'CreationDate' : 'DC.date.created',
'ModificationDate' : 'DC.date.modified',
'Type' : 'DC.type',
'Format' : 'DC.format',
'Language' : 'DC.language',
'Rights' : 'DC.rights',
}
METADATA_DC_AUTHORFIELDS = ('Creator', 'Contributors', 'Publisher')
@implementer(IPloneTool)
class PloneTool(PloneBaseTool, UniqueObject, SimpleItem):
"""Various utility methods."""
id = 'plone_utils'
meta_type = 'Plone Utility Tool'
toolicon = 'skins/plone_images/site_icon.png'
security = ClassSecurityInfo()
plone_tool = 1
# Prefix for forms fields!?
field_prefix = 'field_'
@security.protected(ManageUsers)
def setMemberProperties(self, member, REQUEST=None, **properties):
pas = getToolByName(self, 'acl_users')
if safe_hasattr(member, 'getId'):
member = member.getId()
user = pas.getUserById(member)
user.setProperties(**properties)
@security.public
@deprecate(('`getSiteEncoding` is deprecated. Plone only supports UTF-8 '
'currently. This method always returns "utf-8"'))
def getSiteEncoding(self):
""" Get the the site encoding, which is utf-8.
>>> ptool = self.portal.plone_utils
>>> ptool.getSiteEncoding()
'utf-8'
"""
return 'utf-8'
@security.public
def portal_utf8(self, str, errors='strict'):
""" Transforms an string in portal encoding to utf8.
>>> ptool = self.portal.plone_utils
>>> text = u'Eksempel \xe6\xf8\xe5'
>>> sitetext = text.encode('utf-8')
>>> ptool.portal_utf8(sitetext) == text.encode('utf-8')
True
"""
return utils.portal_utf8(self, str, errors)
@security.public
def utf8_portal(self, str, errors='strict'):
""" Transforms an utf8 string to portal encoding.
>>> ptool = self.portal.plone_utils
>>> text = u'Eksempel \xe6\xf8\xe5'
>>> utf8text = text.encode('utf-8')
>>> ptool.utf8_portal(utf8text) == text.encode('utf-8')
True
"""
return utils.utf8_portal(self, str, errors)
@security.private
def getMailHost(self):
""" Gets the MailHost.
>>> ptool = self.portal.plone_utils
>>> ptool.getMailHost()
<MailHost ...>
"""
return getattr(aq_parent(self), 'MailHost')
@security.public
def validateSingleNormalizedEmailAddress(self, address):
"""Lower-level function to validate a single normalized email address,
see validateEmailAddress.
"""
if not isinstance(address, basestring):
return False
sub = EMAIL_CUTOFF_RE.match(address)
if sub is not None:
# Address contains two newlines (possible spammer relay attack)
return False
# sub is an empty string if the address is valid
sub = EMAIL_RE.sub('', address)
if sub == '':
return True
return False
@security.public
def validateSingleEmailAddress(self, address):
"""Validate a single email address, see also validateEmailAddresses."""
if not isinstance(address, basestring):
return False
sub = EMAIL_CUTOFF_RE.match(address)
if sub is not None:
# Address contains two newlines (spammer attack using
# "address\n\nSpam message")
return False
if len(getaddresses([address])) != 1:
# none or more than one address
return False
# Validate the address
for name, addr in getaddresses([address]):
if not self.validateSingleNormalizedEmailAddress(addr):
return False
return True
@security.public
def validateEmailAddresses(self, addresses):
"""Validate a list of possibly several email addresses, see also
validateSingleEmailAddress.
"""
if not isinstance(addresses, basestring):
return False
sub = EMAIL_CUTOFF_RE.match(addresses)
if sub is not None:
# Addresses contains two newlines (spammer attack using
# "To: list\n\nSpam message")
return False
# Validate each address
for name, addr in getaddresses([addresses]):
if not self.validateSingleNormalizedEmailAddress(addr):
return False
return True
@security.public
def editMetadata(self, obj, allowDiscussion=None, title=None,
subject=None, description=None, contributors=None,
effective_date=None, expiration_date=None, format=None,
language=None, rights=None, **kwargs):
"""Responsible for setting metadata on a content object.
We assume the obj implements IDublinCoreMetadata.
"""
mt = getToolByName(self, 'portal_membership')
if not mt.checkPermission(ModifyPortalContent, obj):
# FIXME: Some scripts rely on this being string?
raise Unauthorized
REQUEST = self.REQUEST
pfx = self.field_prefix
def getfield(request, name, default=None, pfx=pfx):
return request.form.get(pfx + name, default)
def tuplify(value):
return tuple(filter(None, value))
if IDublinCore.providedBy(obj):
if title is None:
title = getfield(REQUEST, 'title')
if description is None:
description = getfield(REQUEST, 'description')
if subject is None:
subject = getfield(REQUEST, 'subject')
if subject is not None:
subject = tuplify(subject)
if contributors is None:
contributors = getfield(REQUEST, 'contributors')
if contributors is not None:
contributors = tuplify(contributors)
if effective_date is None:
effective_date = getfield(REQUEST, 'effective_date')
if effective_date == '':
effective_date = 'None'
if expiration_date is None:
expiration_date = getfield(REQUEST, 'expiration_date')
if expiration_date == '':
expiration_date = 'None'
if IMutableDublinCore.providedBy(obj):
if title is not None:
obj.setTitle(title)
if description is not None:
obj.setDescription(description)
if subject is not None:
obj.setSubject(subject)
if contributors is not None:
obj.setContributors(contributors)
if effective_date is not None:
obj.setEffectiveDate(effective_date)
if expiration_date is not None:
obj.setExpirationDate(expiration_date)
if format is not None:
obj.setFormat(format)
if language is not None:
obj.setLanguage(language)
if rights is not None:
obj.setRights(rights)
# Make the catalog aware of changes
obj.reindexObject()
def _renameObject(self, obj, id):
if not id:
REQUEST = self.REQUEST
id = REQUEST.get('id', '')
id = REQUEST.get(self.field_prefix + 'id', '')
if id != obj.getId():
parent = aq_parent(aq_inner(obj))
parent.manage_renameObject(obj.getId(), id)
def _makeTransactionNote(self, obj, msg=''):
# TODO Why not aq_parent()?
relative_path = '/'.join(
getToolByName(self, 'portal_url').getRelativeContentPath(obj)[:-1]
)
if not msg:
msg = relative_path + '/' + obj.title_or_id() \
+ ' has been modified.'
if isinstance(msg, UnicodeType):
# Convert unicode to a regular string for the backend write IO.
# UTF-8 is the only reasonable choice, as using unicode means
# that Latin-1 is probably not enough.
msg = msg.encode('utf-8')
if not transaction.get().description:
transaction_note(msg)
@security.public
def contentEdit(self, obj, **kwargs):
"""Encapsulates how the editing of content occurs."""
try:
self.editMetadata(obj, **kwargs)
except AttributeError, msg:
log('Failure editing metadata at: %s.\n%s\n' %
(obj.absolute_url(), msg))
if kwargs.get('id', None) is not None:
self._renameObject(obj, id=kwargs['id'].strip())
self._makeTransactionNote(obj)
@security.public
def availableMIMETypes(self):
"""Returns a map of mimetypes.
Requires mimetype registry from Archetypes >= 1.3.
"""
mtr = getToolByName(self, 'mimetypes_registry')
return mtr.list_mimetypes()
@security.protected(View)
def getWorkflowChainFor(self, object):
"""Proxy the request for the chain to the workflow tool, as
this method is private there.
"""
wftool = getToolByName(self, 'portal_workflow')
wfs = ()
try:
wfs = wftool.getChainFor(object)
except ConflictError:
raise
except:
pass
return wfs
@security.protected(View)
def getIconFor(self, category, id, default=_marker, context=None):
"""Get an icon for an action, from its icon_expr.
"""
if context is None:
context = aq_parent(self)
action_chain = '%s/%s' % (category, id)
if category == 'controlpanel':
tool = getToolByName(context, 'portal_controlpanel')
actions = [ai for ai in tool.listActionInfos() if ai['id'] == id]
else:
tool = getToolByName(context, 'portal_actions')
actions = tool.listActionInfos(
action_chain=action_chain, object=context)
if len(actions) > 0:
icon = actions[0].get('icon', None)
if icon:
return icon
else:
if default is not _marker:
icon = default
else:
raise KeyError(action_chain)
return icon
@security.protected(View)
def getReviewStateTitleFor(self, obj):
"""Utility method that gets the workflow state title for the
object's review_state.
Returns None if no review_state found.
>>> ptool = self.portal.plone_utils
>>> ptool.getReviewStateTitleFor(self.folder).lower()
'public draft'
"""
wf_tool = getToolByName(self, 'portal_workflow')
wfs = ()
objstate = None
try:
objstate = wf_tool.getInfoFor(obj, 'review_state')
wfs = wf_tool.getWorkflowsFor(obj)
except WorkflowException:
pass
if wfs:
for w in wfs:
if objstate in w.states:
return w.states[objstate].title or objstate
return None
@security.protected(ManagePortal)
def changeOwnershipOf(self, object, userid, recursive=0, REQUEST=None):
"""Changes the ownership of an object."""
membership = getToolByName(self, 'portal_membership')
acl_users = getattr(self, 'acl_users')
user = acl_users.getUserById(userid)
if user is None:
# The user could be in the top level acl_users folder in
# the Zope root, in which case this should find him:
user = membership.getMemberById(userid)
if user is None:
raise KeyError(
'Only retrievable users in this site can be made owners.')
# Be careful not to pass MemberData to changeOwnership
user = user.getUser()
object.changeOwnership(user, recursive)
def fixOwnerRole(object, user_id):
# Get rid of all other owners
owners = object.users_with_local_role('Owner')
for o in owners:
roles = list(object.get_local_roles_for_userid(o))
roles.remove('Owner')
if roles:
object.manage_setLocalRoles(o, roles)
else:
object.manage_delLocalRoles([o])
# Fix for 1750
roles = list(object.get_local_roles_for_userid(user_id))
roles.append('Owner')
object.manage_setLocalRoles(user_id, roles)
fixOwnerRole(object, user.getId())
if base_hasattr(object, 'reindexObject'):
object.reindexObject()
if recursive:
catalog_tool = getToolByName(self, 'portal_catalog')
purl = getToolByName(self, 'portal_url')
_path = purl.getRelativeContentURL(object)
subobjects = [
b.getObject()
for b in catalog_tool(path={'query': _path, 'level': 1})
]
for obj in subobjects:
fixOwnerRole(obj, user.getId())
if base_hasattr(obj, 'reindexObject'):
obj.reindexObject()
changeOwnershipOf = postonly(changeOwnershipOf)
@security.public
def urlparse(self, url):
"""Returns the pieces of url in a six-part tuple.
See Python standard library urlparse.urlparse:
http://python.org/doc/lib/module-urlparse.html
>>> ptool = self.portal.plone_utils
>>> url = 'http://dev.plone.org/plone/query?milestone=2.1#foo'
>>> tuple(ptool.urlparse(url))
('http', 'dev.plone.org', '/plone/query', '', 'milestone=2.1', 'foo')
New in Python 2.6: urlparse now returns a ParseReusult object.
We just need the tuple form which is tuple(result).
"""
return tuple(urlparse.urlparse(url))
@security.public
def urlunparse(self, url_tuple):
"""Puts a url back together again, in the manner that
urlparse breaks it.
See also Python standard library: urlparse.urlunparse:
http://python.org/doc/lib/module-urlparse.html
>>> ptool = self.portal.plone_utils
>>> ptool.urlunparse(
... ('http', 'plone.org', '/support', '', '', 'users'))
'http://plone.org/support#users'
"""
return urlparse.urlunparse(url_tuple)
# Enable scripts to get the string value of an exception even if the
# thrown exception is a string and not a subclass of Exception.
def exceptionString(self):
# Don't assign the traceback to s
# (otherwise will generate a circular reference)
s = sys.exc_info()[:2]
if s[0] == None:
return None
if isinstance(s[0], basestring):
return s[0]
return str(s[1])
# Provide a way of dumping an exception to the log even if we
# catch it and otherwise ignore it
def logException(self):
"""Dumps most recent exception to the log.
"""
log_exc()
@security.public
def createSitemap(self, context, request=None):
"""Returns a sitemap navtree structure.
"""
if request is None:
request = self.REQUEST
return utils.createSiteMap(context, request)
def _addToNavTreeResult(self, result, data):
"""Adds a piece of content to the result tree.
"""
return utils.addToNavTreeResult(result, data)
@security.protected(AccessContentsInformation)
def typesToList(self):
return utils.typesToList(self)
@security.public
def createNavTree(self, context, sitemap=None, request=None):
"""Returns a structure that can be used by navigation_tree_slot.
"""
if request is None:
request = self.REQUEST
return utils.createNavTree(context, request)
@security.public
def createBreadCrumbs(self, context, request=None):
"""Returns a structure for the portal breadcumbs.
"""
if request is None:
request = self.REQUEST
return utils.createBreadCrumbs(context, request)
@security.public
def good_id(self, id):
"""Exposes ObjectManager's bad_id test to skin scripts."""
m = bad_id(id)
if m is not None:
return 0
return 1
@security.public
def bad_chars(self, id):
"""Returns a list of the Bad characters."""
return BAD_CHARS(id)
@security.public
def getInheritedLocalRoles(self, context):
"""Returns a tuple with the acquired local roles."""
portal = getToolByName(context, 'portal_url').getPortalObject()
result = []
cont = 1
if portal != context:
parent = aq_parent(context)
while cont:
if not getattr(parent, 'acl_users', False):
break
userroles = parent.acl_users._getLocalRolesForDisplay(parent)
for user, roles, role_type, name in userroles:
# Find user in result
found = 0
for user2, roles2, type2, name2 in result:
if user2 == user:
# Check which roles must be added to roles2
for role in roles:
if role not in roles2:
roles2.append(role)
found = 1
break
if found == 0:
# Add it to result and make sure roles is a list so
# we may append and not overwrite the loop variable
result.append([user, list(roles), role_type, name])
if parent == portal:
cont = 0
elif not self.isLocalRoleAcquired(parent):
# Role acquired check here
cont = 0
else:
parent = aq_parent(parent)
# Tuplize all inner roles
for pos in range(len(result) - 1, -1, -1):
result[pos][1] = tuple(result[pos][1])
result[pos] = tuple(result[pos])
return tuple(result)
#
# The three methods used in determining what the default-page of a folder
# is. These are:
#
# - getDefaultPage(folder)
# : get id of contentish object that is default-page in the folder
# - isDefaultPage(object)
# : determine if an object is the default-page in its parent folder
# - browserDefault(object)
# : lookup rules for old-style content types
#
@security.public
def isDefaultPage(self, obj, request=None):
"""Finds out if the given obj is the default page in its parent folder.
Uses the lookup rules of Plone. Lookup happens over a view, for which
in theory a different implementation may exist.
"""
if request is None:
request = self.REQUEST
return check_default_page_via_view(obj, request)
@security.public
def getDefaultPage(self, obj, request=None):
"""Given a folderish item, find out if it has a default-page using
the lookup rules of Plone (see Products.CMFPlone/defaultpage.py).
Lookup happens over a view, for which in theory a different
implementation may be used.
"""
if request is None:
if hasattr(self, 'REQUEST'):
request = self.REQUEST
if request:
return get_default_page_via_view(obj, request)
@security.public
def addPortalMessage(self, message, type='info', request=None):
"""\
Call this once or more to add messages to be displayed at the
top of the web page.
Examples:
>>> ptool = self.portal.plone_utils
>>> ptool.addPortalMessage(u'A random warning message', 'warning')
If no type is given it defaults to 'info'
>>> ptool.addPortalMessage(u'A random info message')
The arguments are:
message: a string, with the text message you want to show,
or a HTML fragment (see type='structure' below)
type: optional, defaults to 'info'. The type determines how
the message will be rendered, as it is used to select
the CSS class for the message. Predefined types are:
'info' - for informational messages
'warning' - for warning messages
'error' - for messages about restricted access or
errors.
Portal messages are by default rendered by the global_statusmessage.pt
page template.
It is also possible to add messages from page templates, as
long as they are processed before the portal_message macro is
called by the main template. Example:
<tal:block tal:define="temp python:context.plone_utils.addPortalMessage('A random info message')" /> # noqa
"""
if request is None:
request = self.REQUEST
IStatusMessage(request).add(message, type=type)
@security.public
def showPortalMessages(self, request=None):
"""\
Return portal status messages that will be displayed when the
response web page is rendered. Portal status messages are by default
rendered by the global_statusmessage.pt page template. They will be
removed after they have been shown.
See addPortalMessages for examples.
"""
if request is None:
request = self.REQUEST
return IStatusMessage(request).show()
@security.public
def browserDefault(self, obj):
"""Sets default so we can return whatever we want instead of index_html.
This method is complex, and interacts with mechanisms such as
IBrowserDefault (implemented in CMFDynamicViewFTI), LinguaPlone and
various mechanisms for setting the default page.
The method returns a tuple (obj, [path]) where path is a path to
a template or other object to be acquired and displayed on the object.
The path is determined as follows:
0. If we're c oming from WebDAV, make sure we don't return a contained
object "default page" ever
1. If there is an index_html attribute (either a contained object or
an explicit attribute) on the object, return that as the
"default page". Note that this may be used by things like
File and Image to return the contents of the file, for example,
not just content-space objects created by the user.
2. If the object implements IBrowserDefault, query this for the
default page.
3. If the object has a property default_page set and this gives a list
of, or single, object id, and that object is is found in the
folder or is the name of a skin template, return that id
4. If the property default_page is set in site_properties and that
property contains a list of ids of which one id is found in the
folder, return that id
5. If the object implements IBrowserDefault, try to get the selected
layout.
6. If the type has a 'folderlisting' action and no default page is
set, use this action. This permits folders to have the default
'view' action be 'string:${object_url}/' and hence default to
a default page when clicking the 'view' tab, whilst allowing the
fallback action to be specified TTW in portal_types (this action
is typically hidden)
7. If nothing else is found, fall back on the object's 'view' action.
8. If this is not found, raise an AttributeError
"""
# WebDAV in Zope is odd it takes the incoming verb eg: PROPFIND
# and then requests that object, for example for: /, with verb PROPFIND
# means acquire PROPFIND from the folder and call it
# its all very odd and WebDAV'y
request = getattr(self, 'REQUEST', None)
if request is not None and 'REQUEST_METHOD' in request:
if request['REQUEST_METHOD'] not in ['GET', 'POST']:
return obj, [request['REQUEST_METHOD']]
# Now back to normal
#
# 1. Get an attribute or contained object index_html
#
# Note: The base PloneFolder, as well as ATCT's ATCTOrderedFolder
# defines a method index_html() which returns a ReplaceableWrapper.
# This is needed for WebDAV to work properly, and to avoid implicit
# acquisition of index_html's, which are generally on-object only.
# For the purposes of determining a default page, we don't want to
# use this index_html(), nor the ComputedAttribute which defines it.
if not isinstance(getattr(obj, 'index_html', None),
ReplaceableWrapper):
index_obj = getattr(aq_base(obj), 'index_html', None)
if index_obj is not None \
and not isinstance(index_obj, ComputedAttribute):
return obj, ['index_html']
#
# 2. Look for a default_page managed by an IBrowserDefault-implementing
# object
#
# 3. Look for a default_page property on the object
#
# 4. Try the default sitewide default_page setting
#
if obj.isPrincipiaFolderish:
defaultPage = self.getDefaultPage(obj)
if defaultPage is not None:
if defaultPage in obj:
return obj, [defaultPage]
# Avoid infinite recursion in the case that the page id == the
# object id
elif (
defaultPage != obj.getId()
and defaultPage != '/'.join(obj.getPhysicalPath())
):
# For the default_page property, we may get things in the
# skin layers or with an explicit path - split this path
# to comply with the __browser_default__() spec
return obj, defaultPage.split('/')
# 5. If there is no default page, try IBrowserDefault.getLayout()
if IBrowserDefault.providedBy(obj):
browserDefault = obj
else:
browserDefault = queryAdapter(obj, IBrowserDefault)
if browserDefault is not None:
layout = browserDefault.getLayout()
if layout is None:
raise AttributeError(
"%s has no assigned layout, perhaps it needs an FTI" % obj)
else:
return obj, [layout]
#
# 6. If the object has a 'folderlisting' action, use this
#
# This allows folders to determine in a flexible manner how they are
# displayed when there is no default page, whilst still using
# browserDefault() to show contained objects by default on the 'view'
# action (this applies to old-style folders only, IBrowserDefault is
# managed explicitly above)
if base_hasattr(obj, 'getTypeInfo'):
try:
# XXX: This isn't quite right since it assumes the action
# starts with ${object_url}. Should we raise an error if
# it doesn't?
act = obj.getTypeInfo().getActionInfo(
'folder/folderlisting'
)['url'].split('/')[-1]
return obj, [act]
except ValueError:
pass
#
# 7. Fall back on the 'view' action
#
try:
# XXX: This isn't quite right since it assumes the action
# starts with ${object_url}. Should we raise an error if
# it doesn't?
act = obj.getTypeInfo().getActionInfo(
'object/view'
)['url'].split('/')[-1]
return obj, [act]
except ValueError:
pass
#
# 8. If we can't find this either, raise an exception
#
raise AttributeError(
"Failed to get a default page or view_action for %s"
% (obj.absolute_url(),)
)
@security.public
def isStructuralFolder(self, obj):
"""Checks if a given object is a "structural folder".
That is, a folderish item which does not explicitly implement
INonStructuralFolder to declare that it doesn't wish to be treated
as a folder by the navtree, the tab generation etc.
>>> ptool = self.portal.plone_utils
>>> ptool.isStructuralFolder(self.folder)
True
"""
return (
obj.isPrincipiaFolderish
and not INonStructuralFolder.providedBy(obj)
)
@security.public
def acquireLocalRoles(self, obj, status=1, REQUEST=None):
"""If status is 1, allow acquisition of local roles (regular
behaviour).
If it's 0, prohibit it (it will allow some kind of local role
blacklisting).
"""
mt = getToolByName(self, 'portal_membership')
if not mt.checkPermission(ModifyPortalContent, obj):
raise Unauthorized
# Set local role status...
# set the variable (or unset it if it's defined)
if not status:
obj.__ac_local_roles_block__ = 1
else:
if getattr(obj, '__ac_local_roles_block__', None):
obj.__ac_local_roles_block__ = None
# Reindex the whole stuff.
obj.reindexObjectSecurity()
acquireLocalRoles = postonly(acquireLocalRoles)
@security.public
def isLocalRoleAcquired(self, obj):
"""Returns local role acquisition blocking status.
True if normal, false if blocked.
"""
if getattr(obj, '__ac_local_roles_block__', None):
return False
return True
@security.public
def getOwnerName(self, obj):
""" Returns the userid of the owner of an object.
>>> ptool = self.portal.plone_utils
>>> from plone.app.testing import TEST_USER_ID
>>> ptool.getOwnerName(self.folder) == TEST_USER_ID
True
"""
mt = getToolByName(self, 'portal_membership')
if not mt.checkPermission(View, obj):
raise Unauthorized
return obj.getOwner().getId()
@security.public
def normalizeString(self, text):
"""Normalizes a title to an id.
The relaxed mode was removed in Plone 4.0. You should use either the
url or file name normalizer from the plone.i18n package instead.
normalizeString() converts a whole string to a normalized form that
should be safe to use as in a url, as a css id, etc.
>>> ptool = self.portal.plone_utils
>>> ptool.normalizeString("Foo bar")
'foo-bar'
>>> ptool.normalizeString("Some!_are allowed, others&?:are not")
'some-_are-allowed-others-are-not'
>>> ptool.normalizeString("Some!_are allowed, others&?:are not")
'some-_are-allowed-others-are-not'
all punctuation and spacing is removed and replaced with a '-':
>>> ptool.normalizeString("a string with spaces")
'a-string-with-spaces'
>>> ptool.normalizeString("p.u,n;c(t)u!a@t#i$o%n")
'p-u-n-c-t-u-a-t-i-o-n'
strings are lowercased:
>>> ptool.normalizeString("UppERcaSE")
'uppercase'
punctuation, spaces, etc. are trimmed and multiples are reduced to just
one:
>>> ptool.normalizeString(" a string ")
'a-string'
>>> ptool.normalizeString(">here's another!")
'heres-another'
>>> ptool.normalizeString("one with !@#$!@#$ stuff in the middle")
'one-with-stuff-in-the-middle'
the exception to all this is that if there is something that looks like
a filename with an extension at the end, it will preserve the last
period.
>>> ptool.normalizeString("this is a file.gif")
'this-is-a-file-gif'
>>> ptool.normalizeString("this is. also. a file.html")
'this-is-also-a-file-html'
"""
return utils.normalizeString(text, context=self)
@security.public
def listMetaTags(self, context):
"""Lists meta tags helper.
Creates a mapping of meta tags -> values for the listMetaTags script.
"""
result = {}
mt = getToolByName(self, 'portal_membership')
registry = getUtility(IRegistry)
site_settings = registry.forInterface(ISiteSchema, prefix="plone")
use_all = site_settings.exposeDCMetaTags
security_settings = registry.forInterface(
ISecuritySchema, prefix='plone')
view_about = security_settings.allow_anon_views_about \
or not mt.isAnonymousUser()
if not use_all:
metadata_names = {'Description': METADATA_DCNAME['Description']}
else:
metadata_names = METADATA_DCNAME
for accessor, key in metadata_names.items():
# check non-public properties
if not view_about and accessor in METADATA_DC_AUTHORFIELDS:
continue
# short circuit non-special cases
if not use_all and accessor not in ('Description', 'Subject'):
continue
method = getattr(aq_inner(context).aq_explicit, accessor, None)
if not callable(method):
continue
# Catch AttributeErrors raised by some AT applications
try:
value = method()
except AttributeError:
value = None
if not value:
# No data
continue
if accessor == 'Publisher' and value == 'No publisher':