Commit 61f8f3ef authored by Jérome Perrin's avatar Jérome Perrin

BusinessTemplate: always call onNewObject/onReplaceObject

Use super to always call the overridden method.

Extend the API to pass the business template being installed.

Call the hooks also when installing sub objects, they were not called
in one place.
parent b39b5e7b
...@@ -1156,22 +1156,24 @@ class ObjectTemplateItem(BaseTemplateItem): ...@@ -1156,22 +1156,24 @@ class ObjectTemplateItem(BaseTemplateItem):
""" """
pass pass
def onNewObject(self, obj): def onNewObject(self, obj, context):
""" """
Installation hook. Installation hook.
Called when installation process determined that object to install is Called when installation process determined that object to install is
new on current site (it's not replacing an existing object). new on current site (it's not replacing an existing object).
`obj` parameter is the newly created object in its acquisition context. `obj` parameter is the newly created object in its acquisition context.
`context` is the business template instance, in its acquisition context.
Can be overridden by subclasses. Can be overridden by subclasses.
""" """
pass pass
def onReplaceObject(self, obj): def onReplaceObject(self, obj, context):
""" """
Installation hook. Installation hook.
Called when installation process determined that object to install is Called when installation process determined that object to install is
to replace an existing object on current site (it's not new). to replace an existing object on current site (it's not new).
`obj` parameter is the replaced object in its acquisition context. `obj` parameter is the replaced object in its acquisition context.
`context` is the business template instance, in its acquisition context.
Can be overridden by subclasses. Can be overridden by subclasses.
""" """
pass pass
...@@ -1416,9 +1418,9 @@ class ObjectTemplateItem(BaseTemplateItem): ...@@ -1416,9 +1418,9 @@ class ObjectTemplateItem(BaseTemplateItem):
if not object_existed: if not object_existed:
# A new object was added, call the hook # A new object was added, call the hook
self.onNewObject(obj) self.onNewObject(obj, context)
else: else:
self.onReplaceObject(obj) self.onReplaceObject(obj, context)
# mark a business template installation so in 'PortalType_afterClone' scripts # mark a business template installation so in 'PortalType_afterClone' scripts
# we can implement logical for reseting or not attributes (i.e reference). # we can implement logical for reseting or not attributes (i.e reference).
...@@ -1972,9 +1974,11 @@ class CategoryTemplateItem(ObjectTemplateItem): ...@@ -1972,9 +1974,11 @@ class CategoryTemplateItem(ObjectTemplateItem):
def beforeInstall(self): def beforeInstall(self):
self._installed_new_category = False self._installed_new_category = False
return super(CategoryTemplateItem, self).beforeInstall()
def onNewObject(self, obj): def onNewObject(self, obj, context):
self._installed_new_category = True self._installed_new_category = True
return super(CategoryTemplateItem, self).onNewObject(obj, context)
def afterInstall(self): def afterInstall(self):
if self._installed_new_category: if self._installed_new_category:
...@@ -2392,7 +2396,8 @@ class WorkflowTemplateItem(ObjectTemplateItem): ...@@ -2392,7 +2396,8 @@ class WorkflowTemplateItem(ObjectTemplateItem):
continue continue
raise raise
container_ids = container.objectIds() container_ids = container.objectIds()
if object_id in container_ids: # Object already exists object_existed = object_id in container_ids
if object_existed:
self._backupObject(action, trashbin, container_path, object_id, keep_subobjects=1) self._backupObject(action, trashbin, container_path, object_id, keep_subobjects=1)
container.manage_delObjects([object_id]) container.manage_delObjects([object_id])
obj = self._objects[path] obj = self._objects[path]
...@@ -2402,6 +2407,11 @@ class WorkflowTemplateItem(ObjectTemplateItem): ...@@ -2402,6 +2407,11 @@ class WorkflowTemplateItem(ObjectTemplateItem):
obj = container._getOb(object_id) obj = container._getOb(object_id)
obj.manage_afterClone(obj) obj.manage_afterClone(obj)
obj.wl_clearLocks() obj.wl_clearLocks()
if not object_existed:
# A new object was added, call the hook
self.onNewObject(obj, context)
else:
self.onReplaceObject(obj, context)
def uninstall(self, context, **kw): def uninstall(self, context, **kw):
object_path = kw.get('object_path', None) object_path = kw.get('object_path', None)
...@@ -4218,7 +4228,7 @@ class _ZodbComponentTemplateItem(ObjectTemplateItem): ...@@ -4218,7 +4228,7 @@ class _ZodbComponentTemplateItem(ObjectTemplateItem):
raise NotImplementedError raise NotImplementedError
def __init__(self, id_list, tool_id='portal_components', **kw): def __init__(self, id_list, tool_id='portal_components', **kw):
ObjectTemplateItem.__init__(self, id_list, tool_id=tool_id, **kw) super(_ZodbComponentTemplateItem, self).__init__(id_list, tool_id=tool_id, **kw)
def isKeepWorkflowObjectLastHistoryOnly(self, path): def isKeepWorkflowObjectLastHistoryOnly(self, path):
""" """
...@@ -4248,9 +4258,13 @@ class _ZodbComponentTemplateItem(ObjectTemplateItem): ...@@ -4248,9 +4258,13 @@ class _ZodbComponentTemplateItem(ObjectTemplateItem):
obj.workflow_history[wf_id] = WorkflowHistoryList([wf_history]) obj.workflow_history[wf_id] = WorkflowHistoryList([wf_history])
def onNewObject(self, _): def onNewObject(self, obj, context):
self._do_reset = True
return super(_ZodbComponentTemplateItem, self).onNewObject(obj, context)
def onReplaceObject(self, obj, context):
self._do_reset = True self._do_reset = True
onReplaceObject = onNewObject return super(_ZodbComponentTemplateItem, self).onReplaceObject(obj, context)
def afterInstall(self): def afterInstall(self):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment