Add Execution Options for a Batch
This assumes you already have a batch (table, handler, master view), and population / execution works as expected, but you wish to provide user with some options at the execution step.
Batch Handler
Ultimately the new execution options must be recognized and implemented by the batch handler, so that's our first step.
Let's say we have a "pricing" batch handler and we want to let the user declare whether or not new labels should be deployed along with the new prices, and to which label printer if so.
We might edit a class called PricingBatchHandler in a file like ~/src/poser/poser/batch/pricing.py:
1 from rattail.batch import pricing as base
2
3 class PricingBatchHandler(base.PricingBatchHandler):
4
5 # must add new options to method signature
6 def execute(self, batch, deploy_labels=False, label_printer=None, progress=None, **kwargs):
7
8 # do whatever you normally do for execution here
9 self.deploy_prices(batch, progress=progress)
10
11 # check status of new option, and act accordingly
12 if deploy_labels:
13 self.deploy_labels(batch, label_printer, progress=progress)
14
15 return True
16
17 def deploy_labels(self, batch, label_printer, progress=None):
18
19 # must do something here
20 print("deploying labels to printer: %s" % label_printer)
Batch Master View
Now that the batch handler will honor the new execution options, we declare them for the UI, in the batch master view.
Note that the important part, is that you define this "execution options schema" such that its nodes correspond to the options accepted by your batch handler's execute() method, i.e. per the above step. They should match in both "name" and "data type".
Here we might edit a class called PricingBatchView in a file like ~/src/poser/poser/web/views/batch/pricing.py:
1 import colander
2 from tailbone import forms
3
4 from tailbone.views.batch import pricing as base
5
6
7 POSSIBLE_PRINTERS = {
8 'zebra001': "Zebra #1",
9 'zebra002': "Zebra #2",
10 'cognitive001': "Cognitive #1",
11 }
12
13
14 # must add this; can call it whatever you like
15 class ExecutionOptions(colander.Schema):
16
17 # NOTE: these schema nodes must correspond in both name and type,
18 # to the options accepted by handler's `execute()` method
19
20 deploy_labels = colander.SchemaNode(colander.Bool())
21
22 label_printer = colander.SchemaNode(
23 colander.String(),
24 validator=colander.OneOf(POSSIBLE_PRINTERS),
25 widget=forms.widgets.PlainSelectWidget(values=POSSIBLE_PRINTERS.items()))
26
27
28 class PricingBatchView(base.PricingBatchView):
29
30 # must add this; should refer to schema defined above
31 execution_options_schema = ExecutionOptions