source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/workflow.py @ 7055

Last change on this file since 7055 was 6644, checked in by Henrik Bettermann, 13 years ago

Don't add log message when no attribute has been changed, but log all transitions.

  • Property svn:keywords set to Id
File size: 3.9 KB
RevLine 
[6295]1"""Workflow for applicants.
2"""
3import grok
[6318]4from datetime import datetime
[6353]5from hurry.workflow.workflow import Transition, WorkflowState, NullCondition
6from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent
[6295]7from waeup.sirp.applicants.interfaces import IApplicantBaseData
[6353]8from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo
[6355]9from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo
[6644]10from waeup.sirp.utils.helpers import get_current_principal
[6295]11
12INITIALIZED = 'initialized'
13STARTED = 'started'
14SUBMITTED = 'submitted'
15ADMITTED = 'admitted'
16NOT_ADMITTED = 'not admitted'
17CREATED = 'created'
18
[6354]19APPLICATION_TRANSITIONS = (
[6353]20    Transition(
[6295]21        transition_id = 'init',
[6306]22        title = 'Initialize application',
[6295]23        source = None,
24        condition = NullCondition,
[6471]25        msg = 'Application initialized',
[6353]26        destination = INITIALIZED),
[6295]27
[6353]28    Transition(
[6295]29        transition_id = 'start',
[6300]30        title = 'Start application',
[6471]31        msg = 'Application started',
[6295]32        source = INITIALIZED,
[6353]33        destination = STARTED),
[6295]34
[6353]35    Transition(
[6295]36        transition_id = 'submit',
[6307]37        title = 'Submit application',
[6471]38        msg = 'Application submitted',
[6295]39        source = STARTED,
[6353]40        destination = SUBMITTED),
[6295]41
[6353]42    Transition(
[6295]43        transition_id = 'admit',
[6300]44        title = 'Admit applicant',
[6471]45        msg = 'Applicant admitted',
[6295]46        source = SUBMITTED,
[6353]47        destination = ADMITTED),
[6295]48
[6353]49    Transition(
[6300]50        transition_id = 'refuse1',
51        title = 'Refuse application',
[6471]52        msg = 'Application refused',
[6295]53        source = SUBMITTED,
[6353]54        destination = NOT_ADMITTED),
[6295]55
[6353]56    Transition(
[6300]57        transition_id = 'refuse2',
58        title = 'Refuse application',
[6471]59        msg = 'Application refused',
[6300]60        source = ADMITTED,
[6353]61        destination = NOT_ADMITTED),
[6300]62
[6353]63    Transition(
[6295]64        transition_id = 'create',
[6307]65        title = 'Create student record',
[6471]66        msg = 'Student record created',
[6295]67        source = ADMITTED,
[6353]68        destination = CREATED),
[6295]69
[6353]70    Transition(
[6300]71        transition_id = 'reset1',
72        title = 'Reset application',
[6471]73        msg = 'Application reset',
[6300]74        source = SUBMITTED,
[6353]75        destination = STARTED),
[6295]76
[6353]77    Transition(
[6300]78        transition_id = 'reset2',
79        title = 'Reset application',
[6471]80        msg = 'Application reset',
[6300]81        source = ADMITTED,
[6353]82        destination = STARTED),
[6300]83
[6353]84    Transition(
[6300]85        transition_id = 'reset3',
86        title = 'Reset application',
[6471]87        msg = 'Application reset',
[6300]88        source = NOT_ADMITTED,
[6353]89        destination = STARTED),
[6300]90
[6353]91    Transition(
[6300]92        transition_id = 'reset4',
93        title = 'Reset application',
[6471]94        msg = 'Application reset',
[6300]95        source = CREATED,
[6353]96        destination = STARTED),
97    )
[6300]98
[6354]99application_workflow = WAeUPWorkflow(APPLICATION_TRANSITIONS)
[6295]100
[6349]101class ApplicationWorkflowState(WorkflowState, grok.Adapter):
[6295]102    """An adapter to adapt Applicant objects to workflow states.
103    """
104    grok.context(IApplicantBaseData)
105    grok.provides(IWorkflowState)
[6316]106
[6349]107    state_key = 'wf.application.state'
108    state_id = 'wf.application.id'
109
[6353]110class ApplicationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter):
[6295]111    """Adapter to adapt Applicant objects to workflow info objects.
112    """
113    grok.context(IApplicantBaseData)
[6353]114    grok.provides(IWAeUPWorkflowInfo)
[6318]115
[6349]116    def __init__(self, context):
117        self.context = context
118        self.wf = application_workflow
[6318]119
120@grok.subscribe(IApplicantBaseData, IWorkflowTransitionEvent)
121def handle_applicant_transition_event(obj, event):
[6471]122    """Append message to applicant history when transition happened.
[6318]123    """
[6471]124    msg = '%s' % event.transition.user_data['msg']
[6339]125    history = IObjectHistory(obj)
126    history.addMessage(msg)
[6644]127    # In some tests we don't have a an applicants root or a user
128    try:
129        user = get_current_principal()
130        applicants_root = grok.getSite()['applicants']
131        applicants_root.logger.info('%s - %s - %s' % (user.id,obj.access_code,msg))
132    except (TypeError, AttributeError):
133        pass
[6318]134    return
Note: See TracBrowser for help on using the repository browser.