NamedFile to NamedBlobFile using transaction to migrate

Good morning, Plone community!

I am in the process of trying to migrate an archetypes FileField over to the dexterity NamedBlobFile. The file transfers successfully, however, i am running into a problem getting the view to display. This is the error I have:

2021-01-08 09:35:28 ERROR Zope.SiteErrorLog 1610116528.090.600301540508 http://localhost:8080/Plone/directory/brown-rhenne-1/first-reappointment/@@edit
Traceback (innermost last):
  Module ZPublisher.Publish, line 138, in publish
  Module ZPublisher.mapply, line 77, in mapply
  Module ZPublisher.Publish, line 48, in call_object
  Module plone.z3cform.layout, line 66, in __call__
  Module plone.z3cform.layout, line 60, in update
  Module z3c.form.form, line 162, in render
  Module zope.browserpage.viewpagetemplatefile, line 51, in __call__
  Module zope.pagetemplate.pagetemplate, line 132, in pt_render
  Module five.pt.engine, line 98, in __call__
  Module z3c.pt.pagetemplate, line 163, in render
  Module chameleon.zpt.template, line 261, in render
  Module chameleon.template, line 171, in render
  Module 15e07ebfb81b5b5346dba864a5a9f1fb.py, line 91, in render
  Module dcd9a3254e94bbffbbb179068b6be223.py, line 1806, in render_titlelessform
  Module dcd9a3254e94bbffbbb179068b6be223.py, line 451, in render_fields
  Module dcd9a3254e94bbffbbb179068b6be223.py, line 126, in render_widget_rendering
  Module dcd9a3254e94bbffbbb179068b6be223.py, line 1069, in render_field
  Module five.pt.expressions, line 161, in __call__
  Module Products.Five.browser.metaconfigure, line 485, in __call__
  Module zope.browserpage.viewpagetemplatefile, line 83, in __call__
  Module zope.browserpage.viewpagetemplatefile, line 51, in __call__
  Module zope.pagetemplate.pagetemplate, line 132, in pt_render
  Module five.pt.engine, line 98, in __call__
  Module z3c.pt.pagetemplate, line 163, in render
  Module chameleon.zpt.template, line 261, in render
  Module chameleon.template, line 171, in render
  Module 0f44a7146c45304890ccb281d5b3eed2.py, line 591, in render
  Module 0f44a7146c45304890ccb281d5b3eed2.py, line 466, in render_widget_wrapper
  Module five.pt.expressions, line 161, in __call__
  Module z3c.form.widget, line 154, in render
  Module zope.browserpage.viewpagetemplatefile, line 51, in __call__
  Module zope.pagetemplate.pagetemplate, line 132, in pt_render
  Module five.pt.engine, line 98, in __call__
  Module z3c.pt.pagetemplate, line 163, in render
  Module chameleon.zpt.template, line 261, in render
  Module chameleon.template, line 191, in render
  Module chameleon.template, line 171, in render
  Module e395f21f55ac39c8135d64d89f83519e.py, line 142, in render
  Module five.pt.expressions, line 154, in __call__
  Module five.pt.expressions, line 126, in traverse
  Module zope.traversing.adapters, line 136, in traversePathElement
   - __traceback_info__: (<NamedFileWidget 'form.widgets.toPresident'>, 'file_content_type')
  Module zope.traversing.adapters, line 50, in traverse
   - __traceback_info__: (<NamedFileWidget 'form.widgets.toPresident'>, 'file_content_type', ())
LocationError: (<NamedFileWidget 'form.widgets.toPresident'>, 'file_content_type')

 - Expression: "widget/@@ploneform-render-widget"
 - Filename:   ... rm-1.2.3-py2.7.egg/plone/app/z3cform/templates/macros.pt
 - Location:   (line 98: col 81)
 - Source:     ... place="structure widget/@@ploneform-render-widget"/>
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 - Expression: "view/file_content_type"
 - Filename:   ... .0.15-py2.7.egg/plone/formwidget/namedfile/file_input.pt
 - Location:   (line 28: col 21)
 - Source:     doc_type view/file_content_type;
                        ^^^^^^^^^^^^^^^^^^^^^^
 - Arguments:  repeat: {...} (0)
               context: <ImplicitAcquisitionWrapper first-reappointment at 0x7f9455b23370>
               exists: True
               views: <ViewMapper - at 0x7f9439543690>
               modules: <TraversableModuleImporter - at 0x7f944f51a9d0>
               args: <tuple - at 0x7f9449500050>
               nothing: <NoneType - at 0x101486e48>
               target_language: <NoneType - at 0x101486e48>
               allow_nochange: True
               default: <object - at 0x7f944f00e1c0>
               request: <instance - at 0x7f9455655d20>
               wrapped_repeat: {...} (0)
               download_url: http://localhost:8080/Plone/directory/brown-rhenne...
               loop: {...} (0)
               template: <ViewPageTemplateFile - at 0x7f9454827850>
               action: nochange
               translate: <function translate at 0x7f9458afded0>
               options: {...} (0)
               view: <NamedFileWidget toPresident at 0x7f943954f910>

I was using transaction in a Plone python view with the following code to migrate the content:

                    faculties = portal_catalog(portal_type="pbAction")
                    for pbAction in faculties:
                        object = pbAction.getObject()
                        print(pbAction.Title)
                        print(facultyobj)
                        transaction.begin()
                        pbActionobj = api.content.create(
                            container=facultyobj,
                            type='pbaction2',
                            id=object.id,
                            appealLetter=object.appealLetter,
                            appealOther=object.appealOther,
                            YearsOfService=object.YearsOfService,
                            managers=object.managers,
                            deptVotesYes=object.deptVotesYes,
                            deptVotesAbstain=object.deptVotesAbstain,
                            deptVotesNo=object.deptVotesNo,
                            collegeVotesYes=object.collegeVotesYes,
                            collegeVotesAbstain=object.collegeVotesAbstain,
                            collegeVotesNo=object.collegeVotesNo,
                            # title=object.title,
                            # description=object.description,
                            moreDocs=object.moreDocs,
                            toPresident=object.toPresident,
                            chairMemo=object.chairMemo,
                            classroomObservations1=object.classroomObservations1,
                            classroomObservations2=object.classroomObservations2,
                            studentEvaluation1=object.studentEvaluation1,
                            studentEvaluation2=object.studentEvaluation2,
                            resume=object.resume,
                            safe_id=True
                        )
                        pbActionobj.manage_setLocalRoles('rnunez', ['Editor'])
                        transaction.commit()
                        print('pbAction Migration Succeeded')

I thank you kindly in advance for your help.

rbrown12

Looks to me like toPresident is not a NamedBlobFile.

If it is already a blob based file it could be enough to fetch the raw blob (not sure how, IIRC there is something like .getBlob on the Archetypes Field).

If it is not already a blob, I would create a new NamedBlobFile and fill it with the data from the old Archetypes field:

from plone.namedfile.file import NamedBlobFile
toPresident = NamedBlobFile(oldrawdata, contentType=oldcontenttype, filename=oldfilename)

I am not 100% sure about the exact way how to get all this from Archetypes file, it's been a while since I used this.

Plone Foundation Code of Conduct