diff --git a/tailbone/templates/batch/create.mako b/tailbone/templates/batch/create.mako index 6e4c57b6..bb7c5cfe 100644 --- a/tailbone/templates/batch/create.mako +++ b/tailbone/templates/batch/create.mako @@ -1,7 +1,7 @@ ## -*- coding: utf-8 -*- <%inherit file="/crud.mako" /> -<%def name="title()">Upload ${batch_display} +<%def name="title()">New ${batch_display} <%def name="context_menu_items()"> % if request.has_perm('{0}.view'.format(permission_prefix)): diff --git a/tailbone/views/batch.py b/tailbone/views/batch.py index ff37aad3..a7baac63 100644 --- a/tailbone/views/batch.py +++ b/tailbone/views/batch.py @@ -414,8 +414,14 @@ class BatchCrud(BaseCrud): if self.creating: del fs.created del fs.created_by - del fs.cognized - del fs.cognized_by + if 'cognized' in fs.render_fields: + del fs.cognized + if 'cognized_by' in fs.render_fields: + del fs.cognized_by + if 'executed' in fs.render_fields: + del fs.executed + if 'executed_by' in fs.render_fields: + del fs.executed_by return fs def configure_fieldset(self, fieldset): @@ -434,6 +440,46 @@ class BatchCrud(BaseCrud): fs.executed_by, ]) + def init_batch(self, batch): + """ + Initialize a new batch. Derived classes can override this to + effectively provide default values for a batch, etc. This method is + invoked after a batch has been fully prepared for insertion to the + database, but before the push to the database occurs. + + Note that the return value of this function matters; if it is boolean + false then the batch will not be persisted at all, and the user will be + redirected to the "create batch" page. + """ + return True + + def save_form(self, form): + """ + Save the uploaded data file if necessary, etc. If batch initialization + fails, don't persist the batch at all; the user will be sent back to + the "create batch" page in that case. + """ + # Transfer form data to batch instance. + form.fieldset.sync() + batch = form.fieldset.model + + # For new batches, assign current user as creator, etc. + if self.creating: + with Session.no_autoflush: + batch.created_by = self.request.user or self.late_login_user() + + # Expunge batch from session to prevent it from being flushed + # during init. This is done as a convenience to views which + # provide an init method. Some batches may have required fields + # which aren't filled in yet, but the view may need to query the + # database to obtain the values. This will cause a session flush, + # and the missing fields will trigger data integrity errors. + Session.expunge(batch) + self.batch_inited = self.init_batch(batch) + if self.batch_inited: + Session.add(batch) + Session.flush() + def update(self): """ Don't allow editing a batch which has already been executed. @@ -541,7 +587,7 @@ class BatchCrud(BaseCrud): else: batch.cognized_by = self.request.user - def refresh_thread(self, batch_uuid, cognizer_uuid=None, success_url=None, progress=None): + def refresh_thread(self, batch_uuid, progress=None, cognizer_uuid=None, success_url=None): """ Thread target for refreshing batch data with progress indicator. """ @@ -718,19 +764,6 @@ class FileBatchCrud(BatchCrud): self.handler.set_data_file(batch, path) os.remove(path) - def init_batch(self, batch): - """ - Initialize a new batch. Derived classes can override this to - effectively provide default values for a batch, etc. This method is - invoked after a batch has been fully prepared for insertion to the - database, but before the push to the database occurs. - - Note that the return value of this function matters; if it is boolean - false then the batch will not be persisted at all, and the user will be - redirected to the "create batch" page. - """ - return True - def post_save(self, form): """ This checks for failed batch initialization when creating a new batch.