[7191] | 1 | ## $Id: student.py 7359 2011-12-16 07:32:42Z henrik $ |
---|
| 2 | ## |
---|
[6621] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
| 4 | ## This program is free software; you can redistribute it and/or modify |
---|
| 5 | ## it under the terms of the GNU General Public License as published by |
---|
| 6 | ## the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | ## (at your option) any later version. |
---|
| 8 | ## |
---|
| 9 | ## This program is distributed in the hope that it will be useful, |
---|
| 10 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | ## GNU General Public License for more details. |
---|
| 13 | ## |
---|
| 14 | ## You should have received a copy of the GNU General Public License |
---|
| 15 | ## along with this program; if not, write to the Free Software |
---|
| 16 | ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
| 17 | ## |
---|
| 18 | """ |
---|
| 19 | Container for the various objects owned by students. |
---|
| 20 | """ |
---|
[7097] | 21 | import os |
---|
[6621] | 22 | import grok |
---|
[7359] | 23 | from zope.component import getUtility |
---|
[6749] | 24 | from zope.component.interfaces import IFactory |
---|
[6621] | 25 | from zope.interface import implementedBy |
---|
[6838] | 26 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo |
---|
| 27 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
[7097] | 28 | from waeup.sirp.interfaces import ( |
---|
[7359] | 29 | IObjectHistory, IUserAccount, IFileStoreNameChooser, IFileStoreHandler, |
---|
| 30 | ISIRPUtils) |
---|
[7321] | 31 | from waeup.sirp.image import SIRPImageFile |
---|
[7097] | 32 | from waeup.sirp.imagestorage import DefaultFileStoreHandler |
---|
[6694] | 33 | from waeup.sirp.students.interfaces import ( |
---|
[6764] | 34 | IStudent, IStudentNavigation, IStudentClearanceEdit, |
---|
[6695] | 35 | IStudentPersonalEdit) |
---|
[6838] | 36 | from waeup.sirp.students.studycourse import StudentStudyCourse |
---|
[6859] | 37 | from waeup.sirp.students.payments import StudentPaymentsContainer |
---|
[6838] | 38 | from waeup.sirp.students.accommodation import StudentAccommodation |
---|
[6836] | 39 | from waeup.sirp.utils.helpers import attrs_to_fields, get_current_principal |
---|
[6651] | 40 | from waeup.sirp.students.utils import generate_student_id |
---|
[6621] | 41 | |
---|
| 42 | class Student(grok.Container): |
---|
| 43 | """This is a student container for the various objects |
---|
| 44 | owned by students. |
---|
| 45 | """ |
---|
[6694] | 46 | grok.implements(IStudent, IStudentNavigation, |
---|
[6767] | 47 | IStudentPersonalEdit, IStudentClearanceEdit) |
---|
[6621] | 48 | grok.provides(IStudent) |
---|
| 49 | |
---|
| 50 | def __init__(self): |
---|
| 51 | super(Student, self).__init__() |
---|
[6749] | 52 | # The site doesn't exist in unit tests |
---|
[6652] | 53 | try: |
---|
[6749] | 54 | students = grok.getSite()['students'] |
---|
| 55 | self.student_id = generate_student_id(students,'?') |
---|
| 56 | except TypeError: |
---|
[6666] | 57 | self.student_id = u'Z654321' |
---|
[6699] | 58 | self.password = None |
---|
[6621] | 59 | return |
---|
| 60 | |
---|
[6637] | 61 | def loggerInfo(self, ob_class, comment=None): |
---|
| 62 | target = self.__name__ |
---|
| 63 | return grok.getSite()['students'].logger_info(ob_class,target,comment) |
---|
| 64 | |
---|
| 65 | @property |
---|
[7357] | 66 | def fullname(self): |
---|
| 67 | middlename = getattr(self, 'middlename', None) |
---|
[7359] | 68 | sirp_utils = getUtility(ISIRPUtils) |
---|
| 69 | return sirp_utils.fullname(self.firstname, self.lastname, middlename) |
---|
[7357] | 70 | |
---|
| 71 | @property |
---|
[6637] | 72 | def state(self): |
---|
| 73 | state = IWorkflowState(self).getState() |
---|
| 74 | return state |
---|
| 75 | |
---|
| 76 | @property |
---|
| 77 | def history(self): |
---|
| 78 | history = IObjectHistory(self) |
---|
| 79 | return history |
---|
| 80 | |
---|
[6642] | 81 | def getStudent(self): |
---|
| 82 | return self |
---|
| 83 | |
---|
[6814] | 84 | @property |
---|
[7203] | 85 | def certcode(self): |
---|
[6814] | 86 | cert = getattr(self.get('studycourse', None), 'certificate', None) |
---|
[7203] | 87 | if cert is not None: |
---|
| 88 | return cert.code |
---|
| 89 | return |
---|
[6814] | 90 | |
---|
[7062] | 91 | @property |
---|
[7203] | 92 | def faccode(self): |
---|
| 93 | cert = getattr(self.get('studycourse', None), 'certificate', None) |
---|
| 94 | if cert is not None: |
---|
| 95 | return cert.__parent__.__parent__.__parent__.code |
---|
| 96 | return |
---|
| 97 | |
---|
| 98 | @property |
---|
| 99 | def depcode(self): |
---|
| 100 | cert = getattr(self.get('studycourse', None), 'certificate', None) |
---|
| 101 | if cert is not None: |
---|
| 102 | return cert.__parent__.__parent__.code |
---|
| 103 | return |
---|
| 104 | |
---|
| 105 | @property |
---|
[7062] | 106 | def current_session(self): |
---|
| 107 | cert = getattr(self.get('studycourse', None), 'current_session', None) |
---|
| 108 | return cert |
---|
| 109 | |
---|
[6621] | 110 | # Set all attributes of Student required in IStudent as field |
---|
| 111 | # properties. Doing this, we do not have to set initial attributes |
---|
| 112 | # ourselves and as a bonus we get free validation when an attribute is |
---|
| 113 | # set. |
---|
| 114 | Student = attrs_to_fields(Student) |
---|
| 115 | |
---|
| 116 | class StudentFactory(grok.GlobalUtility): |
---|
| 117 | """A factory for students. |
---|
| 118 | """ |
---|
| 119 | grok.implements(IFactory) |
---|
| 120 | grok.name(u'waeup.Student') |
---|
| 121 | title = u"Create a new student.", |
---|
| 122 | description = u"This factory instantiates new student instances." |
---|
| 123 | |
---|
| 124 | def __call__(self, *args, **kw): |
---|
| 125 | return Student() |
---|
| 126 | |
---|
| 127 | def getInterfaces(self): |
---|
| 128 | return implementedBy(Student) |
---|
[6836] | 129 | |
---|
[6838] | 130 | @grok.subscribe(IStudent, grok.IObjectAddedEvent) |
---|
[6839] | 131 | def handle_student_added(student, event): |
---|
[6838] | 132 | """If a student is added all subcontainers are automatically added |
---|
| 133 | and the transition create is fired. The latter produces a logging message. |
---|
| 134 | """ |
---|
| 135 | student.clearance_locked = True |
---|
| 136 | studycourse = StudentStudyCourse() |
---|
| 137 | student['studycourse'] = studycourse |
---|
[6859] | 138 | payments = StudentPaymentsContainer() |
---|
[6838] | 139 | student['payments'] = payments |
---|
| 140 | accommodation = StudentAccommodation() |
---|
| 141 | student['accommodation'] = accommodation |
---|
| 142 | # Assign global student role for new student |
---|
| 143 | account = IUserAccount(student) |
---|
| 144 | account.roles = ['waeup.Student'] |
---|
| 145 | # Assign local StudentRecordOwner role |
---|
| 146 | role_manager = IPrincipalRoleManager(student) |
---|
| 147 | role_manager.assignRoleToPrincipal( |
---|
| 148 | 'waeup.local.StudentRecordOwner', student.student_id) |
---|
| 149 | IWorkflowInfo(student).fireTransition('create') |
---|
| 150 | return |
---|
| 151 | |
---|
[6836] | 152 | @grok.subscribe(IStudent, grok.IObjectRemovedEvent) |
---|
[6839] | 153 | def handle_student_removed(student, event): |
---|
[6836] | 154 | """If a student is removed a message is logged. |
---|
| 155 | """ |
---|
| 156 | comment = 'Student record removed' |
---|
| 157 | target = student.student_id |
---|
[6841] | 158 | # In some tests we don't have a principal |
---|
| 159 | try: |
---|
| 160 | user = get_current_principal().id |
---|
| 161 | except (TypeError, AttributeError): |
---|
| 162 | return |
---|
[7212] | 163 | try: |
---|
| 164 | grok.getSite()['students'].logger.info('%s - %s - %s' % ( |
---|
| 165 | user, target, comment)) |
---|
| 166 | except KeyError: |
---|
| 167 | # If we delete an entire university instance there won't be |
---|
| 168 | # a students subcontainer |
---|
| 169 | return |
---|
[7097] | 170 | return |
---|
| 171 | |
---|
| 172 | #: The file id marker for student files |
---|
| 173 | STUDENT_FILE_STORE_NAME = 'file-student' |
---|
| 174 | |
---|
| 175 | class StudentFileNameChooser(grok.Adapter): |
---|
[7099] | 176 | """A file id chooser for :class:`Student` objects. |
---|
[7097] | 177 | |
---|
[7099] | 178 | `context` is an :class:`Student` instance. |
---|
[7097] | 179 | |
---|
[7099] | 180 | The :class:`StudentImageNameChooser` can build/check file ids for |
---|
| 181 | :class:`Student` objects suitable for use with |
---|
[7097] | 182 | :class:`ExtFileStore` instances. The delivered file_id contains |
---|
[7099] | 183 | the file id marker for :class:`Student` object and the student id |
---|
| 184 | of the context student. |
---|
[7097] | 185 | |
---|
| 186 | This chooser is registered as an adapter providing |
---|
| 187 | :class:`waeup.sirp.interfaces.IFileStoreNameChooser`. |
---|
| 188 | |
---|
| 189 | File store name choosers like this one are only convenience |
---|
[7099] | 190 | components to ease the task of creating file ids for student |
---|
[7097] | 191 | objects. You are nevertheless encouraged to use them instead of |
---|
[7099] | 192 | manually setting up filenames for students. |
---|
[7097] | 193 | |
---|
| 194 | .. seealso:: :mod:`waeup.sirp.imagestorage` |
---|
| 195 | |
---|
| 196 | """ |
---|
| 197 | grok.context(IStudent) |
---|
| 198 | grok.implements(IFileStoreNameChooser) |
---|
| 199 | |
---|
| 200 | def checkName(self, name=None, attr=None): |
---|
| 201 | """Check whether the given name is a valid file id for the context. |
---|
| 202 | |
---|
| 203 | Returns ``True`` only if `name` equals the result of |
---|
| 204 | :meth:`chooseName`. |
---|
| 205 | |
---|
| 206 | """ |
---|
| 207 | return name == self.chooseName() |
---|
| 208 | |
---|
[7106] | 209 | def chooseName(self, attr, name=None): |
---|
[7097] | 210 | """Get a valid file id for student context. |
---|
| 211 | |
---|
| 212 | *Example:* |
---|
| 213 | |
---|
[7105] | 214 | For a student with student id ``'A123456'`` and |
---|
[7106] | 215 | with attr ``'nice_image.jpeg'`` stored in |
---|
[7097] | 216 | the students container this chooser would create: |
---|
| 217 | |
---|
[7106] | 218 | ``'__file-student__students/A/A123456/nice_image_A123456.jpeg'`` |
---|
[7097] | 219 | |
---|
| 220 | meaning that the nice image of this applicant would be |
---|
| 221 | stored in the site-wide file storage in path: |
---|
| 222 | |
---|
[7106] | 223 | ``students/A/A123456/nice_image_A123456.jpeg`` |
---|
[7097] | 224 | |
---|
| 225 | """ |
---|
[7106] | 226 | basename, ext = os.path.splitext(attr) |
---|
[7099] | 227 | stud_id = self.context.student_id |
---|
[7106] | 228 | marked_filename = '__%s__%s/%s/%s_%s%s' % ( |
---|
| 229 | STUDENT_FILE_STORE_NAME, stud_id[0], stud_id, basename, stud_id, ext) |
---|
[7097] | 230 | return marked_filename |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | class StudentFileStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility): |
---|
| 234 | """Student specific file handling. |
---|
| 235 | |
---|
| 236 | This handler knows in which path in a filestore to store student |
---|
| 237 | files and how to turn this kind of data into some (browsable) |
---|
| 238 | file object. |
---|
| 239 | |
---|
| 240 | It is called from the global file storage, when it wants to |
---|
| 241 | get/store a file with a file id starting with |
---|
| 242 | ``__file-student__`` (the marker string for student files). |
---|
| 243 | |
---|
| 244 | Like each other file store handler it does not handle the files |
---|
| 245 | really (this is done by the global file store) but only computes |
---|
| 246 | paths and things like this. |
---|
| 247 | """ |
---|
| 248 | grok.implements(IFileStoreHandler) |
---|
| 249 | grok.name(STUDENT_FILE_STORE_NAME) |
---|
| 250 | |
---|
| 251 | def pathFromFileID(self, store, root, file_id): |
---|
[7099] | 252 | """All student files are put in directory ``students``. |
---|
[7097] | 253 | """ |
---|
| 254 | marker, filename, basename, ext = store.extractMarker(file_id) |
---|
[7122] | 255 | sub_root = os.path.join(root, 'students') |
---|
| 256 | return super(StudentFileStoreHandler, self).pathFromFileID( |
---|
| 257 | store, sub_root, basename) |
---|
[7097] | 258 | |
---|
| 259 | def createFile(self, store, root, filename, file_id, file): |
---|
| 260 | """Create a browsable file-like object. |
---|
| 261 | """ |
---|
[7122] | 262 | # call super method to ensure that any old files with |
---|
| 263 | # different filename extension are deleted. |
---|
| 264 | file, path, file_obj = super( |
---|
| 265 | StudentFileStoreHandler, self).createFile( |
---|
| 266 | store, root, filename, file_id, file) |
---|
[7321] | 267 | return file, path, SIRPImageFile( |
---|
[7122] | 268 | file_obj.filename, file_obj.data) |
---|