zopyx
(Andreas Jung)
October 25, 2016, 10:54am
1
I have a folderish content-type that accepts other 'Folder' objects as subjects. I need to attach folder restrictions to newly created Folders . In side a handler for IObjectCreatedEvent I can get hold of the related folder instance however the folder instance is not acquisition-wrapped or not finally constructed in order get hold of the parent for performing some further checks. Is there a different way hooking into the Dexterity content-type creation cycle?
-aj
jaroel
(Roel Bruggink)
October 25, 2016, 3:30pm
2
How about def _setObject
in the container class?
if 'permission' in meta_type:
if sm.checkPermission(meta_type['permission'], self):
meta_types.append(meta_type)
else:
meta_types.append(meta_type)
return meta_types
_checkId = checkValidId
def _setOb(self, id, object):
setattr(self, id, object)
def _delOb(self, id):
delattr(self, id)
def _getOb(self, id, default=_marker):
# FIXME: what we really need to do here is ensure that only
# sub-items are returned. That could have a measurable hit
# on performance as things are currently implemented, so for
# the moment we just make sure not to expose private attrs.
if id[:1] != '_' and hasattr(aq_base(self), id):
Dexterity's addContentToContainer (and createContentInContainer) call container._setObject
-Roel
jaroel
(Roel Bruggink)
October 25, 2016, 3:49pm
3
Or, use IObjectAddedEvent which should have event.newParent
Then obj should be aq wrapped, or you can do it yourself with _of _:
def handle(obj, event):
aq_wrapped = obj.__of__(event.newParent)
zopyx
(Andreas Jung)
October 25, 2016, 4:22pm
4
seanupton
(Sean Upton)
October 25, 2016, 5:34pm
5
In corner cases where I have been unable to use IObjectAddedEvent, IIRC, I have resorted in IObjectCreatedEvent subscribers to obtaining the parent folder from the request object, obtained via zope.globalrequest.getRequest(). This is not pretty; it is a last resort for peculiar situations.
Sean
jaroel
(Roel Bruggink)
October 25, 2016, 6:09pm
6
Yep; the object hasn't been added to the tree when ObjectCreated fires. Therefor no acquisition yet.
):
form_schema = IFormFieldProvider(schema_interface, None)
if form_schema is not None:
yield form_schema
else:
log.debug("Behavior assignable found for context.")
for behavior_reg in behavior_assignable.enumerateBehaviors():
form_schema = IFormFieldProvider(behavior_reg.interface, None)
if form_schema is not None:
yield form_schema
def createContent(portal_type, **kw):
fti = getUtility(IDexterityFTI, name=portal_type)
content = createObject(fti.factory)
# Note: The factory may have done this already, but we want to be sure
# that the created type has the right portal type. It is possible
# to re-define a type through the web that uses the factory from an
# existing type, but wants a unique portal_type!
content.portal_type = fti.getId()
zopyx
(Andreas Jung)
October 26, 2016, 3:34pm
7
jaroel:
Or, use IObjectAddedEvent which should have event.newParent
Then obj should be aq wrapped, or you can do it yourself with of :
def handle(obj, event):
aq_wrapped = obj.of (event.newParent)
IObjectAddedEvent did the job.
Tnx
-aj