source: main/waeup.sirp/trunk/src/waeup/sirp/students/student.py @ 7066

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

Add current_session to students_catalog indexes.

  • Property svn:keywords set to Id
File size: 4.8 KB
Line 
1## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann
2## This program is free software; you can redistribute it and/or modify
3## it under the terms of the GNU General Public License as published by
4## the Free Software Foundation; either version 2 of the License, or
5## (at your option) any later version.
6##
7## This program is distributed in the hope that it will be useful,
8## but WITHOUT ANY WARRANTY; without even the implied warranty of
9## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10## GNU General Public License for more details.
11##
12## You should have received a copy of the GNU General Public License
13## along with this program; if not, write to the Free Software
14## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15##
16"""
17Container for the various objects owned by students.
18"""
19import grok
20from grok import index
21from zope.component.interfaces import IFactory
22from zope.interface import implementedBy
23from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo
24from zope.securitypolicy.interfaces import IPrincipalRoleManager
25from waeup.sirp.interfaces import IObjectHistory, IUserAccount
26from waeup.sirp.students.interfaces import (
27    IStudent, IStudentNavigation, IStudentClearanceEdit,
28    IStudentPersonalEdit)
29from waeup.sirp.students.studycourse import StudentStudyCourse
30from waeup.sirp.students.payments import StudentPaymentsContainer
31from waeup.sirp.students.accommodation import StudentAccommodation
32from waeup.sirp.utils.helpers import attrs_to_fields, get_current_principal
33from waeup.sirp.students.utils import generate_student_id
34
35class Student(grok.Container):
36    """This is a student container for the various objects
37    owned by students.
38    """
39    grok.implements(IStudent, IStudentNavigation,
40        IStudentPersonalEdit, IStudentClearanceEdit)
41    grok.provides(IStudent)
42
43    def __init__(self):
44        super(Student, self).__init__()
45        # The site doesn't exist in unit tests
46        try:
47            students = grok.getSite()['students']
48            self.student_id = generate_student_id(students,'?')
49        except TypeError:
50            self.student_id = u'Z654321'
51        self.password = None
52       
53        return
54
55    def loggerInfo(self, ob_class, comment=None):
56        target = self.__name__
57        return grok.getSite()['students'].logger_info(ob_class,target,comment)
58
59    @property
60    def state(self):
61        state = IWorkflowState(self).getState()
62        return state
63
64    @property
65    def history(self):
66        history = IObjectHistory(self)
67        return history
68
69    def getStudent(self):
70        return self
71
72    @property
73    def certificate(self):
74        cert = getattr(self.get('studycourse', None), 'certificate', None)
75        return cert
76
77    @property
78    def current_session(self):
79        cert = getattr(self.get('studycourse', None), 'current_session', None)
80        return cert
81
82# Set all attributes of Student required in IStudent as field
83# properties. Doing this, we do not have to set initial attributes
84# ourselves and as a bonus we get free validation when an attribute is
85# set.
86Student = attrs_to_fields(Student)
87
88class StudentFactory(grok.GlobalUtility):
89    """A factory for students.
90    """
91    grok.implements(IFactory)
92    grok.name(u'waeup.Student')
93    title = u"Create a new student.",
94    description = u"This factory instantiates new student instances."
95
96    def __call__(self, *args, **kw):
97        return Student()
98
99    def getInterfaces(self):
100        return implementedBy(Student)
101
102@grok.subscribe(IStudent, grok.IObjectAddedEvent)
103def handle_student_added(student, event):
104    """If a student is added all subcontainers are automatically added
105    and the transition create is fired. The latter produces a logging message.
106    """
107    student.clearance_locked = True
108    studycourse = StudentStudyCourse()
109    student['studycourse'] = studycourse
110    payments = StudentPaymentsContainer()
111    student['payments'] = payments
112    accommodation = StudentAccommodation()
113    student['accommodation'] = accommodation
114    # Assign global student role for new student
115    account = IUserAccount(student)
116    account.roles = ['waeup.Student']
117    # Assign local StudentRecordOwner role
118    role_manager = IPrincipalRoleManager(student)
119    role_manager.assignRoleToPrincipal(
120        'waeup.local.StudentRecordOwner', student.student_id)
121    IWorkflowInfo(student).fireTransition('create')
122    return
123
124@grok.subscribe(IStudent, grok.IObjectRemovedEvent)
125def handle_student_removed(student, event):
126    """If a student is removed a message is logged.
127    """
128    comment = 'Student record removed'
129    target = student.student_id
130    # In some tests we don't have a principal
131    try:
132        user = get_current_principal().id
133    except (TypeError, AttributeError):
134        return
135    grok.getSite()['students'].logger.info('%s - %s - %s' % (
136        user, target, comment))
137    return
Note: See TracBrowser for help on using the repository browser.