[7192] | 1 | ## $Id: applicant.py 11615 2014-05-01 17:54:44Z 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 |
---|
[8843] | 29 | from zope.schema.interfaces import RequiredMissing, ConstraintNotSatisfied |
---|
[6296] | 30 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
[7819] | 31 | from waeup.kofa.image import KofaImageFile |
---|
[7811] | 32 | from waeup.kofa.imagestorage import DefaultFileStoreHandler |
---|
| 33 | from waeup.kofa.interfaces import ( |
---|
[7819] | 34 | IObjectHistory, IFileStoreHandler, IFileStoreNameChooser, IKofaUtils, |
---|
[9217] | 35 | IExtFileStore, IPDF, IUserAccount, IUniversity) |
---|
[8311] | 36 | from waeup.kofa.interfaces import MessageFactory as _ |
---|
[7811] | 37 | from waeup.kofa.students.vocabularies import RegNumNotInSource |
---|
[8843] | 38 | from waeup.kofa.students.studycourse import StudentStudyCourse |
---|
[7811] | 39 | from waeup.kofa.utils.helpers import attrs_to_fields |
---|
[10845] | 40 | from waeup.kofa.applicants.interfaces import ( |
---|
| 41 | IApplicant, IApplicantEdit, ISpecialApplicant) |
---|
[7811] | 42 | from waeup.kofa.applicants.workflow import application_states_dict |
---|
[5272] | 43 | |
---|
[8404] | 44 | def search(query=None, searchtype=None, view=None): |
---|
| 45 | if searchtype in ('fullname',): |
---|
| 46 | results = Query().searchResults( |
---|
| 47 | Text(('applicants_catalog', searchtype), query)) |
---|
| 48 | else: |
---|
| 49 | results = Query().searchResults( |
---|
| 50 | Eq(('applicants_catalog', searchtype), query)) |
---|
| 51 | return results |
---|
| 52 | |
---|
[7250] | 53 | class Applicant(grok.Container): |
---|
[10845] | 54 | grok.implements(IApplicant, IApplicantEdit, ISpecialApplicant) |
---|
[5483] | 55 | grok.provides(IApplicant) |
---|
[5272] | 56 | |
---|
[11615] | 57 | applicant_student_mapping = [ |
---|
[11614] | 58 | ('firstname', 'firstname'), |
---|
| 59 | ('middlename', 'middlename'), |
---|
| 60 | ('lastname', 'lastname'), |
---|
| 61 | ('sex', 'sex'), |
---|
| 62 | ('date_of_birth', 'date_of_birth'), |
---|
| 63 | ('email', 'email'), |
---|
| 64 | ('phone', 'phone'), |
---|
[11615] | 65 | ] |
---|
[8667] | 66 | |
---|
[7260] | 67 | def __init__(self): |
---|
[6296] | 68 | super(Applicant, self).__init__() |
---|
[7240] | 69 | self.password = None |
---|
[6476] | 70 | self.application_date = None |
---|
[7260] | 71 | self.applicant_id = None |
---|
[6331] | 72 | return |
---|
[6296] | 73 | |
---|
[8742] | 74 | def writeLogMessage(self, view, message): |
---|
| 75 | ob_class = view.__implemented__.__name__.replace('waeup.kofa.','') |
---|
| 76 | self.__parent__.__parent__.logger.info( |
---|
| 77 | '%s - %s - %s' % (ob_class, self.applicant_id, message)) |
---|
| 78 | return |
---|
[6475] | 79 | |
---|
[6324] | 80 | @property |
---|
[8286] | 81 | def state(self): |
---|
[7686] | 82 | return IWorkflowState(self).getState() |
---|
[6324] | 83 | |
---|
[6339] | 84 | @property |
---|
[8377] | 85 | def container_code(self): |
---|
| 86 | return self.__parent__.code |
---|
| 87 | |
---|
| 88 | @property |
---|
[7686] | 89 | def translated_state(self): |
---|
[8286] | 90 | return application_states_dict[self.state] |
---|
[7686] | 91 | |
---|
| 92 | @property |
---|
[6339] | 93 | def history(self): |
---|
| 94 | history = IObjectHistory(self) |
---|
| 95 | return history |
---|
| 96 | |
---|
[7240] | 97 | @property |
---|
[10831] | 98 | def special(self): |
---|
| 99 | return self.__parent__.prefix.startswith('special') |
---|
| 100 | |
---|
| 101 | @property |
---|
[7240] | 102 | def application_number(self): |
---|
[7260] | 103 | try: |
---|
| 104 | return self.applicant_id.split('_')[1] |
---|
| 105 | except AttributeError: |
---|
| 106 | return None |
---|
[7240] | 107 | |
---|
| 108 | @property |
---|
[7364] | 109 | def display_fullname(self): |
---|
[7356] | 110 | middlename = getattr(self, 'middlename', None) |
---|
[7819] | 111 | kofa_utils = getUtility(IKofaUtils) |
---|
[7811] | 112 | return kofa_utils.fullname(self.firstname, self.lastname, middlename) |
---|
[7240] | 113 | |
---|
[9480] | 114 | def _setStudyCourseAttributes(self, studycourse): |
---|
| 115 | studycourse.entry_mode = self.course_admitted.study_mode |
---|
| 116 | studycourse.current_level = self.course_admitted.start_level |
---|
| 117 | studycourse.certificate = self.course_admitted |
---|
| 118 | studycourse.entry_session = self.__parent__.year |
---|
| 119 | studycourse.current_session = self.__parent__.year |
---|
| 120 | return |
---|
| 121 | |
---|
[7391] | 122 | def createStudent(self, view=None): |
---|
[8311] | 123 | """Create a student, fill with base data, create an application slip |
---|
| 124 | and copy applicant data. |
---|
[7338] | 125 | """ |
---|
[7344] | 126 | # Is applicant in the correct state? |
---|
[8286] | 127 | if self.state != 'admitted': |
---|
[8311] | 128 | return False, _('Applicant has not yet been admitted.') |
---|
[7344] | 129 | # Does registration number exist? |
---|
[7338] | 130 | student = createObject(u'waeup.Student') |
---|
[7341] | 131 | try: |
---|
| 132 | student.reg_number = self.reg_number |
---|
| 133 | except RegNumNotInSource: |
---|
[8311] | 134 | return False, _('Registration Number exists.') |
---|
[7344] | 135 | # Has the course_admitted field been properly filled? |
---|
[7347] | 136 | if self.course_admitted is None: |
---|
[8311] | 137 | return False, _('No course admitted provided.') |
---|
[8843] | 138 | # Set student attributes |
---|
[8635] | 139 | try: |
---|
[11614] | 140 | for item in self.applicant_student_mapping: |
---|
| 141 | setattr(student, item[1], getattr(self, item[0], None)) |
---|
[8635] | 142 | except RequiredMissing, err: |
---|
[8855] | 143 | return False, 'RequiredMissing: %s' % err |
---|
[8843] | 144 | except: |
---|
[9391] | 145 | return False, 'Unknown Error' |
---|
[8843] | 146 | # Finally prove if the certificate still exists |
---|
| 147 | try: |
---|
| 148 | StudentStudyCourse().certificate = self.course_admitted |
---|
| 149 | except ConstraintNotSatisfied, err: |
---|
[8855] | 150 | return False, 'ConstraintNotSatisfied: %s' % self.course_admitted.code |
---|
[8843] | 151 | # Add student |
---|
[7406] | 152 | site = grok.getSite() |
---|
| 153 | site['students'].addStudent(student) |
---|
[9121] | 154 | # Save student_id |
---|
| 155 | self.student_id = student.student_id |
---|
| 156 | # Fire transitions |
---|
[7406] | 157 | IWorkflowInfo(self).fireTransition('create') |
---|
[8487] | 158 | IWorkflowInfo(student).fireTransition('admit') |
---|
[7406] | 159 | # Set password |
---|
[7409] | 160 | IUserAccount(student).setPassword(self.application_number) |
---|
[9480] | 161 | # Save the certificate and set study course attributes |
---|
| 162 | self._setStudyCourseAttributes(student['studycourse']) |
---|
[7388] | 163 | self._copyPassportImage(student) |
---|
[7421] | 164 | # Update the catalog |
---|
| 165 | notify(grok.ObjectModifiedEvent(student)) |
---|
[7391] | 166 | # Save application slip |
---|
| 167 | self._createApplicationPDF(student, view=view) |
---|
[7355] | 168 | |
---|
[8383] | 169 | return True, _('Student ${a} created', mapping = {'a':student.student_id}) |
---|
[7338] | 170 | |
---|
[7391] | 171 | def _createApplicationPDF(self, student, view=None): |
---|
| 172 | """Create an application slip as PDF and store it in student folder. |
---|
| 173 | """ |
---|
| 174 | file_store = getUtility(IExtFileStore) |
---|
| 175 | applicant_slip = getAdapter(self, IPDF, name='application_slip')( |
---|
| 176 | view=view) |
---|
| 177 | file_id = IFileStoreNameChooser(student).chooseName( |
---|
| 178 | attr="application_slip.pdf") |
---|
| 179 | file_store.createFile(file_id, StringIO(applicant_slip)) |
---|
| 180 | return |
---|
| 181 | |
---|
[7388] | 182 | def _copyPassportImage(self, student): |
---|
| 183 | """Copy any passport image over to student location. |
---|
| 184 | """ |
---|
| 185 | file_store = getUtility(IExtFileStore) |
---|
| 186 | appl_file = file_store.getFileByContext(self) |
---|
[7417] | 187 | if appl_file is None: |
---|
| 188 | return |
---|
[7388] | 189 | stud_file_id = IFileStoreNameChooser(student).chooseName( |
---|
| 190 | attr="passport.jpg") |
---|
[7417] | 191 | file_store.createFile(stud_file_id, appl_file) |
---|
[7388] | 192 | return |
---|
| 193 | |
---|
[5983] | 194 | # Set all attributes of Applicant required in IApplicant as field |
---|
| 195 | # properties. Doing this, we do not have to set initial attributes |
---|
| 196 | # ourselves and as a bonus we get free validation when an attribute is |
---|
| 197 | # set. |
---|
[6115] | 198 | Applicant = attrs_to_fields(Applicant) |
---|
[5272] | 199 | |
---|
[8404] | 200 | class ApplicantsCatalog(grok.Indexes): |
---|
[6115] | 201 | """A catalog indexing :class:`Applicant` instances in the ZODB. |
---|
| 202 | """ |
---|
[9217] | 203 | grok.site(IUniversity) |
---|
[6115] | 204 | grok.name('applicants_catalog') |
---|
| 205 | grok.context(IApplicant) |
---|
| 206 | |
---|
[8404] | 207 | fullname = index.Text(attribute='display_fullname') |
---|
[7240] | 208 | applicant_id = index.Field(attribute='applicant_id') |
---|
[7270] | 209 | reg_number = index.Field(attribute='reg_number') |
---|
[8039] | 210 | email = index.Field(attribute='email') |
---|
[8635] | 211 | state = index.Field(attribute='state') |
---|
[6115] | 212 | |
---|
[5318] | 213 | class ApplicantFactory(grok.GlobalUtility): |
---|
[6620] | 214 | """A factory for applicants. |
---|
[5318] | 215 | """ |
---|
| 216 | grok.implements(IFactory) |
---|
| 217 | grok.name(u'waeup.Applicant') |
---|
| 218 | title = u"Create a new applicant.", |
---|
| 219 | description = u"This factory instantiates new applicant instances." |
---|
| 220 | |
---|
| 221 | def __call__(self, *args, **kw): |
---|
[7260] | 222 | return Applicant() |
---|
[5318] | 223 | |
---|
| 224 | def getInterfaces(self): |
---|
[5479] | 225 | return implementedBy(Applicant) |
---|
[7063] | 226 | |
---|
| 227 | |
---|
| 228 | #: The file id marker for applicant passport images |
---|
| 229 | APPLICANT_IMAGE_STORE_NAME = 'img-applicant' |
---|
| 230 | |
---|
| 231 | class ApplicantImageNameChooser(grok.Adapter): |
---|
| 232 | """A file id chooser for :class:`Applicant` objects. |
---|
| 233 | |
---|
| 234 | `context` is an :class:`Applicant` instance. |
---|
| 235 | |
---|
| 236 | The :class:`ApplicantImageNameChooser` can build/check file ids |
---|
| 237 | for :class:`Applicant` objects suitable for use with |
---|
| 238 | :class:`ExtFileStore` instances. The delivered file_id contains |
---|
| 239 | the file id marker for :class:`Applicant` object and the |
---|
| 240 | registration number or access code of the context applicant. Also |
---|
| 241 | the name of the connected applicant container will be part of the |
---|
| 242 | generated file id. |
---|
| 243 | |
---|
| 244 | This chooser is registered as an adapter providing |
---|
[7811] | 245 | :class:`waeup.kofa.interfaces.IFileStoreNameChooser`. |
---|
[7063] | 246 | |
---|
| 247 | File store name choosers like this one are only convenience |
---|
| 248 | components to ease the task of creating file ids for applicant |
---|
| 249 | objects. You are nevertheless encouraged to use them instead of |
---|
| 250 | manually setting up filenames for applicants. |
---|
| 251 | |
---|
[7811] | 252 | .. seealso:: :mod:`waeup.kofa.imagestorage` |
---|
[7063] | 253 | |
---|
| 254 | """ |
---|
| 255 | grok.context(IApplicant) |
---|
| 256 | grok.implements(IFileStoreNameChooser) |
---|
| 257 | |
---|
[7067] | 258 | def checkName(self, name=None, attr=None): |
---|
[7063] | 259 | """Check whether the given name is a valid file id for the context. |
---|
| 260 | |
---|
| 261 | Returns ``True`` only if `name` equals the result of |
---|
| 262 | :meth:`chooseName`. |
---|
[7067] | 263 | |
---|
| 264 | The `attr` parameter is not taken into account for |
---|
| 265 | :class:`Applicant` context as the single passport image is the |
---|
| 266 | only file we store for applicants. |
---|
[7063] | 267 | """ |
---|
| 268 | return name == self.chooseName() |
---|
| 269 | |
---|
[7105] | 270 | def chooseName(self, name=None, attr=None): |
---|
[7063] | 271 | """Get a valid file id for applicant context. |
---|
| 272 | |
---|
| 273 | *Example:* |
---|
| 274 | |
---|
[7240] | 275 | For an applicant with applicant_id. ``'app2001_1234'`` |
---|
[7063] | 276 | and stored in an applicants container called |
---|
| 277 | ``'mycontainer'``, this chooser would create: |
---|
| 278 | |
---|
[7240] | 279 | ``'__img-applicant__mycontainer/app2001_1234.jpg'`` |
---|
[7063] | 280 | |
---|
| 281 | meaning that the passport image of this applicant would be |
---|
| 282 | stored in the site-wide file storage in path: |
---|
| 283 | |
---|
[7240] | 284 | ``mycontainer/app2001_1234.jpg`` |
---|
[7063] | 285 | |
---|
| 286 | If the context applicant has no parent, ``'_default'`` is used |
---|
| 287 | as parent name. |
---|
[7067] | 288 | |
---|
[10089] | 289 | In the beginning the `attr` parameter was not taken into account for |
---|
| 290 | :class:`Applicant` context as the single passport image was the |
---|
| 291 | only file we store for applicants. Meanwhile FUTMinna requires |
---|
| 292 | uploads of other documents too. Now we store passport image |
---|
| 293 | files without attribute but all other documents with. |
---|
[7067] | 294 | |
---|
[7063] | 295 | """ |
---|
| 296 | parent_name = getattr( |
---|
| 297 | getattr(self.context, '__parent__', None), |
---|
| 298 | '__name__', '_default') |
---|
[10089] | 299 | if attr is None or attr == 'passport.jpg': |
---|
| 300 | marked_filename = '__%s__%s/%s.jpg' % ( |
---|
| 301 | APPLICANT_IMAGE_STORE_NAME, |
---|
| 302 | parent_name, self.context.applicant_id) |
---|
| 303 | else: |
---|
| 304 | basename, ext = os.path.splitext(attr) |
---|
| 305 | marked_filename = '__%s__%s/%s_%s%s' % ( |
---|
| 306 | APPLICANT_IMAGE_STORE_NAME, |
---|
[10092] | 307 | parent_name, basename, self.context.applicant_id, ext) |
---|
[7063] | 308 | return marked_filename |
---|
| 309 | |
---|
| 310 | |
---|
| 311 | class ApplicantImageStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility): |
---|
| 312 | """Applicant specific image handling. |
---|
| 313 | |
---|
| 314 | This handler knows in which path in a filestore to store applicant |
---|
| 315 | images and how to turn this kind of data into some (browsable) |
---|
| 316 | file object. |
---|
| 317 | |
---|
| 318 | It is called from the global file storage, when it wants to |
---|
| 319 | get/store a file with a file id starting with |
---|
| 320 | ``__img-applicant__`` (the marker string for applicant images). |
---|
| 321 | |
---|
| 322 | Like each other file store handler it does not handle the files |
---|
| 323 | really (this is done by the global file store) but only computes |
---|
| 324 | paths and things like this. |
---|
| 325 | """ |
---|
| 326 | grok.implements(IFileStoreHandler) |
---|
| 327 | grok.name(APPLICANT_IMAGE_STORE_NAME) |
---|
| 328 | |
---|
| 329 | def pathFromFileID(self, store, root, file_id): |
---|
| 330 | """All applicants images are filed in directory ``applicants``. |
---|
| 331 | """ |
---|
| 332 | marker, filename, basename, ext = store.extractMarker(file_id) |
---|
[7121] | 333 | sub_root = os.path.join(root, 'applicants') |
---|
| 334 | return super(ApplicantImageStoreHandler, self).pathFromFileID( |
---|
| 335 | store, sub_root, basename) |
---|
[7063] | 336 | |
---|
| 337 | def createFile(self, store, root, filename, file_id, file): |
---|
| 338 | """Create a browsable file-like object. |
---|
| 339 | """ |
---|
[7121] | 340 | # call super method to ensure that any old files with |
---|
| 341 | # different filename extension are deleted. |
---|
| 342 | file, path, file_obj = super( |
---|
| 343 | ApplicantImageStoreHandler, self).createFile( |
---|
| 344 | store, root, filename, file_id, file) |
---|
[7819] | 345 | return file, path, KofaImageFile( |
---|
[7121] | 346 | file_obj.filename, file_obj.data) |
---|
[7240] | 347 | |
---|
| 348 | @grok.subscribe(IApplicant, grok.IObjectAddedEvent) |
---|
| 349 | def handle_applicant_added(applicant, event): |
---|
| 350 | """If an applicant is added local and site roles are assigned. |
---|
| 351 | """ |
---|
| 352 | role_manager = IPrincipalRoleManager(applicant) |
---|
| 353 | role_manager.assignRoleToPrincipal( |
---|
| 354 | 'waeup.local.ApplicationOwner', applicant.applicant_id) |
---|
| 355 | # Assign current principal the global Applicant role |
---|
| 356 | role_manager = IPrincipalRoleManager(grok.getSite()) |
---|
| 357 | role_manager.assignRoleToPrincipal( |
---|
| 358 | 'waeup.Applicant', applicant.applicant_id) |
---|
[8375] | 359 | if applicant.state is None: |
---|
| 360 | IWorkflowInfo(applicant).fireTransition('init') |
---|
[7240] | 361 | |
---|
| 362 | # Assign global applicant role for new applicant (alternative way) |
---|
| 363 | #account = IUserAccount(applicant) |
---|
| 364 | #account.roles = ['waeup.Applicant'] |
---|
| 365 | |
---|
| 366 | return |
---|
| 367 | |
---|
| 368 | @grok.subscribe(IApplicant, grok.IObjectRemovedEvent) |
---|
| 369 | def handle_applicant_removed(applicant, event): |
---|
| 370 | """If an applicant is removed a message is logged. |
---|
| 371 | """ |
---|
| 372 | comment = 'Applicant record removed' |
---|
| 373 | target = applicant.applicant_id |
---|
| 374 | try: |
---|
[7652] | 375 | grok.getSite()['applicants'].logger.info('%s - %s' % ( |
---|
| 376 | target, comment)) |
---|
[7240] | 377 | except KeyError: |
---|
| 378 | # If we delete an entire university instance there won't be |
---|
| 379 | # an applicants subcontainer |
---|
| 380 | return |
---|
[8285] | 381 | # Remove also any passport image. |
---|
| 382 | file_store = getUtility(IExtFileStore) |
---|
| 383 | file_store.deleteFileByContext(applicant) |
---|
[7240] | 384 | return |
---|