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 | """ |
---|
17 | Container for the various objects owned by students. |
---|
18 | """ |
---|
19 | import grok |
---|
20 | from grok import index |
---|
21 | from zope.component.interfaces import IFactory |
---|
22 | from zope.interface import implementedBy |
---|
23 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo |
---|
24 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
25 | from waeup.sirp.interfaces import IObjectHistory, IUserAccount |
---|
26 | from waeup.sirp.students.interfaces import ( |
---|
27 | IStudent, IStudentNavigation, IStudentClearanceEdit, |
---|
28 | IStudentPersonalEdit) |
---|
29 | from waeup.sirp.students.studycourse import StudentStudyCourse |
---|
30 | from waeup.sirp.students.payments import StudentPaymentsContainer |
---|
31 | from waeup.sirp.students.accommodation import StudentAccommodation |
---|
32 | from waeup.sirp.utils.helpers import attrs_to_fields, get_current_principal |
---|
33 | from waeup.sirp.students.utils import generate_student_id |
---|
34 | |
---|
35 | class 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 | # Set all attributes of Student required in IStudent as field |
---|
78 | # properties. Doing this, we do not have to set initial attributes |
---|
79 | # ourselves and as a bonus we get free validation when an attribute is |
---|
80 | # set. |
---|
81 | Student = attrs_to_fields(Student) |
---|
82 | |
---|
83 | class StudentFactory(grok.GlobalUtility): |
---|
84 | """A factory for students. |
---|
85 | """ |
---|
86 | grok.implements(IFactory) |
---|
87 | grok.name(u'waeup.Student') |
---|
88 | title = u"Create a new student.", |
---|
89 | description = u"This factory instantiates new student instances." |
---|
90 | |
---|
91 | def __call__(self, *args, **kw): |
---|
92 | return Student() |
---|
93 | |
---|
94 | def getInterfaces(self): |
---|
95 | return implementedBy(Student) |
---|
96 | |
---|
97 | @grok.subscribe(IStudent, grok.IObjectAddedEvent) |
---|
98 | def handle_student_added(student, event): |
---|
99 | """If a student is added all subcontainers are automatically added |
---|
100 | and the transition create is fired. The latter produces a logging message. |
---|
101 | """ |
---|
102 | student.clearance_locked = True |
---|
103 | studycourse = StudentStudyCourse() |
---|
104 | student['studycourse'] = studycourse |
---|
105 | payments = StudentPaymentsContainer() |
---|
106 | student['payments'] = payments |
---|
107 | accommodation = StudentAccommodation() |
---|
108 | student['accommodation'] = accommodation |
---|
109 | # Assign global student role for new student |
---|
110 | account = IUserAccount(student) |
---|
111 | account.roles = ['waeup.Student'] |
---|
112 | # Assign local StudentRecordOwner role |
---|
113 | role_manager = IPrincipalRoleManager(student) |
---|
114 | role_manager.assignRoleToPrincipal( |
---|
115 | 'waeup.local.StudentRecordOwner', student.student_id) |
---|
116 | IWorkflowInfo(student).fireTransition('create') |
---|
117 | return |
---|
118 | |
---|
119 | @grok.subscribe(IStudent, grok.IObjectRemovedEvent) |
---|
120 | def handle_student_removed(student, event): |
---|
121 | """If a student is removed a message is logged. |
---|
122 | """ |
---|
123 | comment = 'Student record removed' |
---|
124 | target = student.student_id |
---|
125 | # In some tests we don't have a principal |
---|
126 | try: |
---|
127 | user = get_current_principal().id |
---|
128 | except (TypeError, AttributeError): |
---|
129 | return |
---|
130 | grok.getSite()['students'].logger.info('%s - %s - %s' % ( |
---|
131 | user, target, comment)) |
---|
132 | return |
---|