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

RelationChoice field leads to ValueError in principals #59

Closed
mtrebron opened this issue Jul 2, 2019 · 9 comments · May be fixed by plone/plone.app.dexterity#311
Closed

RelationChoice field leads to ValueError in principals #59

mtrebron opened this issue Jul 2, 2019 · 9 comments · May be fixed by plone/plone.app.dexterity#311

Comments

@mtrebron
Copy link

mtrebron commented Jul 2, 2019

Dexterity types which include a RelationChoice field e.g.

    <field name="teaser_image" type="z3c.relationfield.schema.RelationChoice">
      <description/>
      <required>False</required>
      <title>Teaser Image</title>
      <portal_type>
        <element>Image</element>
      </portal_type>
    </field>

lead to the following error in Plone 5.2 RC5

Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 155, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 337, in publish_module
  Module ZPublisher.WSGIPublisher, line 243, in publish
  Module ZPublisher.BaseRequest, line 523, in traverse
  Module ZPublisher.BaseRequest, line 330, in traverseName
  Module zope.traversing.namespace, line 165, in namespaceLookup
  Module plone.z3cform.traversal, line 54, in traverse
  Module plone.dexterity.browser.add, line 141, in update
  Module plone.z3cform.fieldsets.extensible, line 65, in update
  Module plone.z3cform.patch, line 30, in GroupForm_update
  Module z3c.form.group, line 141, in update
  Module z3c.form.group, line 52, in update
  Module z3c.form.group, line 48, in updateWidgets
  Module z3c.form.field, line 277, in update
  Module plone.app.z3cform.widget, line 443, in update
  Module z3c.form.browser.text, line 36, in update
  Module z3c.form.browser.widget, line 171, in update
  Module Products.CMFPlone.patches.z3c_form, line 47, in _wrapped
  Module z3c.form.widget, line 132, in update
  Module plone.app.z3cform.converters, line 181, in toWidgetValue
  Module plone.app.vocabularies.principals, line 147, in getTerm
  Module plone.app.vocabularies.principals, line 113, in _get_term_from_source
ValueError: value or token must be provided (only one of those)
@cekk
Copy link
Member

cekk commented Jul 8, 2019

I have the same problem with a custom field that is using CatalogSource as source.

What i don't understand, is why the view called by the widget tries to get the term of principals vocabulary.

@mtrebron
Copy link
Author

mtrebron commented Jul 9, 2019

What i don't understand, is why the view called by the widget tries to get the term of principals vocabulary.

Because of this:

 [plone.app.z3cform.converters:180][waitress] <AjaxSelectWidget 'form.widgets.IDublinCore.creators'>

Maybe this issue belongs somewhere else: plone.app.z3cform or CMFPlone?

Edit, there seem to be two things at play here.

  1. this check https://github.com/plone/plone.app.z3cform/blob/master/plone/app/z3cform/converters.py#L174 does not catch (None, ) which is the value passed to the converter.

So, when returning missing_value above and

  1. adding plone.app.vocabularies.Principals here
    https://github.com/plone/plone.app.content/blob/master/plone/app/content/browser/vocabulary.py#L40

The widget appears to be "working" - I can now navigate back to the LRF
However, I probably still have to manually add a path to the LIF folder 'Assets' to the widget Favorites in order to select my image...

Edit/update: an image in the LRF can now be found correctly

@petschki
Copy link
Member

petschki commented Nov 18, 2019

Looking at the RelatedItems behavior (https://github.com/plone/plone.app.relationfield/blob/master/plone/app/relationfield/behavior.py#L26) there is the widget directive with the vocabulary defined. So something like this should do the trick (untested!)

<field name="teaser_image" type="z3c.relationfield.schema.RelationChoice">
  <description/>
  <required>False</required>
  <title>Teaser Image</title>
  <form:widget type="plone.app.z3cform.widget.RelatedItemsFieldWidget">
      <vocabulary>plone.app.vocabularies.Catalog</vocabulary>
  </form:widget>
</field>

NOTE: the pattern_options parameter can define a selectableTypes list ... but I'm not shure how to define this in the XML schema ...

and remember to add xmlns:form="http://namespaces.plone.org/supermodel/form" to the prefixes...

@mtrebron
Copy link
Author

mtrebron commented Nov 20, 2019

The problem lies with the toWidgetValue converter not recognizing (None, ) as None and the lack of permissions on plone.app.vocabularies.Principals. The latter is required by a query from the form.widgets.IDublinCore.creators widget- unclear why this is done...

@adrianschulz
Copy link

Any updates here? Is somebody working on this one?

fulv added a commit to plone/plone.app.dexterity that referenced this issue Aug 6, 2020
This would fix plone/plone.app.vocabularies#59, but I'm only floating this change as an idea, hoping for some feedback.

`z3c.form.widget.Widget.update()` passes the result of `creatorsDefault` (which is called by an `IValue` adapter's `get()` method) to `plone.app.z3cform.converters.AjaxSelectWidgetConverter.toWidgetValue()`.

`getSecurityManager().getUser()` returns a regular `<PropertiedUser>` for most requests and thus the return value is a tuple, e.g. `('admin',)`.

However, for ajax requests, e.g. `/++add++MyContentType/++widget++form.widgets.IMyBehavior.behavior_field`, the user is anonymous:

```
(Pdb) user
<SpecialUser 'Anonymous User'>
(Pdb) pp user.__dict__
{'__': '', 'domains': [], 'name': 'Anonymous User', 'roles': ('Anonymous',)}
```

Thus, the return value is the tuple `(None,)`.  This breaks `AjaxSelectWidgets` when the content type includes the Dublin Core behavior or just the ownership field, even though the field itself is a `RelationList` with a `value_type=RelationChoice( source=CatalogSource(portal_type='MyContentType) )`.  In other words, even though the broken field has no need for the item's creator, the creator gets calculated anyway when the request is handled.

The problem with the tuple `(None,)` is that `toWidgetValue()` does not know how to deal with it and so it returns `None` instead of `field.missing_value` as it should.

This could be fixed in at least four different places:

1. plone.app.dexterity.behaviors.metadata.creatorsDefault
2. plone.app.z3cform.converters.AjaxSelectWidgetConverter.toWidgetValue
3. z3c.form.widget.Widget.update
4. have getSecurityManager().getUser() return the logged in user even on Ajax requests.

1. is the origin of the unexpected value, however, I don't know if making it return `None` for anonymous users would make any other code mad.
2. is the place that does not expect a `(None,)` tuple instead of `None`, so this could be fixed by simply adding a check for `(None,)`.
3. is the "middleman" between 1 and 2, so it could convert `(None,)` to `None` before passing it on.
4. I have no idea what this would entail.
@fulv
Copy link
Member

fulv commented Aug 6, 2020

By the way, I don't see the need to add a permission on plone.app.vocabularies.Principals, @mtrebron .
It works without for me.

@mtrebron
Copy link
Author

mtrebron commented Aug 8, 2020

@fulv correct, that permission is not necessary (anymore?).

@NicolasGoeddel
Copy link

Is this the same issue as this one? plone/plone.app.z3cform#121

@mtrebron
Copy link
Author

Yes, good catch. Will close as fixed by plone/plone.app.z3cform#122

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants