Refactor handheld batch views to use master3
This commit is contained in:
parent
eac59ba5c8
commit
8eab3c5b36
3 changed files with 77 additions and 63 deletions
|
@ -27,9 +27,13 @@ Base views for maintaining batches
|
|||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
import six
|
||||
|
||||
import colander
|
||||
import deform
|
||||
from deform import widget as dfwidget
|
||||
from pyramid_deform import SessionFileUploadTempStore
|
||||
from webhelpers2.html import tags
|
||||
|
||||
from tailbone.views import MasterView3
|
||||
|
@ -46,8 +50,7 @@ class BatchMasterView3(MasterView3, BatchMasterView2):
|
|||
'created',
|
||||
'created_by',
|
||||
'rowcount',
|
||||
'cognized',
|
||||
'cognized_by',
|
||||
'status_code',
|
||||
'executed',
|
||||
'executed_by',
|
||||
'purge',
|
||||
|
@ -113,7 +116,8 @@ class BatchMasterView3(MasterView3, BatchMasterView2):
|
|||
def save_create_form(self, form):
|
||||
self.before_create(form)
|
||||
|
||||
with self.Session.no_autoflush:
|
||||
session = self.Session()
|
||||
with session.no_autoflush:
|
||||
|
||||
# transfer form data to batch instance
|
||||
batch = self.objectify(form, self.form_deserialized)
|
||||
|
@ -121,23 +125,35 @@ class BatchMasterView3(MasterView3, BatchMasterView2):
|
|||
# current user is batch creator
|
||||
batch.created_by = self.request.user or self.late_login_user()
|
||||
|
||||
# obtain kwargs for making batch via handler, below
|
||||
kwargs = self.get_batch_kwargs(batch)
|
||||
|
||||
# TODO: this needs work yet surely...
|
||||
filedict = kwargs.pop('filename', None)
|
||||
filepath = None
|
||||
if filedict:
|
||||
kwargs['filename'] = '' # null not allowed
|
||||
tempdir = tempfile.mkdtemp()
|
||||
filepath = os.path.join(tempdir, filedict['filename'])
|
||||
tmpinfo = form.deform_form['filename'].widget.tmpstore.get(filedict['uid'])
|
||||
tmpdata = tmpinfo['fp'].read()
|
||||
with open(filepath, 'wb') as f:
|
||||
f.write(tmpdata)
|
||||
|
||||
# TODO: is this still necessary with colander?
|
||||
# destroy initial batch and re-make using handler
|
||||
kwargs = self.get_batch_kwargs(batch)
|
||||
# if batch in self.Session:
|
||||
# self.Session.expunge(batch)
|
||||
batch = self.handler.make_batch(self.Session(), **kwargs)
|
||||
batch = self.handler.make_batch(session, **kwargs)
|
||||
|
||||
self.Session.flush()
|
||||
|
||||
# TODO: this needs work yet surely...
|
||||
# if batch has input data file, let handler properly establish that
|
||||
filename = getattr(batch, 'filename', None)
|
||||
if filename:
|
||||
path = os.path.join(self.upload_dir, filename)
|
||||
if os.path.exists(path):
|
||||
self.handler.set_input_file(batch, path)
|
||||
os.remove(path)
|
||||
if filedict:
|
||||
self.handler.set_input_file(batch, filepath)
|
||||
os.remove(filepath)
|
||||
os.rmdir(tempdir)
|
||||
|
||||
return batch
|
||||
|
||||
|
@ -175,11 +191,11 @@ class FileBatchMasterView3(BatchMasterView3, FileBatchMasterView2):
|
|||
if self.editing:
|
||||
f.set_readonly('filename')
|
||||
|
||||
# if creating, let filename be our only field by default
|
||||
if self.creating:
|
||||
f.fields = [
|
||||
'filename',
|
||||
]
|
||||
if 'filename' not in f.fields:
|
||||
f.fields.insert(0, 'filename')
|
||||
tmpstore = SessionFileUploadTempStore(self.request)
|
||||
f.set_node('filename', colander.SchemaNode(deform.FileData(), widget=dfwidget.FileUploadWidget(tmpstore)))
|
||||
|
||||
def render_filename(self, batch, field):
|
||||
path = batch.filepath(self.rattail_config, filename=batch.filename)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue