plone / plone.api

The Plone API
https://6.docs.plone.org/plone.api
Other
89 stars 54 forks source link

api.portal.get_tool fails to find portal when renaming a Plone site #285

Open ale-rt opened 8 years ago

ale-rt commented 8 years ago

This is the traceback I got:

2015-11-25 17:43:36 ERROR Zope.SiteErrorLog 1448469816.880.658912754901 http://localhost:9000/manage_renameObjects
Traceback (innermost last):
  Module ZPublisher.Publish, line 138, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 48, in call_object
  Module OFS.CopySupport, line 333, in manage_renameObjects
  Module OFS.CopySupport, line 392, in manage_renameObject
  Module zope.event, line 31, in notify
  Module zope.component.event, line 24, in dispatch
  Module zope.component._api, line 136, in subscribers
  Module zope.component.registry, line 321, in subscribers
  Module zope.interface.adapter, line 585, in subscribers
  Module zope.component.event, line 32, in objectEventNotify
  Module zope.component._api, line 136, in subscribers
  Module zope.component.registry, line 321, in subscribers
  Module zope.interface.adapter, line 585, in subscribers
  Module OFS.subscribers, line 113, in dispatchObjectMovedEvent
  Module zope.container.contained, line 153, in dispatchToSublocations
  Module zope.component._api, line 136, in subscribers
  Module zope.component.registry, line 321, in subscribers
  Module zope.interface.adapter, line 585, in subscribers
  Module OFS.subscribers, line 113, in dispatchObjectMovedEvent
  Module zope.container.contained, line 153, in dispatchToSublocations
  Module zope.component._api, line 136, in subscribers
  Module zope.component.registry, line 321, in subscribers
  Module zope.interface.adapter, line 585, in subscribers
  Module OFS.subscribers, line 113, in dispatchObjectMovedEvent
  Module zope.container.contained, line 153, in dispatchToSublocations
  Module zope.component._api, line 136, in subscribers
  Module zope.component.registry, line 321, in subscribers
  Module zope.interface.adapter, line 585, in subscribers
  Module OFS.subscribers, line 113, in dispatchObjectMovedEvent
  Module zope.container.contained, line 153, in dispatchToSublocations
  Module zope.component._api, line 136, in subscribers
  Module zope.component.registry, line 321, in subscribers
  Module zope.interface.adapter, line 585, in subscribers
  Module OFS.subscribers, line 113, in dispatchObjectMovedEvent
  Module zope.container.contained, line 153, in dispatchToSublocations
  Module zope.component._api, line 136, in subscribers
  Module zope.component.registry, line 321, in subscribers
  Module zope.interface.adapter, line 585, in subscribers
  Module Products.CMFCore.CMFCatalogAware, line 267, in handleContentishEvent
  Module Products.Archetypes.CatalogMultiplex, line 39, in indexObject
  Module Products.CMFPlone.CatalogTool, line 353, in catalog_object
  Module Products.ZCatalog.ZCatalog, line 476, in catalog_object
  Module Products.ZCatalog.Catalog, line 334, in catalogObject
  Module Products.ZCatalog.Catalog, line 284, in updateMetadata
  Module Products.ZCatalog.Catalog, line 410, in recordify
  Module Products.Ploneboard.content.PloneboardConversation, line 203, in getLastCommentAuthor
  Module Products.Ploneboard.content.PloneboardConversation, line 227, in getLastComment
  Module Products.Ploneboard.content.PloneboardConversation, line 73, in _get_catalog
  Module plone.api.portal, line 2, in get_tool
  Module plone.api.validation, line 70, in wrapped
  Module plone.api.portal, line 101, in get_tool
  Module plone.api.portal, line 65, in get
CannotGetPortalError: Unable to get the portal object. More info on https://ploneapi.readthedocs.org/en/latest/api/exceptions.html#plone.api.exc.CannotGetPortalError

The line calling plone.api is this one:

ale-rt commented 8 years ago

Related to #20. I do not know a reasonable way to fix this, but this worries me: renaming Plone sites from the ZMI should work. In addition I am wondering that renaming a Plone site contained in another Plone site will lead to an error because api.portal.get will return the "wrong" portal...

With the wide adoption of plone.api I suspect that the renaming of Plone sites will be impossible :)

ale-rt commented 8 years ago

A good thing is that the global request is still available.

With this patch to plone.api I was able to rename the Plone site:

diff --git a/src/plone/api/portal.py b/src/plone/api/portal.py
index dbce214..99bd78d 100644
--- a/src/plone/api/portal.py
+++ b/src/plone/api/portal.py
@@ -60,6 +60,11 @@ def get():
         for potential_portal in closest_site.aq_chain:
             if ISiteRoot in providedBy(potential_portal):
                 return potential_portal
+    else:
+        from zope.globalrequest import getRequest
+        request = getRequest()
+        for new_id in request.form['new_ids']:
+            return request.traverse('').aq_parent[new_id]

     raise CannotGetPortalError(
         "Unable to get the portal object. More info on "

Could we work around this adding a form on the Plone site that has to be removed?

fulv commented 8 years ago

AFAIK, renaming a plone site has never been supported. I could look up references to back up my claim, but I'm pretty sure.

On Wed, Nov 25, 2015 at 9:29 AM Alessandro Pisa notifications@github.com wrote:

A good thing is that the global request is still available.

With this patch to plone.api I was able to rename the Plone site:

diff --git a/src/plone/api/portal.py b/src/plone/api/portal.py index dbce214..99bd78d 100644--- a/src/plone/api/portal.py+++ b/src/plone/api/portal.py@@ -60,6 +60,11 @@ def get(): for potential_portal in closest_site.aq_chain: if ISiteRoot in providedBy(potential_portal): return potential_portal+ else:+ from zope.globalrequest import getRequest+ request = getRequest()+ for new_id in request.form['new_ids']:+ return request.traverse('').aq_parent[new_id]

 raise CannotGetPortalError(
     "Unable to get the portal object. More info on "

Could we work around this adding a form on the Plone site that has to be removed?

— Reply to this email directly or view it on GitHub https://github.com/plone/plone.api/issues/285#issuecomment-159681796.

davilima6 commented 8 years ago

iirc Although not recommended, it would work most of the times (eg: if there weren't too many or too complex addons). You'd "only" lose relations between items, even if clearing & rebuilding catalog. Not sure about collections/portlets paths.

dmunicio commented 6 years ago

With proposed fix, I am getting an error when deleting plone site. This patch fixes both problems:

diff --git a/src/plone/api/portal.py b/src/plone/api/portal.py
index dbce214..99bd78d 100644
--- a/src/plone/api/portal.py
+++ b/src/plone/api/portal.py
@@ -60,6 +60,11 @@ def get():
         for potential_portal in closest_site.aq_chain:
             if ISiteRoot in providedBy(potential_portal):
                 return potential_portal
+    else:
+        from zope.globalrequest import getRequest
+        request = getRequest()
+        if 'new_ids' in request.form:
+            for new_id in request.form['new_ids']:
+                return request.traverse('').aq_parent[new_id]
+        if 'ids' in request.form:
+            for id in request.form['ids']:
+                return None
+

     raise CannotGetPortalError(
         "Unable to get the portal object. More info on "