[6621] | 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 | Containers for students. |
---|
| 18 | """ |
---|
| 19 | import grok |
---|
| 20 | import os |
---|
| 21 | from zope.component.interfaces import IFactory |
---|
[6681] | 22 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
[6830] | 23 | from hurry.workflow.interfaces import IWorkflowInfo |
---|
[6679] | 24 | from waeup.sirp.interfaces import IUserAccount |
---|
[6633] | 25 | from waeup.sirp.students.interfaces import ( |
---|
[6635] | 26 | IStudentsContainer, IStudent, IStudentPayments, IStudentAccommodation) |
---|
[6633] | 27 | from waeup.sirp.students.studycourse import StudentStudyCourse |
---|
[6635] | 28 | from waeup.sirp.students.payments import StudentPayments |
---|
| 29 | from waeup.sirp.students.accommodation import StudentAccommodation |
---|
[6637] | 30 | from waeup.sirp.utils.helpers import get_current_principal |
---|
| 31 | from waeup.sirp.utils.logger import Logger |
---|
[6621] | 32 | |
---|
[6637] | 33 | class StudentsContainer(grok.Container, Logger): |
---|
[6621] | 34 | """ |
---|
| 35 | The node containing the student models |
---|
| 36 | """ |
---|
| 37 | |
---|
| 38 | grok.implements(IStudentsContainer) |
---|
| 39 | |
---|
| 40 | def archive(self, id=None): |
---|
| 41 | raise NotImplementedError() |
---|
| 42 | |
---|
| 43 | def clear(self, id=None, archive=True): |
---|
| 44 | raise NotImplementedError() |
---|
| 45 | |
---|
[6633] | 46 | def addStudent(self, student): |
---|
| 47 | """Add a student with subcontainers. |
---|
| 48 | """ |
---|
| 49 | if not IStudent.providedBy(student): |
---|
[6663] | 50 | raise TypeError( |
---|
| 51 | 'StudentsContainers contain only IStudent instances') |
---|
[6695] | 52 | student.clearance_locked = True |
---|
[6633] | 53 | self[student.student_id] = student |
---|
| 54 | studycourse = StudentStudyCourse() |
---|
| 55 | self[student.student_id]['studycourse'] = studycourse |
---|
[6635] | 56 | payments = StudentPayments() |
---|
| 57 | self[student.student_id]['payments'] = payments |
---|
| 58 | accommodation = StudentAccommodation() |
---|
| 59 | self[student.student_id]['accommodation'] = accommodation |
---|
[6681] | 60 | # Assign global student role for new student |
---|
[6679] | 61 | account = IUserAccount(student) |
---|
[6684] | 62 | account.roles = ['waeup.Student'] |
---|
[6681] | 63 | # Assign local StudentRecordOwner role |
---|
| 64 | role_manager = IPrincipalRoleManager(self[student.student_id]) |
---|
| 65 | role_manager.assignRoleToPrincipal( |
---|
| 66 | 'waeup.local.StudentRecordOwner', student.student_id) |
---|
[6652] | 67 | # Return student_id (only needed in tests) |
---|
[6830] | 68 | IWorkflowInfo(self[student.student_id]).fireTransition('create') |
---|
[6652] | 69 | return student.student_id |
---|
[6637] | 70 | |
---|
| 71 | logger_name = 'waeup.sirp.${sitename}.students' |
---|
| 72 | logger_filename = 'students.log' |
---|
| 73 | |
---|
[6644] | 74 | def logger_info(self, ob_class, target, comment=None): |
---|
[6637] | 75 | """Get the logger's info method. |
---|
| 76 | """ |
---|
| 77 | user = get_current_principal() |
---|
| 78 | if user is None: |
---|
| 79 | user = 'system' |
---|
| 80 | else: |
---|
| 81 | user = user.id |
---|
| 82 | self.logger.info('%s - %s - %s - %s' % ( |
---|
[6644] | 83 | user, ob_class, target, comment)) |
---|
[6637] | 84 | return |
---|
[6832] | 85 | |
---|
| 86 | @grok.subscribe(IStudent, grok.IObjectRemovedEvent) |
---|
| 87 | def removedStudentHandler(student, event): |
---|
| 88 | """If a student is removed a message is logged. |
---|
| 89 | """ |
---|
| 90 | comment = 'Student record removed' |
---|
| 91 | user = get_current_principal().id |
---|
| 92 | target = student.student_id |
---|
| 93 | grok.getSite()['students'].logger.info('%s - %s - %s' % ( |
---|
| 94 | user, target, comment)) |
---|