Hi everyone,
is it possible to provide a default file for a Dexterity Named Blob File Field?
I tried the following with some partial success:
from plone.autoform import directives
from plone.formwidget.namedfile.widget import NamedFileFieldWidget
from plone.namedfile.field import NamedBlobFile as NamedBlobFileField
from zope.interface import provider
from zope.schema.interfaces import IContextAwareDefaultFactory
def defaultParameterFileFactory(filename):
cwd = os.path.dirname(__file__)
filepath = os.path.join(cwd, "..", "data", filename)
data = open(filepath, "rb").read()
blob = NamedBlobFile(data=data, filename=filename)
@provider(IContextAwareDefaultFactory)
def factory(context):
return blob
return factory
class IDataInterfaceSchema(model.Schema):
directives.widget("parameters", NamedFileFieldWidget)
parameters = NamedBlobFileField(
title=_(u"Parameters"),
description=_(u"Please upload the Parameters Excel"),
required=False,
defaultFactory=defaultParameterFileFactory(u"ld2328_twcvua_parameter.xlsx"),
)
The file is referenced after creation, but with a size of 0 KB
:
Also the downloaded file has a size of 0 and can not be opened.
Then I tried to provide it in the update
of a custom Add-View:
import os
from plone.namedfile.file import NamedBlobFile
from plone.dexterity.browser.add import DefaultAddForm
from plone.dexterity.browser.add import DefaultAddView
def get_parameter_file(filename):
cwd = os.path.dirname(__file__)
filepath = os.path.join(cwd, "../../..", "data", filename)
data = open(filepath, "rb").read()
return NamedBlobFile(data=data, filename=filename)
class AddForm(DefaultAddForm):
def get_widget(self, name, default=None):
"""Lookup a widget by name in the current form
"""
if name in self.widgets.keys():
return self.widgets[name]
for group in self.groups:
if name in group.widgets.keys():
return group.widgets[name]
return default
def update(self):
super(AddForm, self).update()
parameters_file = get_parameter_file(u"ld2328_twcvua_parameter.xlsx")
parameters_widget = self.get_widget("parameters")
# Workaround to display the filename in the widget template
parameters_widget.ignoreContext = False
parameters_widget.value = parameters_file
...
Which seems to work nice in the add form, especially because the user can see the default file immediately:
However, no file is uploaded in this case:
Finally, I tried to provide the file directly as the default
value in the schema and not the defaultFactory
. But there I get again files with 0 KB
of size in my content type.
Any further ideas?
Thanks and best regards
Ramon