Skip to content

Commit

Permalink
Clarify RestTraverser code. (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauritsvanrees authored Jun 24, 2024
1 parent c0a73b0 commit 9145dd0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions news/+traverser.internal
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Clarify RestTraverser code.
[maurits]
30 changes: 20 additions & 10 deletions src/plone/distribution/browser/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from plone.protect.interfaces import IDisableCSRFProtection
from Products.CMFPlone.browser.admin import AddPloneSite as AddPloneSiteView
from Products.CMFPlone.browser.admin import AppTraverser
from zope.component import adapts
from zExceptions import NotFound
from zope.component import adapter
from zope.component import queryMultiAdapter
from zope.interface import alsoProvides
from zope.publisher.browser import BrowserView
Expand All @@ -14,25 +15,34 @@
import json


@adapter(IApplication, IRequest)
class RestTraverser(AppTraverser):
"""Traverser supporting REST calls in Zope Application root."""

adapts(IApplication, IRequest)

def publishTraverse(self, request, name):
if not name.startswith("@"):
return super().publishTraverse(request, name)

# This will fail with an AttributeError unless the url has ++api++,
# otherwise the request has no _rest_service_id.
# Result is a NotFound error.
service = queryMultiAdapter(
(self.context, request), name=request._rest_service_id + name
)
if service is None:
# No service, fallback to regular view
view = queryMultiAdapter((self.context, request), name=name)
if view is not None:
return view
raise
return service
if service is not None:
return service
# No service, fallback to regular view.
# But this is unlikely to happen, unless you visit a url like
# http://localhost:8082/++api++/@ok instead of
# http://localhost:8082/@@ok
# Note that a request for http://localhost:8082/++api++/@@ok
# does not even come in this traverser.
# Anyway, strip off the '@' sign.
name = name.lstrip("@")
view = queryMultiAdapter((self.context, request), name=name)
if view is not None:
return view
raise NotFound(self.context)


class Overview(BrowserView):
Expand Down

0 comments on commit 9145dd0

Please sign in to comment.