Add basic support for receiving from multiple invoice files

This commit is contained in:
Lance Edgar 2023-01-10 16:46:21 -06:00
parent 2b7ebedb22
commit dfa4178204
10 changed files with 295 additions and 40 deletions

View file

@ -53,6 +53,7 @@ from rattail.gpc import GPC
import colander
import deform
from deform import widget as dfwidget
from pyramid import httpexceptions
from pyramid.renderers import get_renderer, render_to_response, render
from pyramid.response import FileResponse
@ -691,26 +692,40 @@ class MasterView(View):
def normalize_uploads(self, form, skip=None):
uploads = {}
def normalize(filedict):
tempdir = tempfile.mkdtemp()
filepath = os.path.join(tempdir, filedict['filename'])
tmpinfo = form.deform_form[node.name].widget.tmpstore.get(filedict['uid'])
tmpdata = tmpinfo['fp'].read()
with open(filepath, 'wb') as f:
f.write(tmpdata)
return {'tempdir': tempdir,
'temp_path': filepath}
for node in form.schema:
if isinstance(node.typ, deform.FileData):
if skip and node.name in skip:
continue
# TODO: does form ever *not* have 'validated' attr here?
if hasattr(form, 'validated'):
filedict = form.validated.get(node.name)
if skip and node.name in skip:
continue
value = form.validated.get(node.name)
if not value:
continue
if isinstance(value, dfwidget.filedict):
uploads[node.name] = normalize(value)
elif not isinstance(value, dict):
try:
values = iter(value)
except TypeError:
pass
else:
filedict = self.form_deserialized.get(node.name)
if filedict:
tempdir = tempfile.mkdtemp()
filepath = os.path.join(tempdir, filedict['filename'])
tmpinfo = form.deform_form[node.name].widget.tmpstore.get(filedict['uid'])
tmpdata = tmpinfo['fp'].read()
with open(filepath, 'wb') as f:
f.write(tmpdata)
uploads[node.name] = {
'tempdir': tempdir,
'temp_path': filepath,
}
for value in values:
if isinstance(value, dfwidget.filedict):
uploads.setdefault(node.name, []).append(
normalize(value))
return uploads
def process_uploads(self, obj, form, uploads):