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

Better support for IQuerySource vocabularies #1078

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions news/1078.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Better handling for IQuerySource and un-named vocabularies.
5 changes: 5 additions & 0 deletions src/plone/restapi/services/querysources/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ def reply(self):
bound_field = field.bind(self.context)

source = bound_field.source
if not source and IQuerySource.providedBy(
getattr(bound_field, "vocabulary", None)
):
source = bound_field.vocabulary

if not IQuerySource.providedBy(source):
return self._error(
404, "Not Found", "Field %r does not have an IQuerySource" % fieldname
Expand Down
20 changes: 16 additions & 4 deletions src/plone/restapi/types/adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from plone.restapi.types.utils import get_widget_params
from plone.schema import IEmail
from plone.schema import IJSONField
import six
from z3c.formwidget.query.interfaces import IQuerySource
from zope.component import adapter
from zope.component import getMultiAdapter
Expand Down Expand Up @@ -106,11 +107,22 @@ def get_widget(self):
def get_widget_params(self):
all_params = get_widget_params([self.field.interface])
params = all_params.get(self.field.getName(), {})

if "vocabulary" in params:
vocab_name = params["vocabulary"]
params["vocabulary"] = {
"@id": get_vocabulary_url(vocab_name, self.context, self.request)
}
vocab = params["vocabulary"]
if isinstance(vocab, six.text_type):
params["vocabulary"] = {
"@id": get_vocabulary_url(vocab, self.context, self.request)
}
elif IQuerySource.providedBy(vocab):
params["vocabulary"] = {
"@id": get_querysource_url(self.field, self.context, self.request)
}
else:
params["vocabulary"] = {
"@id": get_source_url(self.field, self.context, self.request)
}

return params

def get_multilingual_directives(self):
Expand Down