Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify RestTraverser code. #89

Merged
merged 1 commit into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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