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

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

Rename StudentPayments? to StudentPaymentsContainer?.

  • Property svn:keywords set to Id
File size: 4.7 KB
RevLine 
[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"""
17Container for the various objects owned by students.
18"""
19import grok
20from grok import index
[6749]21from zope.component.interfaces import IFactory
[6621]22from zope.interface import implementedBy
[6838]23from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo
24from zope.securitypolicy.interfaces import IPrincipalRoleManager
25from waeup.sirp.interfaces import IObjectHistory, IUserAccount
[6694]26from waeup.sirp.students.interfaces import (
[6764]27    IStudent, IStudentNavigation, IStudentClearanceEdit,
[6695]28    IStudentPersonalEdit)
[6838]29from waeup.sirp.students.studycourse import StudentStudyCourse
[6859]30from waeup.sirp.students.payments import StudentPaymentsContainer
[6838]31from waeup.sirp.students.accommodation import StudentAccommodation
[6836]32from waeup.sirp.utils.helpers import attrs_to_fields, get_current_principal
[6651]33from waeup.sirp.students.utils import generate_student_id
[6621]34
35class Student(grok.Container):
36    """This is a student container for the various objects
37    owned by students.
38    """
[6694]39    grok.implements(IStudent, IStudentNavigation,
[6767]40        IStudentPersonalEdit, IStudentClearanceEdit)
[6621]41    grok.provides(IStudent)
42
43    def __init__(self):
44        super(Student, self).__init__()
[6749]45        # The site doesn't exist in unit tests
[6652]46        try:
[6749]47            students = grok.getSite()['students']
48            self.student_id = generate_student_id(students,'?')
49        except TypeError:
[6666]50            self.student_id = u'Z654321'
[6699]51        self.password = None
[6830]52       
[6621]53        return
54
[6637]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
[6642]69    def getStudent(self):
70        return self
71
[6814]72    @property
73    def certificate(self):
74        cert = getattr(self.get('studycourse', None), 'certificate', None)
75        return cert
76
[6621]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.
81Student = attrs_to_fields(Student)
82
83class 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)
[6836]96
[6838]97@grok.subscribe(IStudent, grok.IObjectAddedEvent)
[6839]98def handle_student_added(student, event):
[6838]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
[6859]105    payments = StudentPaymentsContainer()
[6838]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
[6836]119@grok.subscribe(IStudent, grok.IObjectRemovedEvent)
[6839]120def handle_student_removed(student, event):
[6836]121    """If a student is removed a message is logged.
122    """
123    comment = 'Student record removed'
124    target = student.student_id
[6841]125    # In some tests we don't have a principal
126    try:
127        user = get_current_principal().id
128    except (TypeError, AttributeError):
129        return
[6836]130    grok.getSite()['students'].logger.info('%s - %s - %s' % (
[6838]131        user, target, comment))
132    return
Note: See TracBrowser for help on using the repository browser.