source: main/waeup.sirp/trunk/src/waeup/sirp/students/workflow.py @ 6741

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

Display Id and state in student layout too. This doesn't look nice on smart phones. Have to think about another solution.

  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1"""Workflow for students.
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.students.interfaces import IStudent
8from waeup.sirp.interfaces import IObjectHistory, IWAeUPWorkflowInfo
9from waeup.sirp.workflow import WAeUPWorkflow, WAeUPWorkflowInfo
10from waeup.sirp.utils.helpers import get_current_principal
11
12CREATED = 'created'
13ADMITTED = 'admitted'
14CLEARANCE = 'clearance started'
15REQUESTED = 'clearance requested'
16CLEARED = 'cleared'
17
18REGISTRATION_TRANSITIONS = (
19    Transition(
20        transition_id = 'create',
21        title = 'Create student',
22        source = None,
23        condition = NullCondition,
24        msg = 'Student record created',
25        destination = CREATED),
26
27    Transition(
28        transition_id = 'admit',
29        title = 'Admit student',
30        msg = 'Student admitted',
31        source = CREATED,
32        destination = ADMITTED),
33
34    Transition(
35        transition_id = 'reset1',
36        title = 'Reset student',
37        msg = 'Student record reset',
38        source = ADMITTED,
39        destination = CREATED),
40
41    Transition(
42        transition_id = 'start_clearance',
43        title = 'Start clearance',
44        msg = 'Clearance started',
45        source = ADMITTED,
46        destination = CLEARANCE),
47
48    Transition(
49        transition_id = 'reset2',
50        title = 'Reset to admitted',
51        msg = 'Student record reset to admitted',
52        source = CLEARANCE,
53        destination = ADMITTED),
54
55    Transition(
56        transition_id = 'request_clearance',
57        title = 'Request clearance',
58        msg = 'Clearance requested',
59        source = CLEARANCE,
60        destination = REQUESTED),
61
62    Transition(
63        transition_id = 'reset3',
64        title = 'Reset to clearance',
65        msg = 'Student record reset to clearance',
66        source = REQUESTED,
67        destination = CLEARANCE),
68
69    Transition(
70        transition_id = 'clear',
71        title = 'Clear student',
72        msg = 'Cleared',
73        source = REQUESTED,
74        destination = CLEARED),
75
76    Transition(
77        transition_id = 'reset4',
78        title = 'Reset to clearance',
79        msg = 'Student record reset to clearance',
80        source = CLEARED,
81        destination = CLEARANCE),
82    )
83
84LOCK_CLEARANCE_TRANS = ('reset2', 'request_clearance')
85UNLOCK_CLEARANCE_TRANS = ('reset3', 'reset4', 'start_clearance')
86
87registration_workflow = WAeUPWorkflow(REGISTRATION_TRANSITIONS)
88
89class RegistrationWorkflowState(WorkflowState, grok.Adapter):
90    """An adapter to adapt Student objects to workflow states.
91    """
92    grok.context(IStudent)
93    grok.provides(IWorkflowState)
94
95    state_key = 'wf.registration.state'
96    state_id = 'wf.registration.id'
97
98class RegistrationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter):
99    """Adapter to adapt Student objects to workflow info objects.
100    """
101    grok.context(IStudent)
102    grok.provides(IWAeUPWorkflowInfo)
103
104    def __init__(self, context):
105        self.context = context
106        self.wf = registration_workflow
107
108@grok.subscribe(IStudent, IWorkflowTransitionEvent)
109def handle_student_transition_event(obj, event):
110    """Append message to student history and log file when transition happened.
111    """
112    msg = '%s' % event.transition.user_data['msg']
113    history = IObjectHistory(obj)
114    history.addMessage(msg)
115    if event.transition.transition_id in LOCK_CLEARANCE_TRANS:
116        obj.clearance_locked = True
117    if event.transition.transition_id in UNLOCK_CLEARANCE_TRANS:
118        obj.clearance_locked = False
119    # In some tests we don't have a students container or a user
120    try:
121        user = get_current_principal()
122        students_container = grok.getSite()['students']
123        students_container.logger.info('%s - %s - %s' % (user.id,obj.student_id,msg))
124    except (TypeError, AttributeError):
125        pass
126    return
Note: See TracBrowser for help on using the repository browser.