[7192] | 1 | ## $Id: applicant.py 16279 2020-10-12 09:54:11Z henrik $ |
---|
[5272] | 2 | ## |
---|
[7192] | 3 | ## Copyright (C) 2011 Uli Fouquet & Henrik Bettermann |
---|
[5272] | 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. |
---|
[7192] | 8 | ## |
---|
[5272] | 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. |
---|
[7192] | 13 | ## |
---|
[5272] | 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 | ## |
---|
[7063] | 18 | import os |
---|
[5272] | 19 | import grok |
---|
[7391] | 20 | from cStringIO import StringIO |
---|
[6115] | 21 | from grok import index |
---|
[8404] | 22 | from hurry.query import Eq, Text |
---|
| 23 | from hurry.query.query import Query |
---|
[7438] | 24 | from zope.component import getUtility, createObject, getAdapter |
---|
[5318] | 25 | from zope.component.interfaces import IFactory |
---|
[7421] | 26 | from zope.event import notify |
---|
[7240] | 27 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
[5479] | 28 | from zope.interface import implementedBy |
---|
[16278] | 29 | from zope.schema.interfaces import ( |
---|
| 30 | RequiredMissing, ConstraintNotSatisfied, WrongType) |
---|
[6296] | 31 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
[7819] | 32 | from waeup.kofa.image import KofaImageFile |
---|
[14256] | 33 | from reportlab.platypus.doctemplate import LayoutError |
---|
[7811] | 34 | from waeup.kofa.imagestorage import DefaultFileStoreHandler |
---|
| 35 | from waeup.kofa.interfaces import ( |
---|
[7819] | 36 | IObjectHistory, IFileStoreHandler, IFileStoreNameChooser, IKofaUtils, |
---|
[9217] | 37 | IExtFileStore, IPDF, IUserAccount, IUniversity) |
---|
[8311] | 38 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7811] | 39 | from waeup.kofa.students.vocabularies import RegNumNotInSource |
---|
[8843] | 40 | from waeup.kofa.students.studycourse import StudentStudyCourse |
---|
[7811] | 41 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
[10845] | 42 | from waeup.kofa.applicants.interfaces import ( |
---|
[15941] | 43 | IApplicant, IApplicantEdit, ISpecialApplicant, IApplicantsUtils) |
---|
[7811] | 44 | from waeup.kofa.applicants.workflow import application_states_dict |
---|
[13968] | 45 | from waeup.kofa.applicants.payment import ApplicantOnlinePayment |
---|
[13974] | 46 | from waeup.kofa.applicants.refereereport import ApplicantRefereeReport |
---|
[5272] | 47 | |
---|
[8404] | 48 | def search(query=None, searchtype=None, view=None): |
---|
| 49 | if searchtype in ('fullname',): |
---|
| 50 | results = Query().searchResults( |
---|
| 51 | Text(('applicants_catalog', searchtype), query)) |
---|
| 52 | else: |
---|
| 53 | results = Query().searchResults( |
---|
| 54 | Eq(('applicants_catalog', searchtype), query)) |
---|
| 55 | return results |
---|
| 56 | |
---|
[7250] | 57 | class Applicant(grok.Container): |
---|
[10845] | 58 | grok.implements(IApplicant, IApplicantEdit, ISpecialApplicant) |
---|
[5483] | 59 | grok.provides(IApplicant) |
---|
[5272] | 60 | |
---|
[11615] | 61 | applicant_student_mapping = [ |
---|
[11614] | 62 | ('firstname', 'firstname'), |
---|
| 63 | ('middlename', 'middlename'), |
---|
| 64 | ('lastname', 'lastname'), |
---|
| 65 | ('sex', 'sex'), |
---|
| 66 | ('date_of_birth', 'date_of_birth'), |
---|
| 67 | ('email', 'email'), |
---|
| 68 | ('phone', 'phone'), |
---|
[11615] | 69 | ] |
---|
[8667] | 70 | |
---|
[16228] | 71 | applicant_graduated_mapping = [ |
---|
| 72 | ('firstname', 'firstname'), |
---|
| 73 | ('middlename', 'middlename'), |
---|
| 74 | ('lastname', 'lastname'), |
---|
| 75 | ('sex', 'sex'), |
---|
| 76 | ('date_of_birth', 'date_of_birth'), |
---|
| 77 | ('email', 'email'), |
---|
| 78 | ('phone', 'phone'), |
---|
| 79 | ] |
---|
| 80 | |
---|
[7260] | 81 | def __init__(self): |
---|
[6296] | 82 | super(Applicant, self).__init__() |
---|
[7240] | 83 | self.password = None |
---|
[6476] | 84 | self.application_date = None |
---|
[7260] | 85 | self.applicant_id = None |
---|
[6331] | 86 | return |
---|
[6296] | 87 | |
---|
[13968] | 88 | @property |
---|
| 89 | def payments(self): |
---|
| 90 | payments = [value for value in self.values() |
---|
| 91 | if isinstance(value, ApplicantOnlinePayment)] |
---|
| 92 | return payments |
---|
| 93 | |
---|
[13974] | 94 | @property |
---|
| 95 | def refereereports(self): |
---|
| 96 | reports = [value for value in self.values() |
---|
| 97 | if isinstance(value, ApplicantRefereeReport)] |
---|
| 98 | return reports |
---|
| 99 | |
---|
[8742] | 100 | def writeLogMessage(self, view, message): |
---|
| 101 | ob_class = view.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 102 | self.__parent__.__parent__.logger.info( |
---|
| 103 | '%s - %s - %s' % (ob_class, self.applicant_id, message)) |
---|
| 104 | return |
---|
[6475] | 105 | |
---|
[6324] | 106 | @property |
---|
[8286] | 107 | def state(self): |
---|
[7686] | 108 | return IWorkflowState(self).getState() |
---|
[6324] | 109 | |
---|
[6339] | 110 | @property |
---|
[8377] | 111 | def container_code(self): |
---|
[13080] | 112 | try: |
---|
| 113 | code = self.__parent__.code |
---|
| 114 | except AttributeError: # in unit tests |
---|
| 115 | return |
---|
[13216] | 116 | if (self.password, |
---|
| 117 | self.firstname, |
---|
| 118 | self.lastname, |
---|
| 119 | self.email) == (None, None, None, None): |
---|
| 120 | return code + '-' |
---|
| 121 | return code + '+' |
---|
[8377] | 122 | |
---|
| 123 | @property |
---|
[7686] | 124 | def translated_state(self): |
---|
[13080] | 125 | try: |
---|
| 126 | state = application_states_dict[self.state] |
---|
| 127 | except LookupError: # in unit tests |
---|
| 128 | return |
---|
| 129 | return state |
---|
[7686] | 130 | |
---|
| 131 | @property |
---|
[6339] | 132 | def history(self): |
---|
| 133 | history = IObjectHistory(self) |
---|
| 134 | return history |
---|
| 135 | |
---|
[7240] | 136 | @property |
---|
[10831] | 137 | def special(self): |
---|
[13080] | 138 | try: |
---|
| 139 | special = self.__parent__.prefix.startswith('special') |
---|
| 140 | except AttributeError: # in unit tests |
---|
| 141 | return |
---|
| 142 | return special |
---|
[10831] | 143 | |
---|
| 144 | @property |
---|
[7240] | 145 | def application_number(self): |
---|
[7260] | 146 | try: |
---|
| 147 | return self.applicant_id.split('_')[1] |
---|
| 148 | except AttributeError: |
---|
| 149 | return None |
---|
[7240] | 150 | |
---|
| 151 | @property |
---|
[7364] | 152 | def display_fullname(self): |
---|
[7356] | 153 | middlename = getattr(self, 'middlename', None) |
---|
[7819] | 154 | kofa_utils = getUtility(IKofaUtils) |
---|
[7811] | 155 | return kofa_utils.fullname(self.firstname, self.lastname, middlename) |
---|
[7240] | 156 | |
---|
[9480] | 157 | def _setStudyCourseAttributes(self, studycourse): |
---|
| 158 | studycourse.entry_mode = self.course_admitted.study_mode |
---|
| 159 | studycourse.current_level = self.course_admitted.start_level |
---|
| 160 | studycourse.certificate = self.course_admitted |
---|
| 161 | studycourse.entry_session = self.__parent__.year |
---|
| 162 | studycourse.current_session = self.__parent__.year |
---|
| 163 | return |
---|
| 164 | |
---|
[16228] | 165 | def _setGraduatedStudyCourseAttributes(self, studycourse): |
---|
| 166 | studycourse.entry_mode = self.course_studied.study_mode |
---|
| 167 | studycourse.current_level = self.course_studied.start_level |
---|
| 168 | studycourse.certificate = self.course_studied |
---|
| 169 | studycourse.entry_session = self.__parent__.year |
---|
| 170 | studycourse.current_session = self.__parent__.year |
---|
| 171 | return |
---|
| 172 | |
---|
| 173 | def createStudent(self, view=None, graduated=False): |
---|
[15941] | 174 | """Create a student, fill with base data, create an application slip, |
---|
| 175 | copy applicant data and files. |
---|
[7338] | 176 | """ |
---|
[15932] | 177 | site = grok.getSite() |
---|
[7344] | 178 | # Is applicant in the correct state? |
---|
[16228] | 179 | if graduated: |
---|
| 180 | if self.state != 'processed': |
---|
| 181 | return False, _('Applicant has not yet been processed.') |
---|
| 182 | certificate = getattr(self, 'course_studied', None) |
---|
| 183 | mapping = self.applicant_graduated_mapping |
---|
| 184 | else: |
---|
| 185 | if self.state != 'admitted': |
---|
| 186 | return False, _('Applicant has not yet been admitted.') |
---|
| 187 | certificate = getattr(self, 'course_admitted', None) |
---|
| 188 | mapping = self.applicant_student_mapping |
---|
[7344] | 189 | # Does registration number exist? |
---|
[7338] | 190 | student = createObject(u'waeup.Student') |
---|
[7341] | 191 | try: |
---|
| 192 | student.reg_number = self.reg_number |
---|
| 193 | except RegNumNotInSource: |
---|
[15932] | 194 | # Reset _curr_stud_id |
---|
| 195 | site['students']._curr_stud_id -= 1 |
---|
[8311] | 196 | return False, _('Registration Number exists.') |
---|
[16228] | 197 | # Has the course_admitted/course_studied field been properly filled? |
---|
| 198 | if certificate is None: |
---|
[15932] | 199 | # Reset _curr_stud_id |
---|
| 200 | site['students']._curr_stud_id -= 1 |
---|
[16228] | 201 | return False, _('No study course provided.') |
---|
[8843] | 202 | # Set student attributes |
---|
[8635] | 203 | try: |
---|
[16228] | 204 | for item in mapping: |
---|
[16279] | 205 | if item[0] == 'email': |
---|
| 206 | setattr(student, item[1], str(getattr(self, item[0], None))) |
---|
| 207 | else: |
---|
| 208 | setattr(student, item[1], getattr(self, item[0], None)) |
---|
[8635] | 209 | except RequiredMissing, err: |
---|
[15932] | 210 | site['students']._curr_stud_id -= 1 |
---|
[8855] | 211 | return False, 'RequiredMissing: %s' % err |
---|
[16278] | 212 | except WrongType, err: |
---|
[16279] | 213 | site['students']._curr_stud_id -= 1 |
---|
| 214 | return False, 'WrongType: %s' % err |
---|
[8843] | 215 | except: |
---|
[15932] | 216 | site['students']._curr_stud_id -= 1 |
---|
[9391] | 217 | return False, 'Unknown Error' |
---|
[12395] | 218 | # Prove if the certificate still exists |
---|
[8843] | 219 | try: |
---|
[16228] | 220 | StudentStudyCourse().certificate = certificate |
---|
[8843] | 221 | except ConstraintNotSatisfied, err: |
---|
[15932] | 222 | # Reset _curr_stud_id |
---|
| 223 | site['students']._curr_stud_id -= 1 |
---|
[16228] | 224 | return False, 'ConstraintNotSatisfied: %s' % certificate.code |
---|
[12395] | 225 | # Finally prove if an application slip can be created |
---|
| 226 | try: |
---|
[12565] | 227 | test_applicant_slip = getAdapter(self, IPDF, name='application_slip')( |
---|
[12395] | 228 | view=view) |
---|
| 229 | except IOError: |
---|
[15932] | 230 | site['students']._curr_stud_id -= 1 |
---|
[12395] | 231 | return False, _('IOError: Application Slip could not be created.') |
---|
[14256] | 232 | except LayoutError, err: |
---|
[15932] | 233 | site['students']._curr_stud_id -= 1 |
---|
[14256] | 234 | return False, _('Reportlab LayoutError: %s' % err) |
---|
[15930] | 235 | except AttributeError, err: |
---|
[15932] | 236 | site['students']._curr_stud_id -= 1 |
---|
[15930] | 237 | return False, _('Reportlab AttributeError: ${a}', mapping = {'a':err}) |
---|
[14263] | 238 | except: |
---|
[15932] | 239 | site['students']._curr_stud_id -= 1 |
---|
[15930] | 240 | return False, _('Unknown Reportlab error') |
---|
[8843] | 241 | # Add student |
---|
[7406] | 242 | site['students'].addStudent(student) |
---|
[9121] | 243 | # Save student_id |
---|
| 244 | self.student_id = student.student_id |
---|
[16228] | 245 | if graduated: |
---|
| 246 | # Set state |
---|
| 247 | IWorkflowState(student).setState('graduated') |
---|
| 248 | # Save the certificate and set study course attributes |
---|
| 249 | self._setGraduatedStudyCourseAttributes(student['studycourse']) |
---|
| 250 | else: |
---|
| 251 | # Fire transitions |
---|
| 252 | IWorkflowInfo(self).fireTransition('create') |
---|
| 253 | IWorkflowInfo(student).fireTransition('admit') |
---|
| 254 | # Save the certificate and set study course attributes |
---|
| 255 | self._setStudyCourseAttributes(student['studycourse']) |
---|
[7406] | 256 | # Set password |
---|
[7409] | 257 | IUserAccount(student).setPassword(self.application_number) |
---|
[7388] | 258 | self._copyPassportImage(student) |
---|
[15941] | 259 | self._copyFiles(student) |
---|
[7421] | 260 | # Update the catalog |
---|
| 261 | notify(grok.ObjectModifiedEvent(student)) |
---|
[7391] | 262 | # Save application slip |
---|
[12565] | 263 | applicant_slip = getAdapter(self, IPDF, name='application_slip')( |
---|
| 264 | view=view) |
---|
[12395] | 265 | self._saveApplicationPDF(student, applicant_slip, view=view) |
---|
| 266 | return True, _('Student ${a} created', mapping = {'a':student.student_id}) |
---|
[7355] | 267 | |
---|
[12395] | 268 | def _saveApplicationPDF(self, student, applicant_slip, view=None): |
---|
[7391] | 269 | """Create an application slip as PDF and store it in student folder. |
---|
| 270 | """ |
---|
| 271 | file_store = getUtility(IExtFileStore) |
---|
| 272 | file_id = IFileStoreNameChooser(student).chooseName( |
---|
| 273 | attr="application_slip.pdf") |
---|
| 274 | file_store.createFile(file_id, StringIO(applicant_slip)) |
---|
| 275 | return |
---|
| 276 | |
---|
[7388] | 277 | def _copyPassportImage(self, student): |
---|
| 278 | """Copy any passport image over to student location. |
---|
| 279 | """ |
---|
| 280 | file_store = getUtility(IExtFileStore) |
---|
| 281 | appl_file = file_store.getFileByContext(self) |
---|
[7417] | 282 | if appl_file is None: |
---|
| 283 | return |
---|
[7388] | 284 | stud_file_id = IFileStoreNameChooser(student).chooseName( |
---|
| 285 | attr="passport.jpg") |
---|
[7417] | 286 | file_store.createFile(stud_file_id, appl_file) |
---|
[7388] | 287 | return |
---|
| 288 | |
---|
[15941] | 289 | def _copyFiles(self, student): |
---|
| 290 | """Copy all other files over to student location. Not |
---|
| 291 | used in base package but tested with fake file. |
---|
| 292 | """ |
---|
| 293 | file_store = getUtility(IExtFileStore) |
---|
[15943] | 294 | filenames = getUtility(IApplicantsUtils).ADDITIONAL_FILES |
---|
[15941] | 295 | for filename in filenames: |
---|
[15943] | 296 | appl_file = file_store.getFileByContext(self, attr=filename[1]) |
---|
[15941] | 297 | if appl_file is None: |
---|
[16016] | 298 | continue |
---|
[15941] | 299 | stud_file_id = IFileStoreNameChooser(student).chooseName( |
---|
[15943] | 300 | attr=filename[1]) |
---|
[15941] | 301 | file_store.createFile(stud_file_id, appl_file) |
---|
| 302 | return |
---|
| 303 | |
---|
[5983] | 304 | # Set all attributes of Applicant required in IApplicant as field |
---|
| 305 | # properties. Doing this, we do not have to set initial attributes |
---|
| 306 | # ourselves and as a bonus we get free validation when an attribute is |
---|
| 307 | # set. |
---|
[6115] | 308 | Applicant = attrs_to_fields(Applicant) |
---|
[5272] | 309 | |
---|
[8404] | 310 | class ApplicantsCatalog(grok.Indexes): |
---|
[6115] | 311 | """A catalog indexing :class:`Applicant` instances in the ZODB. |
---|
| 312 | """ |
---|
[9217] | 313 | grok.site(IUniversity) |
---|
[6115] | 314 | grok.name('applicants_catalog') |
---|
| 315 | grok.context(IApplicant) |
---|
| 316 | |
---|
[8404] | 317 | fullname = index.Text(attribute='display_fullname') |
---|
[7240] | 318 | applicant_id = index.Field(attribute='applicant_id') |
---|
[7270] | 319 | reg_number = index.Field(attribute='reg_number') |
---|
[8039] | 320 | email = index.Field(attribute='email') |
---|
[8635] | 321 | state = index.Field(attribute='state') |
---|
[13216] | 322 | container_code = index.Field(attribute='container_code') |
---|
[6115] | 323 | |
---|
[5318] | 324 | class ApplicantFactory(grok.GlobalUtility): |
---|
[6620] | 325 | """A factory for applicants. |
---|
[5318] | 326 | """ |
---|
| 327 | grok.implements(IFactory) |
---|
| 328 | grok.name(u'waeup.Applicant') |
---|
| 329 | title = u"Create a new applicant.", |
---|
| 330 | description = u"This factory instantiates new applicant instances." |
---|
| 331 | |
---|
| 332 | def __call__(self, *args, **kw): |
---|
[7260] | 333 | return Applicant() |
---|
[5318] | 334 | |
---|
| 335 | def getInterfaces(self): |
---|
[5479] | 336 | return implementedBy(Applicant) |
---|
[7063] | 337 | |
---|
| 338 | |
---|
| 339 | #: The file id marker for applicant passport images |
---|
| 340 | APPLICANT_IMAGE_STORE_NAME = 'img-applicant' |
---|
| 341 | |
---|
| 342 | class ApplicantImageNameChooser(grok.Adapter): |
---|
| 343 | """A file id chooser for :class:`Applicant` objects. |
---|
| 344 | |
---|
| 345 | `context` is an :class:`Applicant` instance. |
---|
| 346 | |
---|
| 347 | The :class:`ApplicantImageNameChooser` can build/check file ids |
---|
| 348 | for :class:`Applicant` objects suitable for use with |
---|
| 349 | :class:`ExtFileStore` instances. The delivered file_id contains |
---|
| 350 | the file id marker for :class:`Applicant` object and the |
---|
| 351 | registration number or access code of the context applicant. Also |
---|
| 352 | the name of the connected applicant container will be part of the |
---|
| 353 | generated file id. |
---|
| 354 | |
---|
| 355 | This chooser is registered as an adapter providing |
---|
[7811] | 356 | :class:`waeup.kofa.interfaces.IFileStoreNameChooser`. |
---|
[7063] | 357 | |
---|
| 358 | File store name choosers like this one are only convenience |
---|
| 359 | components to ease the task of creating file ids for applicant |
---|
| 360 | objects. You are nevertheless encouraged to use them instead of |
---|
| 361 | manually setting up filenames for applicants. |
---|
| 362 | |
---|
[7811] | 363 | .. seealso:: :mod:`waeup.kofa.imagestorage` |
---|
[7063] | 364 | |
---|
| 365 | """ |
---|
| 366 | grok.context(IApplicant) |
---|
| 367 | grok.implements(IFileStoreNameChooser) |
---|
| 368 | |
---|
[7067] | 369 | def checkName(self, name=None, attr=None): |
---|
[7063] | 370 | """Check whether the given name is a valid file id for the context. |
---|
| 371 | |
---|
| 372 | Returns ``True`` only if `name` equals the result of |
---|
| 373 | :meth:`chooseName`. |
---|
[7067] | 374 | |
---|
| 375 | The `attr` parameter is not taken into account for |
---|
| 376 | :class:`Applicant` context as the single passport image is the |
---|
| 377 | only file we store for applicants. |
---|
[7063] | 378 | """ |
---|
| 379 | return name == self.chooseName() |
---|
| 380 | |
---|
[7105] | 381 | def chooseName(self, name=None, attr=None): |
---|
[7063] | 382 | """Get a valid file id for applicant context. |
---|
| 383 | |
---|
| 384 | *Example:* |
---|
| 385 | |
---|
[7240] | 386 | For an applicant with applicant_id. ``'app2001_1234'`` |
---|
[7063] | 387 | and stored in an applicants container called |
---|
| 388 | ``'mycontainer'``, this chooser would create: |
---|
| 389 | |
---|
[7240] | 390 | ``'__img-applicant__mycontainer/app2001_1234.jpg'`` |
---|
[7063] | 391 | |
---|
| 392 | meaning that the passport image of this applicant would be |
---|
| 393 | stored in the site-wide file storage in path: |
---|
| 394 | |
---|
[7240] | 395 | ``mycontainer/app2001_1234.jpg`` |
---|
[7063] | 396 | |
---|
| 397 | If the context applicant has no parent, ``'_default'`` is used |
---|
| 398 | as parent name. |
---|
[7067] | 399 | |
---|
[10089] | 400 | In the beginning the `attr` parameter was not taken into account for |
---|
| 401 | :class:`Applicant` context as the single passport image was the |
---|
[15941] | 402 | only file we store for applicants. Meanwhile many universities require |
---|
[10089] | 403 | uploads of other documents too. Now we store passport image |
---|
| 404 | files without attribute but all other documents with. |
---|
[7067] | 405 | |
---|
[7063] | 406 | """ |
---|
| 407 | parent_name = getattr( |
---|
| 408 | getattr(self.context, '__parent__', None), |
---|
| 409 | '__name__', '_default') |
---|
[10089] | 410 | if attr is None or attr == 'passport.jpg': |
---|
| 411 | marked_filename = '__%s__%s/%s.jpg' % ( |
---|
| 412 | APPLICANT_IMAGE_STORE_NAME, |
---|
| 413 | parent_name, self.context.applicant_id) |
---|
| 414 | else: |
---|
| 415 | basename, ext = os.path.splitext(attr) |
---|
| 416 | marked_filename = '__%s__%s/%s_%s%s' % ( |
---|
| 417 | APPLICANT_IMAGE_STORE_NAME, |
---|
[10092] | 418 | parent_name, basename, self.context.applicant_id, ext) |
---|
[7063] | 419 | return marked_filename |
---|
| 420 | |
---|
| 421 | |
---|
| 422 | class ApplicantImageStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility): |
---|
| 423 | """Applicant specific image handling. |
---|
| 424 | |
---|
| 425 | This handler knows in which path in a filestore to store applicant |
---|
| 426 | images and how to turn this kind of data into some (browsable) |
---|
| 427 | file object. |
---|
| 428 | |
---|
| 429 | It is called from the global file storage, when it wants to |
---|
| 430 | get/store a file with a file id starting with |
---|
| 431 | ``__img-applicant__`` (the marker string for applicant images). |
---|
| 432 | |
---|
| 433 | Like each other file store handler it does not handle the files |
---|
| 434 | really (this is done by the global file store) but only computes |
---|
| 435 | paths and things like this. |
---|
| 436 | """ |
---|
| 437 | grok.implements(IFileStoreHandler) |
---|
| 438 | grok.name(APPLICANT_IMAGE_STORE_NAME) |
---|
| 439 | |
---|
| 440 | def pathFromFileID(self, store, root, file_id): |
---|
| 441 | """All applicants images are filed in directory ``applicants``. |
---|
| 442 | """ |
---|
| 443 | marker, filename, basename, ext = store.extractMarker(file_id) |
---|
[7121] | 444 | sub_root = os.path.join(root, 'applicants') |
---|
| 445 | return super(ApplicantImageStoreHandler, self).pathFromFileID( |
---|
| 446 | store, sub_root, basename) |
---|
[7063] | 447 | |
---|
| 448 | def createFile(self, store, root, filename, file_id, file): |
---|
| 449 | """Create a browsable file-like object. |
---|
| 450 | """ |
---|
[7121] | 451 | # call super method to ensure that any old files with |
---|
| 452 | # different filename extension are deleted. |
---|
| 453 | file, path, file_obj = super( |
---|
| 454 | ApplicantImageStoreHandler, self).createFile( |
---|
| 455 | store, root, filename, file_id, file) |
---|
[7819] | 456 | return file, path, KofaImageFile( |
---|
[7121] | 457 | file_obj.filename, file_obj.data) |
---|
[7240] | 458 | |
---|
| 459 | @grok.subscribe(IApplicant, grok.IObjectAddedEvent) |
---|
| 460 | def handle_applicant_added(applicant, event): |
---|
| 461 | """If an applicant is added local and site roles are assigned. |
---|
| 462 | """ |
---|
| 463 | role_manager = IPrincipalRoleManager(applicant) |
---|
| 464 | role_manager.assignRoleToPrincipal( |
---|
| 465 | 'waeup.local.ApplicationOwner', applicant.applicant_id) |
---|
| 466 | # Assign current principal the global Applicant role |
---|
| 467 | role_manager = IPrincipalRoleManager(grok.getSite()) |
---|
| 468 | role_manager.assignRoleToPrincipal( |
---|
| 469 | 'waeup.Applicant', applicant.applicant_id) |
---|
[8375] | 470 | if applicant.state is None: |
---|
| 471 | IWorkflowInfo(applicant).fireTransition('init') |
---|
[7240] | 472 | |
---|
| 473 | # Assign global applicant role for new applicant (alternative way) |
---|
| 474 | #account = IUserAccount(applicant) |
---|
| 475 | #account.roles = ['waeup.Applicant'] |
---|
| 476 | |
---|
| 477 | return |
---|
| 478 | |
---|
| 479 | @grok.subscribe(IApplicant, grok.IObjectRemovedEvent) |
---|
| 480 | def handle_applicant_removed(applicant, event): |
---|
[13224] | 481 | """If an applicant is removed a message is logged, passport images are |
---|
| 482 | deleted and the global role is unset. |
---|
[7240] | 483 | """ |
---|
[13073] | 484 | comment = 'Application record removed' |
---|
[7240] | 485 | target = applicant.applicant_id |
---|
| 486 | try: |
---|
[7652] | 487 | grok.getSite()['applicants'].logger.info('%s - %s' % ( |
---|
| 488 | target, comment)) |
---|
[7240] | 489 | except KeyError: |
---|
| 490 | # If we delete an entire university instance there won't be |
---|
| 491 | # an applicants subcontainer |
---|
| 492 | return |
---|
[13224] | 493 | # Remove any passport image. |
---|
[8285] | 494 | file_store = getUtility(IExtFileStore) |
---|
| 495 | file_store.deleteFileByContext(applicant) |
---|
[15941] | 496 | # Remove all other files too |
---|
[15943] | 497 | filenames = getUtility(IApplicantsUtils).ADDITIONAL_FILES |
---|
| 498 | for filename in filenames: |
---|
| 499 | file_store.deleteFileByContext(applicant, attr=filename[1]) |
---|
[13224] | 500 | # Remove global role |
---|
| 501 | role_manager = IPrincipalRoleManager(grok.getSite()) |
---|
| 502 | role_manager.unsetRoleForPrincipal( |
---|
| 503 | 'waeup.Applicant', applicant.applicant_id) |
---|
[7240] | 504 | return |
---|