"""Workflow for applicants. """ import grok from datetime import datetime from hurry.workflow.workflow import Transition, WorkflowState, NullCondition from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent from waeup.sirp.applicants.interfaces import IApplicantBaseData from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo INITIALIZED = 'initialized' STARTED = 'started' SUBMITTED = 'submitted' ADMITTED = 'admitted' NOT_ADMITTED = 'not admitted' CREATED = 'created' APPLICATION_TRANSITIONS = ( Transition( transition_id = 'init', title = 'Initialize application', source = None, condition = NullCondition, msg = 'application process initialized', destination = INITIALIZED), Transition( transition_id = 'start', title = 'Start application', msg = 'application process started', source = INITIALIZED, destination = STARTED), Transition( transition_id = 'submit', title = 'Submit application', msg = 'application record submitted', source = STARTED, destination = SUBMITTED), Transition( transition_id = 'admit', title = 'Admit applicant', msg = 'applicant admitted', source = SUBMITTED, destination = ADMITTED), Transition( transition_id = 'refuse1', title = 'Refuse application', msg = 'application refused', source = SUBMITTED, destination = NOT_ADMITTED), Transition( transition_id = 'refuse2', title = 'Refuse application', msg = 'application refused', source = ADMITTED, destination = NOT_ADMITTED), Transition( transition_id = 'create', title = 'Create student record', msg = 'student record created', source = ADMITTED, destination = CREATED), Transition( transition_id = 'reset1', title = 'Reset application', msg = 'application record reset', source = SUBMITTED, destination = STARTED), Transition( transition_id = 'reset2', title = 'Reset application', msg = 'application record reset', source = ADMITTED, destination = STARTED), Transition( transition_id = 'reset3', title = 'Reset application', msg = 'application record reset', source = NOT_ADMITTED, destination = STARTED), Transition( transition_id = 'reset4', title = 'Reset application', msg = 'application record reset', source = CREATED, destination = STARTED), ) application_workflow = WAeUPWorkflow(APPLICATION_TRANSITIONS) class ApplicationWorkflowState(WorkflowState, grok.Adapter): """An adapter to adapt Applicant objects to workflow states. """ grok.context(IApplicantBaseData) grok.provides(IWorkflowState) state_key = 'wf.application.state' state_id = 'wf.application.id' class ApplicationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter): """Adapter to adapt Applicant objects to workflow info objects. """ grok.context(IApplicantBaseData) grok.provides(IWAeUPWorkflowInfo) def __init__(self, context): self.context = context self.wf = application_workflow @grok.subscribe(IApplicantBaseData, IWorkflowTransitionEvent) def handle_applicant_transition_event(obj, event): """Append message to applicant when transition happened. """ timestamp = datetime.now().strftime("%d/%m/%Y %H:%M:%S") msg = '%s - %s (new state: %s)' % ( timestamp, event.transition.user_data['msg'], event.destination) history = IObjectHistory(obj) history.addMessage(msg) return