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

Last change on this file since 6944 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
Line 
1"""Workflow for applicants.
2"""
3import grok
4from datetime import datetime
5from hurry.workflow.workflow import Transition, WorkflowState, NullCondition
6from hurry.workflow.interfaces import IWorkflowState, IWorkflowTransitionEvent
7from waeup.sirp.applicants.interfaces import IApplicantBaseData
8from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo
9from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo
10from waeup.sirp.utils.helpers import get_current_principal
11
12INITIALIZED = 'initialized'
13STARTED = 'started'
14SUBMITTED = 'submitted'
15ADMITTED = 'admitted'
16NOT_ADMITTED = 'not admitted'
17CREATED = 'created'
18
19APPLICATION_TRANSITIONS = (
20    Transition(
21        transition_id = 'init',
22        title = 'Initialize application',
23        source = None,
24        condition = NullCondition,
25        msg = 'Application initialized',
26        destination = INITIALIZED),
27
28    Transition(
29        transition_id = 'start',
30        title = 'Start application',
31        msg = 'Application started',
32        source = INITIALIZED,
33        destination = STARTED),
34
35    Transition(
36        transition_id = 'submit',
37        title = 'Submit application',
38        msg = 'Application submitted',
39        source = STARTED,
40        destination = SUBMITTED),
41
42    Transition(
43        transition_id = 'admit',
44        title = 'Admit applicant',
45        msg = 'Applicant admitted',
46        source = SUBMITTED,
47        destination = ADMITTED),
48
49    Transition(
50        transition_id = 'refuse1',
51        title = 'Refuse application',
52        msg = 'Application refused',
53        source = SUBMITTED,
54        destination = NOT_ADMITTED),
55
56    Transition(
57        transition_id = 'refuse2',
58        title = 'Refuse application',
59        msg = 'Application refused',
60        source = ADMITTED,
61        destination = NOT_ADMITTED),
62
63    Transition(
64        transition_id = 'create',
65        title = 'Create student record',
66        msg = 'Student record created',
67        source = ADMITTED,
68        destination = CREATED),
69
70    Transition(
71        transition_id = 'reset1',
72        title = 'Reset application',
73        msg = 'Application reset',
74        source = SUBMITTED,
75        destination = STARTED),
76
77    Transition(
78        transition_id = 'reset2',
79        title = 'Reset application',
80        msg = 'Application reset',
81        source = ADMITTED,
82        destination = STARTED),
83
84    Transition(
85        transition_id = 'reset3',
86        title = 'Reset application',
87        msg = 'Application reset',
88        source = NOT_ADMITTED,
89        destination = STARTED),
90
91    Transition(
92        transition_id = 'reset4',
93        title = 'Reset application',
94        msg = 'Application reset',
95        source = CREATED,
96        destination = STARTED),
97    )
98
99application_workflow = WAeUPWorkflow(APPLICATION_TRANSITIONS)
100
101class ApplicationWorkflowState(WorkflowState, grok.Adapter):
102    """An adapter to adapt Applicant objects to workflow states.
103    """
104    grok.context(IApplicantBaseData)
105    grok.provides(IWorkflowState)
106
107    state_key = 'wf.application.state'
108    state_id = 'wf.application.id'
109
110class ApplicationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter):
111    """Adapter to adapt Applicant objects to workflow info objects.
112    """
113    grok.context(IApplicantBaseData)
114    grok.provides(IWAeUPWorkflowInfo)
115
116    def __init__(self, context):
117        self.context = context
118        self.wf = application_workflow
119
120@grok.subscribe(IApplicantBaseData, IWorkflowTransitionEvent)
121def handle_applicant_transition_event(obj, event):
122    """Append message to applicant history when transition happened.
123    """
124    msg = '%s' % event.transition.user_data['msg']
125    history = IObjectHistory(obj)
126    history.addMessage(msg)
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
134    return
Note: See TracBrowser for help on using the repository browser.