class WizardStep:
"""Provides convenience methods for wizard steps. You -must- setup a bean
in your subclass"""
bean = None
goingBackwards = False
def callWizard(self, action):
self.wizard(callingStep=self, action=action)
def __call__(self, *args, **kargs):
self.formValues = {}
self.errorMessages = {}
self.initWizard()
self.bean.stepList = self.wizard.steps
self.callWizard("Show")
def run(self):
"""This method is called when it is time for us to run."""
self.executeAction()
self.forward(self.getName() + "_view")
def forward(self, where):
_ = self._ctx._
if self.errorMessages:
self.setError(_("""\
An error was found in your data. See below for more information."""),
errors=self.errorMessages)
self.bean.defaults = [
self.getPreviousData() or {},
self.formValues or {},
self.getDefaults(),
]
self._ctx.iLib.forward(where, self.bean)
def initWizard(self):
self.wizard = self._ctx.iLib.aquariumFactory(
"screen." + self.wizardName)
def getDefaults(self):
return {}
def review(self):
return ""
def doNextAction(self):
"""Tells the wizard to take us to the next step."""
nextMethod = lambda: self.callWizard("Next")
self.validateAndProcess(nextMethod)
def doPrevAction(self):
"""Tells the wizard to take us to the previous step."""
self.goingBackwards = True
prevMethod = lambda: self.callWizard("Prev")
self.validateAndProcess(prevMethod)
# If we are executing this code, validation failed, we allow
# going previous without processing the data, we hold on to
# it in the session so that we can show it later.
self._ctx.session["wizard" + self.getName()] = self._ctx.form
prevMethod()
def getPreviousData(self):
prevData = self._ctx.session.get("wizard" + self.getName())
if prevData:
del self._ctx.session["wizard" + self.getName()]
return prevData
def doCancelAction(self):
"""Tells the wizard to cancel."""
self.callWizard("Cancel")
def validateAndProcess(self, successCallback):
if self.validate():
self.process()
successCallback()
def validate(self):
"""You should override this with a validator that actually does
something. On success it should populate self.formValues with
the return data from FormValid. On failure it should populate
self.formValues with self._ctx.form
"""
self.formValues = self._ctx.form
self.errorMessages = {}
return True
def process(self):
"""Override this method so that you can actually process the data.
Most likely this means to save the data.
Note that if you are reading from self._ctx.form in here (instead
of self.formValues) you are probably doing something wrong.
"""
pass
|