[7191] | 1 | ## $Id: student.py 15163 2018-09-23 05:05:04Z 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 |
---|
[8448] | 22 | import re |
---|
[8403] | 23 | import shutil |
---|
[6621] | 24 | import grok |
---|
[9334] | 25 | from datetime import datetime, timedelta |
---|
[7949] | 26 | from hurry.workflow.interfaces import IWorkflowState, IWorkflowInfo |
---|
[9334] | 27 | from zope.password.interfaces import IPasswordManager |
---|
[8323] | 28 | from zope.component import getUtility, createObject |
---|
[6749] | 29 | from zope.component.interfaces import IFactory |
---|
[6621] | 30 | from zope.interface import implementedBy |
---|
[6838] | 31 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
[9137] | 32 | from zope.schema.interfaces import ConstraintNotSatisfied |
---|
[8403] | 33 | |
---|
[7949] | 34 | from waeup.kofa.image import KofaImageFile |
---|
| 35 | from waeup.kofa.imagestorage import DefaultFileStoreHandler |
---|
[7811] | 36 | from waeup.kofa.interfaces import ( |
---|
[7359] | 37 | IObjectHistory, IUserAccount, IFileStoreNameChooser, IFileStoreHandler, |
---|
[9517] | 38 | IKofaUtils, registration_states_vocab, IExtFileStore, |
---|
[14169] | 39 | CREATED, ADMITTED, CLEARANCE, PAID, REGISTERED, VALIDATED, RETURNING, |
---|
[15163] | 40 | GRADUATED, TRANSVAL, TRANSREL) |
---|
[7949] | 41 | from waeup.kofa.students.accommodation import StudentAccommodation |
---|
[8411] | 42 | from waeup.kofa.students.interfaces import ( |
---|
[12104] | 43 | IStudent, IStudentNavigation, IStudentPersonalEdit, ICSVStudentExporter, |
---|
| 44 | IStudentsUtils) |
---|
[7949] | 45 | from waeup.kofa.students.payments import StudentPaymentsContainer |
---|
| 46 | from waeup.kofa.students.utils import generate_student_id |
---|
[8403] | 47 | from waeup.kofa.utils.helpers import attrs_to_fields, now, copy_filesystem_tree |
---|
[6621] | 48 | |
---|
[8448] | 49 | RE_STUDID_NON_NUM = re.compile('[^\d]+') |
---|
| 50 | |
---|
[6621] | 51 | class Student(grok.Container): |
---|
| 52 | """This is a student container for the various objects |
---|
| 53 | owned by students. |
---|
| 54 | """ |
---|
[9563] | 55 | grok.implements(IStudent, IStudentNavigation, IStudentPersonalEdit) |
---|
[6621] | 56 | grok.provides(IStudent) |
---|
| 57 | |
---|
[9334] | 58 | temp_password_minutes = 10 |
---|
| 59 | |
---|
[6621] | 60 | def __init__(self): |
---|
| 61 | super(Student, self).__init__() |
---|
[6749] | 62 | # The site doesn't exist in unit tests |
---|
[6652] | 63 | try: |
---|
[8481] | 64 | self.student_id = generate_student_id() |
---|
[6749] | 65 | except TypeError: |
---|
[6666] | 66 | self.student_id = u'Z654321' |
---|
[6699] | 67 | self.password = None |
---|
[9334] | 68 | self.temp_password = None |
---|
[6621] | 69 | return |
---|
| 70 | |
---|
[9334] | 71 | def setTempPassword(self, user, password): |
---|
| 72 | """Set a temporary password (LDAP-compatible) SSHA encoded for |
---|
| 73 | officers. |
---|
| 74 | """ |
---|
| 75 | passwordmanager = getUtility(IPasswordManager, 'SSHA') |
---|
| 76 | self.temp_password = {} |
---|
| 77 | self.temp_password[ |
---|
| 78 | 'password'] = passwordmanager.encodePassword(password) |
---|
| 79 | self.temp_password['user'] = user |
---|
| 80 | self.temp_password['timestamp'] = datetime.utcnow() # offset-naive datetime |
---|
| 81 | |
---|
| 82 | def getTempPassword(self): |
---|
| 83 | """Check if a temporary password has been set and if it |
---|
| 84 | is not expired. |
---|
| 85 | |
---|
| 86 | Return the temporary password if valid, |
---|
| 87 | None otherwise. Unset the temporary password if expired. |
---|
| 88 | """ |
---|
| 89 | temp_password_dict = getattr(self, 'temp_password', None) |
---|
| 90 | if temp_password_dict is not None: |
---|
| 91 | delta = timedelta(minutes=self.temp_password_minutes) |
---|
| 92 | now = datetime.utcnow() |
---|
| 93 | if now < temp_password_dict.get('timestamp') + delta: |
---|
| 94 | return temp_password_dict.get('password') |
---|
| 95 | else: |
---|
| 96 | # Unset temporary password if expired |
---|
| 97 | self.temp_password = None |
---|
| 98 | return None |
---|
| 99 | |
---|
[8735] | 100 | def writeLogMessage(self, view, message): |
---|
| 101 | ob_class = view.__implemented__.__name__.replace('waeup.kofa.','') |
---|
[8737] | 102 | self.__parent__.logger.info( |
---|
| 103 | '%s - %s - %s' % (ob_class, self.__name__, message)) |
---|
| 104 | return |
---|
[6637] | 105 | |
---|
| 106 | @property |
---|
[7364] | 107 | def display_fullname(self): |
---|
[7357] | 108 | middlename = getattr(self, 'middlename', None) |
---|
[7819] | 109 | kofa_utils = getUtility(IKofaUtils) |
---|
[7811] | 110 | return kofa_utils.fullname(self.firstname, self.lastname, middlename) |
---|
[7357] | 111 | |
---|
| 112 | @property |
---|
[7364] | 113 | def fullname(self): |
---|
| 114 | middlename = getattr(self, 'middlename', None) |
---|
| 115 | if middlename: |
---|
| 116 | return '%s-%s-%s' % (self.firstname.lower(), |
---|
| 117 | middlename.lower(), self.lastname.lower()) |
---|
| 118 | else: |
---|
| 119 | return '%s-%s' % (self.firstname.lower(), self.lastname.lower()) |
---|
| 120 | |
---|
| 121 | @property |
---|
[6637] | 122 | def state(self): |
---|
| 123 | state = IWorkflowState(self).getState() |
---|
| 124 | return state |
---|
| 125 | |
---|
| 126 | @property |
---|
[7677] | 127 | def translated_state(self): |
---|
[13080] | 128 | try: |
---|
| 129 | state = registration_states_vocab.getTermByToken( |
---|
| 130 | self.state).title |
---|
| 131 | except LookupError: # in unit tests |
---|
| 132 | return |
---|
[7677] | 133 | return state |
---|
| 134 | |
---|
| 135 | @property |
---|
[6637] | 136 | def history(self): |
---|
| 137 | history = IObjectHistory(self) |
---|
| 138 | return history |
---|
| 139 | |
---|
[8736] | 140 | @property |
---|
| 141 | def student(self): |
---|
[6642] | 142 | return self |
---|
| 143 | |
---|
[6814] | 144 | @property |
---|
[7203] | 145 | def certcode(self): |
---|
[6814] | 146 | cert = getattr(self.get('studycourse', None), 'certificate', None) |
---|
[7203] | 147 | if cert is not None: |
---|
| 148 | return cert.code |
---|
| 149 | return |
---|
[6814] | 150 | |
---|
[7062] | 151 | @property |
---|
[7203] | 152 | def faccode(self): |
---|
| 153 | cert = getattr(self.get('studycourse', None), 'certificate', None) |
---|
| 154 | if cert is not None: |
---|
| 155 | return cert.__parent__.__parent__.__parent__.code |
---|
| 156 | return |
---|
| 157 | |
---|
| 158 | @property |
---|
| 159 | def depcode(self): |
---|
| 160 | cert = getattr(self.get('studycourse', None), 'certificate', None) |
---|
| 161 | if cert is not None: |
---|
| 162 | return cert.__parent__.__parent__.code |
---|
| 163 | return |
---|
| 164 | |
---|
| 165 | @property |
---|
[7062] | 166 | def current_session(self): |
---|
[7948] | 167 | session = getattr( |
---|
| 168 | self.get('studycourse', None), 'current_session', None) |
---|
[7641] | 169 | return session |
---|
[7062] | 170 | |
---|
[7641] | 171 | @property |
---|
[9761] | 172 | def entry_session(self): |
---|
| 173 | session = getattr( |
---|
| 174 | self.get('studycourse', None), 'entry_session', None) |
---|
| 175 | return session |
---|
| 176 | |
---|
| 177 | @property |
---|
[10245] | 178 | def entry_mode(self): |
---|
| 179 | session = getattr( |
---|
| 180 | self.get('studycourse', None), 'entry_mode', None) |
---|
| 181 | return session |
---|
| 182 | |
---|
| 183 | @property |
---|
[9142] | 184 | def current_level(self): |
---|
| 185 | level = getattr( |
---|
| 186 | self.get('studycourse', None), 'current_level', None) |
---|
| 187 | return level |
---|
| 188 | |
---|
| 189 | @property |
---|
[9183] | 190 | def current_verdict(self): |
---|
| 191 | level = getattr( |
---|
| 192 | self.get('studycourse', None), 'current_verdict', None) |
---|
| 193 | return level |
---|
| 194 | |
---|
| 195 | @property |
---|
[7641] | 196 | def current_mode(self): |
---|
[7948] | 197 | certificate = getattr( |
---|
| 198 | self.get('studycourse', None), 'certificate', None) |
---|
[7641] | 199 | if certificate is not None: |
---|
| 200 | return certificate.study_mode |
---|
[8472] | 201 | return None |
---|
[7641] | 202 | |
---|
[8472] | 203 | @property |
---|
| 204 | def is_postgrad(self): |
---|
| 205 | is_postgrad = getattr( |
---|
| 206 | self.get('studycourse', None), 'is_postgrad', False) |
---|
| 207 | return is_postgrad |
---|
| 208 | |
---|
[9517] | 209 | @property |
---|
[10155] | 210 | def is_special_postgrad(self): |
---|
| 211 | is_special_postgrad = getattr( |
---|
| 212 | self.get('studycourse', None), 'is_special_postgrad', False) |
---|
| 213 | return is_special_postgrad |
---|
| 214 | |
---|
| 215 | @property |
---|
[11604] | 216 | def is_fresh(self): |
---|
| 217 | return self.current_session == self.entry_session |
---|
| 218 | |
---|
| 219 | @property |
---|
[9521] | 220 | def before_payment(self): |
---|
[14169] | 221 | non_fresh_states = (PAID, REGISTERED, VALIDATED, RETURNING, GRADUATED) |
---|
[11604] | 222 | if self.is_fresh and self.state not in non_fresh_states: |
---|
[9517] | 223 | return True |
---|
| 224 | return False |
---|
| 225 | |
---|
[9545] | 226 | @property |
---|
| 227 | def personal_data_expired(self): |
---|
[9569] | 228 | if self.state in (CREATED, ADMITTED,): |
---|
| 229 | return False |
---|
[9545] | 230 | now = datetime.utcnow() |
---|
[9569] | 231 | if self.personal_updated is None: |
---|
| 232 | return True |
---|
| 233 | days_ago = getattr(now - self.personal_updated, 'days') |
---|
| 234 | if days_ago > 180: |
---|
| 235 | return True |
---|
[9545] | 236 | return False |
---|
| 237 | |
---|
[10266] | 238 | @property |
---|
| 239 | def transcript_enabled(self): |
---|
| 240 | return True |
---|
| 241 | |
---|
[13103] | 242 | @property |
---|
[15163] | 243 | def studycourse_locked(self): |
---|
| 244 | return self.state in (GRADUATED, TRANSREL, TRANSVAL) |
---|
| 245 | |
---|
| 246 | @property |
---|
[13103] | 247 | def clearance_locked(self): |
---|
| 248 | return self.state != CLEARANCE |
---|
| 249 | |
---|
[9131] | 250 | def transfer(self, certificate, current_session=None, |
---|
[9136] | 251 | current_level=None, current_verdict=None, previous_verdict=None): |
---|
[9131] | 252 | """ Creates a new studycourse and backups the old one. |
---|
| 253 | """ |
---|
[9962] | 254 | newcourse = createObject(u'waeup.StudentStudyCourse') |
---|
[9137] | 255 | try: |
---|
[9962] | 256 | newcourse.certificate = certificate |
---|
| 257 | newcourse.entry_mode = 'transfer' |
---|
| 258 | newcourse.current_session = current_session |
---|
| 259 | newcourse.current_level = current_level |
---|
| 260 | newcourse.current_verdict = current_verdict |
---|
| 261 | newcourse.previous_verdict = previous_verdict |
---|
[9137] | 262 | except ConstraintNotSatisfied: |
---|
| 263 | return -1 |
---|
[9962] | 264 | oldcourse = self['studycourse'] |
---|
| 265 | if getattr(oldcourse, 'entry_session', None) is None or\ |
---|
| 266 | getattr(oldcourse, 'certificate', None) is None: |
---|
[9138] | 267 | return -2 |
---|
[9962] | 268 | newcourse.entry_session = oldcourse.entry_session |
---|
[9131] | 269 | # Students can be transferred only two times. |
---|
| 270 | if 'studycourse_1' in self.keys(): |
---|
| 271 | if 'studycourse_2' in self.keys(): |
---|
[9138] | 272 | return -3 |
---|
[9962] | 273 | self['studycourse_2'] = oldcourse |
---|
[9131] | 274 | else: |
---|
[9962] | 275 | self['studycourse_1'] = oldcourse |
---|
[9131] | 276 | del self['studycourse'] |
---|
[9962] | 277 | self['studycourse'] = newcourse |
---|
[9131] | 278 | self.__parent__.logger.info( |
---|
| 279 | '%s - transferred from %s to %s' % ( |
---|
[13028] | 280 | self.student_id, |
---|
| 281 | oldcourse.certificate.code, |
---|
| 282 | newcourse.certificate.code)) |
---|
[9137] | 283 | history = IObjectHistory(self) |
---|
| 284 | history.addMessage('Transferred from %s to %s' % ( |
---|
[9962] | 285 | oldcourse.certificate.code, newcourse.certificate.code)) |
---|
[9137] | 286 | return |
---|
[9131] | 287 | |
---|
[10054] | 288 | def revert_transfer(self): |
---|
| 289 | """ Revert previous transfer. |
---|
[9131] | 290 | |
---|
[10054] | 291 | """ |
---|
| 292 | if not self.has_key('studycourse_1'): |
---|
| 293 | return -1 |
---|
| 294 | del self['studycourse'] |
---|
| 295 | if 'studycourse_2' in self.keys(): |
---|
| 296 | studycourse = self['studycourse_2'] |
---|
| 297 | self['studycourse'] = studycourse |
---|
| 298 | del self['studycourse_2'] |
---|
| 299 | else: |
---|
| 300 | studycourse = self['studycourse_1'] |
---|
| 301 | self['studycourse'] = studycourse |
---|
| 302 | del self['studycourse_1'] |
---|
| 303 | self.__parent__.logger.info( |
---|
| 304 | '%s - transfer reverted' % self.student_id) |
---|
| 305 | history = IObjectHistory(self) |
---|
| 306 | history.addMessage('Transfer reverted') |
---|
| 307 | return |
---|
| 308 | |
---|
[6621] | 309 | # Set all attributes of Student required in IStudent as field |
---|
| 310 | # properties. Doing this, we do not have to set initial attributes |
---|
| 311 | # ourselves and as a bonus we get free validation when an attribute is |
---|
| 312 | # set. |
---|
| 313 | Student = attrs_to_fields(Student) |
---|
| 314 | |
---|
| 315 | class StudentFactory(grok.GlobalUtility): |
---|
| 316 | """A factory for students. |
---|
| 317 | """ |
---|
| 318 | grok.implements(IFactory) |
---|
| 319 | grok.name(u'waeup.Student') |
---|
| 320 | title = u"Create a new student.", |
---|
| 321 | description = u"This factory instantiates new student instances." |
---|
| 322 | |
---|
| 323 | def __call__(self, *args, **kw): |
---|
| 324 | return Student() |
---|
| 325 | |
---|
| 326 | def getInterfaces(self): |
---|
| 327 | return implementedBy(Student) |
---|
[6836] | 328 | |
---|
[6838] | 329 | @grok.subscribe(IStudent, grok.IObjectAddedEvent) |
---|
[6839] | 330 | def handle_student_added(student, event): |
---|
[6838] | 331 | """If a student is added all subcontainers are automatically added |
---|
[7948] | 332 | and the transition create is fired. The latter produces a logging |
---|
| 333 | message. |
---|
[6838] | 334 | """ |
---|
[8323] | 335 | studycourse = createObject(u'waeup.StudentStudyCourse') |
---|
[6838] | 336 | student['studycourse'] = studycourse |
---|
[6859] | 337 | payments = StudentPaymentsContainer() |
---|
[6838] | 338 | student['payments'] = payments |
---|
| 339 | accommodation = StudentAccommodation() |
---|
| 340 | student['accommodation'] = accommodation |
---|
| 341 | # Assign global student role for new student |
---|
| 342 | account = IUserAccount(student) |
---|
| 343 | account.roles = ['waeup.Student'] |
---|
| 344 | # Assign local StudentRecordOwner role |
---|
| 345 | role_manager = IPrincipalRoleManager(student) |
---|
| 346 | role_manager.assignRoleToPrincipal( |
---|
| 347 | 'waeup.local.StudentRecordOwner', student.student_id) |
---|
[8375] | 348 | if student.state is None: |
---|
[7513] | 349 | IWorkflowInfo(student).fireTransition('create') |
---|
[6838] | 350 | return |
---|
| 351 | |
---|
[8448] | 352 | def path_from_studid(student_id): |
---|
| 353 | """Convert a student_id into a predictable relative folder path. |
---|
| 354 | |
---|
| 355 | Used for storing files. |
---|
| 356 | |
---|
| 357 | Returns the name of folder in which files for a particular student |
---|
| 358 | should be stored. This is a relative path, relative to any general |
---|
[8452] | 359 | students folder with 5 zero-padded digits (except when student_id |
---|
| 360 | is overlong). |
---|
[8448] | 361 | |
---|
[8452] | 362 | We normally map 1,000 different student ids into one single |
---|
| 363 | path. For instance ``K1000000`` will give ``01000/K1000000``, |
---|
| 364 | ``K1234567`` will give ``0123/K1234567`` and ``K12345678`` will |
---|
| 365 | result in ``1234/K12345678``. |
---|
| 366 | |
---|
| 367 | For lower numbers < 10**6 we return the same path for up to 10,000 |
---|
| 368 | student_ids. So for instance ``KM123456`` will result in |
---|
| 369 | ``00120/KM123456`` (there will be no path starting with |
---|
| 370 | ``00123``). |
---|
| 371 | |
---|
| 372 | Works also with overlong number: here the leading zeros will be |
---|
| 373 | missing but ``K123456789`` will give reliably |
---|
| 374 | ``12345/K123456789`` as expected. |
---|
[8448] | 375 | """ |
---|
| 376 | # remove all non numeric characters and turn this into an int. |
---|
| 377 | num = int(RE_STUDID_NON_NUM.sub('', student_id)) |
---|
[8452] | 378 | if num < 10**6: |
---|
| 379 | # store max. of 10000 studs per folder and correct num for 5 digits |
---|
| 380 | num = num / 10000 * 10 |
---|
| 381 | else: |
---|
| 382 | # store max. of 1000 studs per folder |
---|
| 383 | num = num / 1000 |
---|
| 384 | # format folder name to have 5 zero-padded digits |
---|
| 385 | folder_name = u'%05d' % num |
---|
[8448] | 386 | folder_name = os.path.join(folder_name, student_id) |
---|
| 387 | return folder_name |
---|
| 388 | |
---|
[8403] | 389 | def move_student_files(student, del_dir): |
---|
| 390 | """Move files belonging to `student` to `del_dir`. |
---|
| 391 | |
---|
| 392 | `del_dir` is expected to be the path to the site-wide directory |
---|
| 393 | for storing backup data. |
---|
| 394 | |
---|
| 395 | The current files of the student are removed after backup. |
---|
| 396 | |
---|
| 397 | If the student has no associated files stored, nothing is done. |
---|
| 398 | """ |
---|
| 399 | stud_id = student.student_id |
---|
| 400 | |
---|
| 401 | src = getUtility(IExtFileStore).root |
---|
[8448] | 402 | src = os.path.join(src, 'students', path_from_studid(stud_id)) |
---|
[8403] | 403 | |
---|
[8448] | 404 | dst = os.path.join( |
---|
| 405 | del_dir, 'media', 'students', path_from_studid(stud_id)) |
---|
[8403] | 406 | |
---|
| 407 | if not os.path.isdir(src): |
---|
| 408 | # Do not copy if no files were stored. |
---|
| 409 | return |
---|
| 410 | if not os.path.exists(dst): |
---|
| 411 | os.makedirs(dst, 0755) |
---|
| 412 | copy_filesystem_tree(src, dst) |
---|
| 413 | shutil.rmtree(src) |
---|
| 414 | return |
---|
| 415 | |
---|
| 416 | def update_student_deletion_csvs(student, del_dir): |
---|
| 417 | """Update deletion CSV files with data from student. |
---|
| 418 | |
---|
| 419 | `del_dir` is expected to be the path to the site-wide directory |
---|
| 420 | for storing backup data. |
---|
| 421 | |
---|
| 422 | Each exporter available for students (and their many subobjects) |
---|
| 423 | is called in order to export CSV data of the given student to csv |
---|
| 424 | files in the site-wide backup directory for object data (see |
---|
| 425 | DataCenter). |
---|
| 426 | |
---|
| 427 | Each exported row is appended a column giving the deletion date |
---|
| 428 | (column `del_date`) as a UTC timestamp. |
---|
| 429 | """ |
---|
[12104] | 430 | |
---|
[12971] | 431 | STUDENT_BACKUP_EXPORTER_NAMES = getUtility( |
---|
| 432 | IStudentsUtils).STUDENT_BACKUP_EXPORTER_NAMES |
---|
[12104] | 433 | |
---|
[12971] | 434 | for name in STUDENT_BACKUP_EXPORTER_NAMES: |
---|
[8411] | 435 | exporter = getUtility(ICSVStudentExporter, name=name) |
---|
| 436 | csv_data = exporter.export_student(student) |
---|
[8403] | 437 | csv_data = csv_data.split('\r\n') |
---|
| 438 | |
---|
| 439 | # append a deletion timestamp on each data row |
---|
| 440 | timestamp = str(now().replace(microsecond=0)) # store UTC timestamp |
---|
| 441 | for num, row in enumerate(csv_data[1:-1]): |
---|
| 442 | csv_data[num+1] = csv_data[num+1] + ',' + timestamp |
---|
| 443 | csv_path = os.path.join(del_dir, '%s.csv' % name) |
---|
| 444 | |
---|
| 445 | # write data to CSV file |
---|
| 446 | if not os.path.exists(csv_path): |
---|
| 447 | # create new CSV file (including header line) |
---|
| 448 | csv_data[0] = csv_data[0] + ',del_date' |
---|
| 449 | open(csv_path, 'wb').write('\r\n'.join(csv_data)) |
---|
| 450 | else: |
---|
| 451 | # append existing CSV file (omitting headerline) |
---|
| 452 | open(csv_path, 'a').write('\r\n'.join(csv_data[1:])) |
---|
| 453 | return |
---|
| 454 | |
---|
[6836] | 455 | @grok.subscribe(IStudent, grok.IObjectRemovedEvent) |
---|
[6839] | 456 | def handle_student_removed(student, event): |
---|
[8403] | 457 | """If a student is removed a message is logged and data is put |
---|
| 458 | into a backup location. |
---|
| 459 | |
---|
| 460 | The data of the removed student is appended to CSV files in local |
---|
| 461 | datacenter and any existing external files (passport images, etc.) |
---|
| 462 | are copied over to this location as well. |
---|
| 463 | |
---|
| 464 | Documents in the file storage refering to the given student are |
---|
| 465 | removed afterwards (if they exist). Please make no assumptions |
---|
| 466 | about how the deletion takes place. Files might be deleted |
---|
| 467 | individually (leaving the students file directory intact) or the |
---|
| 468 | whole student directory might be deleted completely. |
---|
| 469 | |
---|
| 470 | All CSV rows created/appended contain a timestamp with the |
---|
| 471 | datetime of removal in an additional `del_date` column. |
---|
| 472 | |
---|
| 473 | XXX: blocking of used student_ids yet not implemented. |
---|
[6836] | 474 | """ |
---|
| 475 | comment = 'Student record removed' |
---|
| 476 | target = student.student_id |
---|
[6841] | 477 | try: |
---|
[8403] | 478 | site = grok.getSite() |
---|
| 479 | site['students'].logger.info('%s - %s' % ( |
---|
[7652] | 480 | target, comment)) |
---|
[7212] | 481 | except KeyError: |
---|
| 482 | # If we delete an entire university instance there won't be |
---|
| 483 | # a students subcontainer |
---|
| 484 | return |
---|
[8403] | 485 | |
---|
| 486 | del_dir = site['datacenter'].deleted_path |
---|
| 487 | |
---|
| 488 | # save files of the student |
---|
| 489 | move_student_files(student, del_dir) |
---|
| 490 | |
---|
| 491 | # update CSV files |
---|
| 492 | update_student_deletion_csvs(student, del_dir) |
---|
[13224] | 493 | |
---|
| 494 | # remove global role |
---|
| 495 | role_manager = IPrincipalRoleManager(grok.getSite()) |
---|
| 496 | role_manager.unsetRoleForPrincipal('waeup.Student', student.student_id) |
---|
[7097] | 497 | return |
---|
| 498 | |
---|
| 499 | #: The file id marker for student files |
---|
| 500 | STUDENT_FILE_STORE_NAME = 'file-student' |
---|
| 501 | |
---|
| 502 | class StudentFileNameChooser(grok.Adapter): |
---|
[7099] | 503 | """A file id chooser for :class:`Student` objects. |
---|
[7097] | 504 | |
---|
[7099] | 505 | `context` is an :class:`Student` instance. |
---|
[7097] | 506 | |
---|
[7099] | 507 | The :class:`StudentImageNameChooser` can build/check file ids for |
---|
| 508 | :class:`Student` objects suitable for use with |
---|
[7097] | 509 | :class:`ExtFileStore` instances. The delivered file_id contains |
---|
[7099] | 510 | the file id marker for :class:`Student` object and the student id |
---|
| 511 | of the context student. |
---|
[7097] | 512 | |
---|
| 513 | This chooser is registered as an adapter providing |
---|
[7811] | 514 | :class:`waeup.kofa.interfaces.IFileStoreNameChooser`. |
---|
[7097] | 515 | |
---|
| 516 | File store name choosers like this one are only convenience |
---|
[7099] | 517 | components to ease the task of creating file ids for student |
---|
[7097] | 518 | objects. You are nevertheless encouraged to use them instead of |
---|
[7099] | 519 | manually setting up filenames for students. |
---|
[7097] | 520 | |
---|
[7811] | 521 | .. seealso:: :mod:`waeup.kofa.imagestorage` |
---|
[7097] | 522 | |
---|
| 523 | """ |
---|
| 524 | grok.context(IStudent) |
---|
| 525 | grok.implements(IFileStoreNameChooser) |
---|
| 526 | |
---|
| 527 | def checkName(self, name=None, attr=None): |
---|
| 528 | """Check whether the given name is a valid file id for the context. |
---|
| 529 | |
---|
| 530 | Returns ``True`` only if `name` equals the result of |
---|
| 531 | :meth:`chooseName`. |
---|
| 532 | |
---|
| 533 | """ |
---|
| 534 | return name == self.chooseName() |
---|
| 535 | |
---|
[7106] | 536 | def chooseName(self, attr, name=None): |
---|
[7097] | 537 | """Get a valid file id for student context. |
---|
| 538 | |
---|
| 539 | *Example:* |
---|
| 540 | |
---|
[7105] | 541 | For a student with student id ``'A123456'`` and |
---|
[7106] | 542 | with attr ``'nice_image.jpeg'`` stored in |
---|
[7097] | 543 | the students container this chooser would create: |
---|
| 544 | |
---|
[7106] | 545 | ``'__file-student__students/A/A123456/nice_image_A123456.jpeg'`` |
---|
[7097] | 546 | |
---|
| 547 | meaning that the nice image of this applicant would be |
---|
| 548 | stored in the site-wide file storage in path: |
---|
| 549 | |
---|
[7106] | 550 | ``students/A/A123456/nice_image_A123456.jpeg`` |
---|
[7097] | 551 | |
---|
| 552 | """ |
---|
[7106] | 553 | basename, ext = os.path.splitext(attr) |
---|
[7099] | 554 | stud_id = self.context.student_id |
---|
[8448] | 555 | marked_filename = '__%s__%s/%s_%s%s' % ( |
---|
| 556 | STUDENT_FILE_STORE_NAME, path_from_studid(stud_id), basename, |
---|
[7948] | 557 | stud_id, ext) |
---|
[7097] | 558 | return marked_filename |
---|
| 559 | |
---|
| 560 | |
---|
| 561 | class StudentFileStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility): |
---|
| 562 | """Student specific file handling. |
---|
| 563 | |
---|
| 564 | This handler knows in which path in a filestore to store student |
---|
| 565 | files and how to turn this kind of data into some (browsable) |
---|
| 566 | file object. |
---|
| 567 | |
---|
| 568 | It is called from the global file storage, when it wants to |
---|
| 569 | get/store a file with a file id starting with |
---|
| 570 | ``__file-student__`` (the marker string for student files). |
---|
| 571 | |
---|
| 572 | Like each other file store handler it does not handle the files |
---|
| 573 | really (this is done by the global file store) but only computes |
---|
| 574 | paths and things like this. |
---|
| 575 | """ |
---|
| 576 | grok.implements(IFileStoreHandler) |
---|
| 577 | grok.name(STUDENT_FILE_STORE_NAME) |
---|
| 578 | |
---|
| 579 | def pathFromFileID(self, store, root, file_id): |
---|
[7099] | 580 | """All student files are put in directory ``students``. |
---|
[7097] | 581 | """ |
---|
| 582 | marker, filename, basename, ext = store.extractMarker(file_id) |
---|
[7122] | 583 | sub_root = os.path.join(root, 'students') |
---|
| 584 | return super(StudentFileStoreHandler, self).pathFromFileID( |
---|
| 585 | store, sub_root, basename) |
---|
[7097] | 586 | |
---|
| 587 | def createFile(self, store, root, filename, file_id, file): |
---|
| 588 | """Create a browsable file-like object. |
---|
| 589 | """ |
---|
[7122] | 590 | # call super method to ensure that any old files with |
---|
| 591 | # different filename extension are deleted. |
---|
| 592 | file, path, file_obj = super( |
---|
| 593 | StudentFileStoreHandler, self).createFile( |
---|
| 594 | store, root, filename, file_id, file) |
---|
[7819] | 595 | return file, path, KofaImageFile( |
---|
[7122] | 596 | file_obj.filename, file_obj.data) |
---|