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

Last change on this file since 6717 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: 2.3 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'
14
15REGISTRATION_TRANSITIONS = (
16    Transition(
17        transition_id = 'create',
18        title = 'Create student',
19        source = None,
20        condition = NullCondition,
21        msg = 'Student record created',
22        destination = CREATED),
23
24    Transition(
25        transition_id = 'admit',
26        title = 'Admit student',
27        msg = 'Student admitted',
28        source = CREATED,
29        destination = ADMITTED),
30
31    Transition(
32        transition_id = 'reset1',
33        title = 'Reset student',
34        msg = 'Student record reset',
35        source = ADMITTED,
36        destination = CREATED),
37    )
38
39registration_workflow = WAeUPWorkflow(REGISTRATION_TRANSITIONS)
40
41class RegistrationWorkflowState(WorkflowState, grok.Adapter):
42    """An adapter to adapt Student objects to workflow states.
43    """
44    grok.context(IStudent)
45    grok.provides(IWorkflowState)
46
47    state_key = 'wf.registration.state'
48    state_id = 'wf.registration.id'
49
50class RegistrationWorkflowInfo(WAeUPWorkflowInfo, grok.Adapter):
51    """Adapter to adapt Student objects to workflow info objects.
52    """
53    grok.context(IStudent)
54    grok.provides(IWAeUPWorkflowInfo)
55
56    def __init__(self, context):
57        self.context = context
58        self.wf = registration_workflow
59
60@grok.subscribe(IStudent, IWorkflowTransitionEvent)
61def handle_student_transition_event(obj, event):
62    """Append message to student history and log file when transition happened.
63    """
64    msg = '%s' % event.transition.user_data['msg']
65    history = IObjectHistory(obj)
66    history.addMessage(msg)
67    # In some tests we don't have a students container or a user
68    try:
69        user = get_current_principal()
70        students_container = grok.getSite()['students']
71        students_container.logger.info('%s - %s - %s' % (user.id,obj.student_id,msg))
72    except (TypeError, AttributeError):
73        pass
74    return
Note: See TracBrowser for help on using the repository browser.