notifyMemberAreaCreated.py
An example of adding default contents to a member area
Size
2.2 kB
-
File type
text/x-python
File contents
## Script (Python) "notifyMemberAreaCreated"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=Modify new member area
##
# When a Member is added, it is done with the rights of an Anonymous
# user. To add content to the folder, the anonymous user must
# temporarily be granted permissions to add these content types.
#
# Basically you need to add permissions to add all the content types you
# want copied from the default member folder.
temp_permissions = [
'Add Documents, Images, and Files', 'Add portal content',
'Delete objects'
]
# this is the plone folder that holds the default member content
default_member_content = getattr(container, 'default_member_content', None)
# Somebody might have deleted the default folder with the default member content.
# Don't crash the site because ot that.
if default_member_content:
anonymous_role = ['Anonymous']
for perm in temp_permissions:
context.manage_permission(perm, anonymous_role, acquire=1)
# if the default_member_content folder has an index html we want
# to overwrite the default one. So it needs to be deleted
idx = 'index_html'
member_has_idx = idx in context.objectIds()
default_has_idx = idx in default_member_content.objectIds()
if default_has_idx and member_has_idx:
context.manage_delObjects(idx)
# here we do the copying
copy_ids = default_member_content.objectIds()
id_filter = ['.personal']
copy_ids = [id for id in copy_ids if id not in id_filter]
objects = default_member_content.manage_copyObjects(copy_ids)
context.manage_pasteObjects(objects)
# for some reason the copied index_html will get the id
# 'copy_of_index_html' even though there shouldn't be one, so we
# need to rename it back to index_html
copy_idx = 'copy_of_index_html'
if copy_idx in context.objectIds():
context.manage_renameObject(copy_idx, idx)
# Here we delete those temporary permissions again
for perm in temp_permissions:
context.manage_permission(perm, [], acquire=1)
context.manage_fixupOwnershipAfterAdd()