I have register a custom script action in my easyform. the form contains three upload fields for files. i would like to check all uploadfields and sum up the filesizes. if a limit is reached, then an error should thrown.
Normally in a z3c form i would register an invariant on the form schema. on the easyform i use a custom script. This script call a browserview to validate the fields.
# this is the custom action script for a easyform onject
valid, message = easyform.restrictedTraverse('@@validate_all_file_sizes')(fields)
if not valid:
return {request.FORM_ERROR_MARKER:message}
This is the Browserview to collect and sum the filesizes:
class ValidateFileSizes(BrowserView):
def __call__(self, fields = []):
# 3 Scenarios are possible
# 1. upload at first time
# form values contain: form.widgets.KEY
#
# ('form.widgets.file1', <ZPublisher.HTTPRequest.FileUpload object at 0x75eeb414a660>),
# 2. form submit at second time - no value change
# form values contains form.widgets.KEY.action == "nochange"
# form values contains form.widgets.KEY.file_upload_id
#
# ('form.widgets.file1.action', 'nochange'),
# ('form.widgets.file1.file_upload_id', '005dc636fc7844629736f0ea737dcecc'),
# 3. form submit at second time - value is changed
# form values contain: form.widgets.KEY
# form values contains form.widgets.KEY.action == "replace"
# form values contains form.widgets.KEY.file_upload_id
#
# ('form.widgets.file1', <ZPublisher.HTTPRequest.FileUpload object at 0x75eeb414a660>),
# ('form.widgets.file1.action', 'replace'),
# ('form.widgets.file1.file_upload_id', '7f932dd470e74ce58e4425def28763b4'),
upload_storage = IFileUploadTemporaryStorage(getSite())
# upload_storage.cleanup(force=True)
upload_map = upload_storage.upload_map
upload_fields = []
model = get_schema(self.context)
fields = getFields(model)
# collect all fields for fileupload
for name, field in fields.items():
if isinstance(field, NamedBlobFile):
upload_fields.append(name)
if not upload_fields:
# no uploadfields in form
# no size check needed
return
for name in upload_fields:
# the id of the uploaded file
file_upload_id = self.request.get(f"form.widgets.{name}.file_upload_id", 0)
print(f"file_upload_id: {file_upload_id}")
for key in upload_map.keys():
fileinfo = upload_map.get(key, None)
print(f"Uploaded Files in Storage: Key:{key} | Filename: {fileinfo.get("filename", None)} | Type: {type(fileinfo)}")
formvalue = self.request.get(f"form.widgets.{name}", None)
if formvalue is not None:
# Scenario 1
# handle the fileupload directly
# ...
pass
else:
# Scenario 2
# Form is a second time submitted because user handle formerrors after first submit
# now check temporarly fileupload in portal root --> FileUploadTemporaryStorage from plone.formwidget.namedfile
if self.request.get(f"form.widgets.{name}.action", None) == "nochange" and file_upload_id:
fileinfo = upload_map.get(file_upload_id, None)
breakpoint()
# -> fileinfo is None or not in upload_map
# -> feels like a bug
# ... and so on
return (False, "Error")
Can someone enlighten me on the use case of the FileUploadTemporaryStorage? Or alternativ, How can i register an invariant for easyform?