diff --git a/news/1078.bugfix b/news/1078.bugfix new file mode 100644 index 0000000000..57b4ccf78f --- /dev/null +++ b/news/1078.bugfix @@ -0,0 +1 @@ +Better handling for IQuerySource and un-named vocabularies. \ No newline at end of file diff --git a/src/plone/restapi/services/querysources/get.py b/src/plone/restapi/services/querysources/get.py index 47d7c513fa..4daeb6af4e 100644 --- a/src/plone/restapi/services/querysources/get.py +++ b/src/plone/restapi/services/querysources/get.py @@ -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 diff --git a/src/plone/restapi/types/adapters.py b/src/plone/restapi/types/adapters.py index 2998ca7de5..d02c63e307 100644 --- a/src/plone/restapi/types/adapters.py +++ b/src/plone/restapi/types/adapters.py @@ -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 @@ -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):