Refactor batch execution options to use colander/deform
This commit is contained in:
parent
2cbacd6187
commit
dd04459748
12 changed files with 87 additions and 99 deletions
|
@ -29,6 +29,7 @@ from __future__ import unicode_literals, absolute_import
|
|||
import os
|
||||
import datetime
|
||||
import logging
|
||||
import tempfile
|
||||
from cStringIO import StringIO
|
||||
|
||||
import six
|
||||
|
@ -39,13 +40,16 @@ from rattail.db import model, Session as RattailSession
|
|||
from rattail.threads import Thread
|
||||
from rattail.util import load_object, prettify
|
||||
|
||||
import colander
|
||||
import deform
|
||||
from deform import widget as dfwidget
|
||||
from pyramid import httpexceptions
|
||||
from pyramid.renderers import render_to_response
|
||||
from pyramid.response import FileResponse
|
||||
from pyramid_simpleform import Form
|
||||
from pyramid_deform import SessionFileUploadTempStore
|
||||
from webhelpers2.html import HTML, tags
|
||||
|
||||
from tailbone import forms, grids
|
||||
from tailbone import forms2 as forms, grids
|
||||
from tailbone.db import Session
|
||||
from tailbone.views import MasterView
|
||||
from tailbone.progress import SessionProgress
|
||||
|
@ -133,8 +137,9 @@ class BatchMasterView(MasterView):
|
|||
kwargs['handler'] = self.handler
|
||||
kwargs['execute_title'] = self.get_execute_title(batch)
|
||||
kwargs['execute_enabled'] = self.instance_executable(batch)
|
||||
if kwargs['execute_enabled'] and self.has_execution_options(batch):
|
||||
kwargs['rendered_execution_options'] = self.render_execution_options(batch)
|
||||
if kwargs['execute_enabled']:
|
||||
url = self.get_action_url('execute', batch)
|
||||
kwargs['execute_form'] = self.make_execute_form(batch, action_url=url)
|
||||
return kwargs
|
||||
|
||||
def allow_worksheet(self, batch):
|
||||
|
@ -179,22 +184,12 @@ class BatchMasterView(MasterView):
|
|||
return batch.id_str
|
||||
|
||||
def template_kwargs_index(self, **kwargs):
|
||||
kwargs['execute_enabled'] = self.instance_executable(None)
|
||||
if kwargs['execute_enabled'] and self.has_execution_options():
|
||||
kwargs['rendered_execution_options'] = self.render_execution_options()
|
||||
route_prefix = self.get_route_prefix()
|
||||
if self.results_executable:
|
||||
url = self.request.route_url('{}.execute_results'.format(route_prefix))
|
||||
kwargs['execute_form'] = self.make_execute_form(action_url=url)
|
||||
return kwargs
|
||||
|
||||
def render_execution_options(self, batch=None):
|
||||
if batch is None:
|
||||
batch = self.model_class
|
||||
form = self.make_execution_options_form(batch)
|
||||
kwargs = {
|
||||
'batch': batch,
|
||||
'form': forms.FormRenderer(form),
|
||||
}
|
||||
kwargs = self.get_exec_options_kwargs(**kwargs)
|
||||
return self.render('exec_options', kwargs)
|
||||
|
||||
def get_exec_options_kwargs(self, **kwargs):
|
||||
return kwargs
|
||||
|
||||
|
@ -422,10 +417,6 @@ class BatchMasterView(MasterView):
|
|||
def template_kwargs_edit(self, **kwargs):
|
||||
batch = kwargs['instance']
|
||||
kwargs['batch'] = batch
|
||||
kwargs['execute_title'] = self.get_execute_title(batch)
|
||||
kwargs['execute_enabled'] = self.instance_executable(batch)
|
||||
if kwargs['execute_enabled'] and self.has_execution_options(batch):
|
||||
kwargs['rendered_execution_options'] = self.render_execution_options(batch)
|
||||
return kwargs
|
||||
|
||||
def mobile_mark_complete(self):
|
||||
|
@ -609,19 +600,28 @@ class BatchMasterView(MasterView):
|
|||
# TODO
|
||||
execution_options_schema = None
|
||||
|
||||
def make_execution_options_form(self, batch=None):
|
||||
def make_execute_schema(self):
|
||||
return self.execution_options_schema()
|
||||
|
||||
def make_execute_form(self, batch=None, **kwargs):
|
||||
"""
|
||||
Return a proper Form for execution options.
|
||||
"""
|
||||
if batch is None:
|
||||
batch = self.model_class
|
||||
defaults = {}
|
||||
for field in self.execution_options_schema.fields:
|
||||
key = 'batch.{}.execute_option.{}'.format(batch.batch_key, field)
|
||||
if key in self.request.session:
|
||||
defaults[field] = self.request.session[key]
|
||||
return Form(self.request, schema=self.execution_options_schema,
|
||||
defaults=defaults or None)
|
||||
route_prefix = self.get_route_prefix()
|
||||
|
||||
if self.has_execution_options(batch):
|
||||
if batch is None:
|
||||
batch = self.model_class
|
||||
schema = self.make_execute_schema()
|
||||
for field in schema:
|
||||
key = 'batch.{}.execute_option.{}'.format(batch.batch_key, field.name)
|
||||
if key in self.request.session:
|
||||
defaults[field.name] = self.request.session[key]
|
||||
else:
|
||||
schema = colander.Schema()
|
||||
|
||||
return forms.Form(schema=schema, request=self.request, defaults=defaults, **kwargs)
|
||||
|
||||
def get_execute_title(self, batch):
|
||||
if hasattr(self.handler, 'get_execute_title'):
|
||||
|
@ -855,7 +855,8 @@ class BatchMasterView(MasterView):
|
|||
"""
|
||||
batch = self.get_instance()
|
||||
query = self.get_effective_row_data(sort=False)
|
||||
batch.rowcount -= query.count()
|
||||
if batch.rowcount is not None:
|
||||
batch.rowcount -= query.count()
|
||||
query.update({'removed': True}, synchronize_session=False)
|
||||
return self.redirect(self.get_action_url('view', batch))
|
||||
|
||||
|
@ -865,17 +866,14 @@ class BatchMasterView(MasterView):
|
|||
displays a progress indicator page.
|
||||
"""
|
||||
batch = self.get_instance()
|
||||
if self.request.method == 'POST':
|
||||
self.executing = True
|
||||
form = self.make_execute_form(batch)
|
||||
if form.validate(newstyle=True):
|
||||
kwargs = dict(form.validated)
|
||||
|
||||
kwargs = {}
|
||||
if self.has_execution_options(batch):
|
||||
form = self.make_execution_options_form(batch)
|
||||
assert form.validate() # TODO
|
||||
kwargs.update(form.data)
|
||||
|
||||
# cache options to use as defaults next time
|
||||
for key, value in form.data.iteritems():
|
||||
self.request.session['batch.{}.execute_option.{}'.format(batch.batch_key, key)] = value
|
||||
# cache options to use as defaults next time
|
||||
for key, value in form.validated.items():
|
||||
self.request.session['batch.{}.execute_option.{}'.format(batch.batch_key, key)] = value
|
||||
|
||||
key = '{}.execute'.format(self.model_class.__tablename__)
|
||||
progress = SessionProgress(self.request, key)
|
||||
|
@ -888,7 +886,7 @@ class BatchMasterView(MasterView):
|
|||
'cancel_msg': "Batch execution was canceled.",
|
||||
})
|
||||
|
||||
self.request.session.flash("Sorry, you must POST to execute a batch.", 'error')
|
||||
self.request.session.flash("Invalid request: {}".format(form.make_deform_form().error), 'error')
|
||||
return self.redirect(self.get_action_url('view', batch))
|
||||
|
||||
def execute_error_message(self, error):
|
||||
|
@ -949,18 +947,13 @@ class BatchMasterView(MasterView):
|
|||
Starts a separate thread for the execution, and displays a progress
|
||||
indicator page.
|
||||
"""
|
||||
if self.request.method == 'POST':
|
||||
form = self.make_execute_form()
|
||||
if form.validate(newstyle=True):
|
||||
kwargs = dict(form.validated)
|
||||
|
||||
kwargs = {}
|
||||
if self.has_execution_options():
|
||||
form = self.make_execution_options_form()
|
||||
if not form.validate():
|
||||
raise RuntimeError("Execution options form did not validate")
|
||||
kwargs.update(form.data)
|
||||
|
||||
# cache options to use as defaults next time
|
||||
for key, value in form.data.iteritems():
|
||||
self.request.session['batch.{}.execute_option.{}'.format(self.model_class.batch_key, key)] = value
|
||||
# cache options to use as defaults next time
|
||||
for key, value in form.validated.items():
|
||||
self.request.session['batch.{}.execute_option.{}'.format(self.model_class.batch_key, key)] = value
|
||||
|
||||
key = '{}.execute_results'.format(self.model_class.__tablename__)
|
||||
batches = self.get_effective_data()
|
||||
|
@ -974,7 +967,7 @@ class BatchMasterView(MasterView):
|
|||
'cancel_msg': "Batch execution was canceled",
|
||||
})
|
||||
|
||||
self.request.session.flash("Sorry, you must POST to execute batches", 'error')
|
||||
self.request.session.flash("Invalid request: {}".format(form.make_deform_form().error), 'error')
|
||||
return self.redirect(self.get_index_url())
|
||||
|
||||
def execute_results_thread(self, batches, user_uuid, progress=None, **kwargs):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue