问题
Dears,
I'm creating a script python to mass upload files in Plone site, the installation is UnifiedInstaller Plone 4.3.10.
This script read a txt, and this txt have separation with semicolon, the error appear when set up a file in a new created item.
Bellow the Script.
from zope.site.hooks import setSite
from plone.namedfile.file import NamedBlobFile
from plone import api
import transaction
import csv
portal = app['Plone']
setSite(portal)
container = portal['PROCESSOS']
with open('CARGA/C008_0002.txt', 'rb') as csvfile:
reader = csv.DictReader(csvfile, delimiter=';', quotechar='|')
for row in reader:
pdf_id = 'P'+str(row['IMAGEM']).strip('Pasta Geral\\ ')
file_obj = api.content.create(
container, 'File',
title=str(row['INTERESSADO']),
id=pdf_id,
description=str(row['CNPJ / CPF'])+' '+str(row['ASSUNTO']),
safe_id=True
)
pdf_path = 'INMEQ/'+str(row['IMAGEM']).replace("\\", "/")
print(pdf_path)
file_obj.file = NamedBlobFile(
data=open(pdf_path, 'r').read(),
contentType='application/pdf',
filename=str(file_obj.id),
)
print('Created: '+row['NDOPROCESSO']+'.')
transaction.commit()
When the script will set up a file the error "WrongType" appear. See verbose bellow.
Traceback (most recent call last):
File "<console>", line 18, in <module>
File "/home/jaf/plone4310/buildout-cache/eggs/plone.namedfile-3.0.9-py2.7.egg/plone/namedfile/file.py", line 384, in __init__
self.filename = filename
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/fieldproperty.py", line 52, in __set__
field.validate(value)
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 182, in validate
self._validate(value)
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 309, in _validate
super(MinMaxLen, self)._validate(value)
File "/home/jaf/plone4310/buildout-cache/eggs/zope.schema-4.2.2-py2.7.egg/zope/schema/_bootstrapfields.py", line 209, in _validate
raise WrongType(value, self._type, self.__name__)
WrongType: ('processo-al-1.pdf', <type 'unicode'>, 'filename')
Thanks for you attention!
-- Juliano Araújo
回答1:
You need to pass the filename as unicode.
file_obj.file = NamedBlobFile(
data=open(pdf_path, 'r').read(),
contentType='application/pdf',
filename=unicode(file_obj.id), # needs to be unicode
)
More Info in the plone.namedfile docu --> https://github.com/plone/plone.namedfile/blob/36014d67c3befacfe3a058f1d3d99a6a4352a31f/plone/namedfile/usage.rst
来源:https://stackoverflow.com/questions/38351633/script-python-using-plone-api-to-create-file-appear-error-wrongtype-when-set-a-f