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

UnboundLocalError: local variable 'data' referenced before assignment #45

Open
gforcada opened this issue Oct 20, 2020 · 2 comments
Open

Comments

@gforcada
Copy link
Sponsor Member

See traceback

https://www.freitag.de/autoren/XXX/@@edit/++widget++form.widgets.portrait/@@download/2014-07-05+%2865%29.JPG
Traceback (innermost last):
  Module ZServer.ZPublisher.Publish, line 144, in publish
  Module ZPublisher.mapply, line 85, in mapply
  Module ZServer.ZPublisher.Publish, line 44, in call_object
  Module plone.formwidget.namedfile.widget, line 409, in __call__
  Module plone.formwidget.namedfile.widget, line 72, in _make_namedfile
UnboundLocalError: local variable 'data' referenced before assignment

Our sentry server reports that the value variable is None, as the URL shows, it is on an edit form, so the widget does not have any image? 🤔 but as one can see on the code, before line 72 there is an if... elif clause that has no fallback value for value variable.

@Pernath
Copy link

Pernath commented Apr 19, 2022

What can we do to avoid or patch the bug without messing with the widget.py code? My team has been facing this issue while using NamedFileField inside subforms.

@Pernath
Copy link

Pernath commented Apr 26, 2022

What can we do to avoid or patch the bug without messing with the widget.py code? My team has been facing this issue while using NamedFileField inside subforms.

After a session of several hours of debugging, we found the culprit in this scenario. Turns out, the NamedFileWidget is only prepared to remember one file at a time in the loaded form, so in actuality, we were abusing Plone's subform capability with a field not intended to be used in it (that's our wild guess). Specially because said NamedFile field, was an attribute of an object subform that we chose as the value_type of a schema.List field.

We found a very problematic line in widget.py (291):

del upload_map[file_upload_id]

That cleaning of the temporary storage, wouldn't allow us to make multiple uploads of files for each element of the List field.

As a workaround, we made a custom widget class inheriting the NamedFileWidget and a slight modification to the extract method to avoid this deletion. After the successfull retrieval of the multiple NamedFile fields values, and the creation of the element of our problematic content type, we then proceeded to clean the multiple file upload ids, recollected during the widgets' update function calls.

gforcada added a commit that referenced this issue Nov 30, 2022
Otherwise the `if...elif` ends and `data` is not defined.

See #45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants