Fix bugs when making inventory batch on mobile

This commit is contained in:
Lance Edgar 2018-02-01 14:28:24 -06:00
parent d35dc5582e
commit c74bce2fdb
3 changed files with 57 additions and 2 deletions

View file

@ -271,6 +271,38 @@ class BatchMasterView(MasterView):
# return this object to replace the original # return this object to replace the original
return batch return batch
# TODO: this is a totaly copy of save_create_form()
def save_mobile_create_form(self, form):
self.before_create(form)
with Session.no_autoflush:
# transfer form data to batch instance
form.fieldset.sync()
batch = form.fieldset.model
# current user is batch creator
batch.created_by = self.request.user or self.late_login_user()
# destroy initial batch and re-make using handler
kwargs = self.get_batch_kwargs(batch)
Session.expunge(batch)
batch = self.handler.make_batch(Session(), **kwargs)
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)
# return this object to replace the original
return batch
def get_batch_kwargs(self, batch, mobile=False): def get_batch_kwargs(self, batch, mobile=False):
""" """
Return a kwargs dict for use with ``self.handler.make_batch()``, using Return a kwargs dict for use with ``self.handler.make_batch()``, using

View file

@ -221,6 +221,22 @@ class InventoryBatchView(BatchMasterView):
return super(InventoryBatchView, self).delete_row() return super(InventoryBatchView, self).delete_row()
def configure_mobile_fieldset(self, fs): def configure_mobile_fieldset(self, fs):
permission_prefix = self.get_permission_prefix()
# TODO: this was copied from configure_form()
modes = dict(self.enum.INVENTORY_MODE)
if not self.request.has_perm('{}.create.replace'.format(permission_prefix)):
if hasattr(self.enum, 'INVENTORY_MODE_REPLACE'):
modes.pop(self.enum.INVENTORY_MODE_REPLACE, None)
if hasattr(self.enum, 'INVENTORY_MODE_REPLACE_ADJUST'):
modes.pop(self.enum.INVENTORY_MODE_REPLACE_ADJUST, None)
if not self.request.has_perm('{}.create.zero'.format(permission_prefix)):
if hasattr(self.enum, 'INVENTORY_MODE_ZERO_ALL'):
modes.pop(self.enum.INVENTORY_MODE_ZERO_ALL, None)
fs.mode.set(renderer=forms.renderers.EnumFieldRenderer(modes),
label="Count Mode", required=True, attrs={'auto-enhance': 'true'})
fs.configure(include=[ fs.configure(include=[
fs.mode, fs.mode,
fs.reason_code, fs.reason_code,

View file

@ -268,9 +268,9 @@ class MasterView(View):
self.creating = True self.creating = True
form = self.make_mobile_form(self.get_model_class()) form = self.make_mobile_form(self.get_model_class())
if self.request.method == 'POST': if self.request.method == 'POST':
if form.validate(): if self.validate_mobile_form(form):
# let save_create_form() return alternate object if necessary # let save_create_form() return alternate object if necessary
obj = self.save_create_form(form) or form.fieldset.model obj = self.save_mobile_create_form(form) or form.fieldset.model
self.after_create(obj) self.after_create(obj)
self.flash_after_create(obj) self.flash_after_create(obj)
return self.redirect_after_create(obj, mobile=True) return self.redirect_after_create(obj, mobile=True)
@ -284,6 +284,10 @@ class MasterView(View):
self.before_create(form) self.before_create(form)
form.save() form.save()
def save_mobile_create_form(self, form):
self.before_create(form)
form.save()
def redirect_after_create(self, instance, mobile=False): def redirect_after_create(self, instance, mobile=False):
if self.populatable and self.should_populate(instance): if self.populatable and self.should_populate(instance):
return self.redirect(self.get_action_url('populate', instance, mobile=mobile)) return self.redirect(self.get_action_url('populate', instance, mobile=mobile))
@ -817,6 +821,9 @@ class MasterView(View):
def validate_form(self, form): def validate_form(self, form):
return form.validate() return form.validate()
def validate_mobile_form(self, form):
return form.validate()
def save_edit_form(self, form): def save_edit_form(self, form):
self.save_form(form) self.save_form(form)
self.after_edit(form.fieldset.model) self.after_edit(form.fieldset.model)