[7192] | 1 | ## $Id: applicant.py 7376 2011-12-18 11:29:58Z 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 |
---|
[6115] | 20 | from grok import index |
---|
[7359] | 21 | from zope.component import getUtility |
---|
[5318] | 22 | from zope.component.interfaces import IFactory |
---|
[7341] | 23 | from zope.component import createObject, getUtility |
---|
| 24 | from zope.catalog.interfaces import ICatalog |
---|
[7240] | 25 | from zope.securitypolicy.interfaces import IPrincipalRoleManager |
---|
[5479] | 26 | from zope.interface import implementedBy |
---|
[6296] | 27 | from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState |
---|
[6115] | 28 | from waeup.sirp.app import University |
---|
[7321] | 29 | from waeup.sirp.image import SIRPImageFile |
---|
[7063] | 30 | from waeup.sirp.imagestorage import DefaultFileStoreHandler |
---|
| 31 | from waeup.sirp.interfaces import ( |
---|
[7359] | 32 | IObjectHistory, IFileStoreHandler, IFileStoreNameChooser, ISIRPUtils) |
---|
[7240] | 33 | from waeup.sirp.utils.helpers import attrs_to_fields, get_current_principal |
---|
[5753] | 34 | from waeup.sirp.applicants.interfaces import ( |
---|
[7341] | 35 | IApplicant, IApplicantEdit, IApplicantBaseData, |
---|
[5272] | 36 | ) |
---|
[7341] | 37 | from waeup.sirp.students.vocabularies import RegNumNotInSource |
---|
[5272] | 38 | |
---|
[7250] | 39 | class Applicant(grok.Container): |
---|
[6196] | 40 | grok.implements(IApplicant,IApplicantEdit) |
---|
[5483] | 41 | grok.provides(IApplicant) |
---|
[5272] | 42 | |
---|
[7260] | 43 | def __init__(self): |
---|
[6296] | 44 | super(Applicant, self).__init__() |
---|
[7240] | 45 | self.password = None |
---|
[6301] | 46 | IWorkflowInfo(self).fireTransition('init') |
---|
[6476] | 47 | self.application_date = None |
---|
[7260] | 48 | self.applicant_id = None |
---|
[6331] | 49 | return |
---|
[6296] | 50 | |
---|
[6475] | 51 | def loggerInfo(self, ob_class, comment=None): |
---|
[7244] | 52 | target = self.applicant_id |
---|
[6475] | 53 | return grok.getSite()['applicants'].logger_info(ob_class,target,comment) |
---|
| 54 | |
---|
[6324] | 55 | @property |
---|
[6332] | 56 | def state(self): |
---|
[6324] | 57 | state = IWorkflowState(self).getState() |
---|
| 58 | return state |
---|
| 59 | |
---|
[6339] | 60 | @property |
---|
| 61 | def history(self): |
---|
| 62 | history = IObjectHistory(self) |
---|
| 63 | return history |
---|
| 64 | |
---|
[7240] | 65 | @property |
---|
| 66 | def application_number(self): |
---|
[7260] | 67 | try: |
---|
| 68 | return self.applicant_id.split('_')[1] |
---|
| 69 | except AttributeError: |
---|
| 70 | return None |
---|
[7240] | 71 | |
---|
| 72 | @property |
---|
[7364] | 73 | def display_fullname(self): |
---|
[7356] | 74 | middlename = getattr(self, 'middlename', None) |
---|
[7359] | 75 | sirp_utils = getUtility(ISIRPUtils) |
---|
| 76 | return sirp_utils.fullname(self.firstname, self.lastname, middlename) |
---|
[7240] | 77 | |
---|
[7338] | 78 | def createStudent(self): |
---|
[7344] | 79 | """Create a student, fill with base data, create a StudentApplication |
---|
| 80 | object and copy applicant data. |
---|
[7338] | 81 | """ |
---|
[7344] | 82 | # Is applicant in the correct state? |
---|
| 83 | if self.state != 'admitted': |
---|
| 84 | return False, "Applicant has not yet been admitted." |
---|
| 85 | # Does registration number exist? |
---|
[7338] | 86 | student = createObject(u'waeup.Student') |
---|
[7341] | 87 | try: |
---|
| 88 | student.reg_number = self.reg_number |
---|
| 89 | except RegNumNotInSource: |
---|
| 90 | return False, 'Registration Number exists.' |
---|
[7344] | 91 | # Has the course_admitted field been properly filled? |
---|
[7347] | 92 | if self.course_admitted is None: |
---|
[7341] | 93 | return False, 'No course admitted provided.' |
---|
[7344] | 94 | # Add student object |
---|
[7338] | 95 | site = grok.getSite() |
---|
| 96 | site['students'].addStudent(student) |
---|
[7344] | 97 | # Fill student_id |
---|
| 98 | self.student_id = student.student_id |
---|
| 99 | # Fire transition 'create' |
---|
| 100 | IWorkflowInfo(self).fireTransition('create') |
---|
| 101 | # Copy some base data |
---|
[7356] | 102 | student.firstname = self.firstname |
---|
| 103 | student.lastname = self.lastname |
---|
[7341] | 104 | student.sex = self.sex |
---|
| 105 | student.date_of_birth = self.date_of_birth |
---|
| 106 | student.email = self.email |
---|
| 107 | student.phone = self.phone |
---|
[7344] | 108 | # Save the certificate |
---|
[7347] | 109 | student['studycourse'].certificate = self.course_admitted |
---|
[7355] | 110 | # Copy passport image |
---|
| 111 | |
---|
| 112 | # Save application slip (ExportPDFPage) |
---|
| 113 | |
---|
[7344] | 114 | return True, 'Student %s created' % student.student_id |
---|
[7338] | 115 | |
---|
[5983] | 116 | # Set all attributes of Applicant required in IApplicant as field |
---|
| 117 | # properties. Doing this, we do not have to set initial attributes |
---|
| 118 | # ourselves and as a bonus we get free validation when an attribute is |
---|
| 119 | # set. |
---|
[6115] | 120 | Applicant = attrs_to_fields(Applicant) |
---|
[5272] | 121 | |
---|
[6115] | 122 | class ApplicantCatalog(grok.Indexes): |
---|
| 123 | """A catalog indexing :class:`Applicant` instances in the ZODB. |
---|
| 124 | """ |
---|
| 125 | grok.site(University) |
---|
| 126 | grok.name('applicants_catalog') |
---|
| 127 | grok.context(IApplicant) |
---|
| 128 | |
---|
[7376] | 129 | #access_code = index.Field(attribute='access_code') |
---|
[7240] | 130 | applicant_id = index.Field(attribute='applicant_id') |
---|
[7270] | 131 | reg_number = index.Field(attribute='reg_number') |
---|
[6115] | 132 | |
---|
[5318] | 133 | class ApplicantFactory(grok.GlobalUtility): |
---|
[6620] | 134 | """A factory for applicants. |
---|
[5318] | 135 | """ |
---|
| 136 | grok.implements(IFactory) |
---|
| 137 | grok.name(u'waeup.Applicant') |
---|
| 138 | title = u"Create a new applicant.", |
---|
| 139 | description = u"This factory instantiates new applicant instances." |
---|
| 140 | |
---|
| 141 | def __call__(self, *args, **kw): |
---|
[7260] | 142 | return Applicant() |
---|
[5318] | 143 | |
---|
| 144 | def getInterfaces(self): |
---|
[5479] | 145 | return implementedBy(Applicant) |
---|
[7063] | 146 | |
---|
| 147 | |
---|
| 148 | #: The file id marker for applicant passport images |
---|
| 149 | APPLICANT_IMAGE_STORE_NAME = 'img-applicant' |
---|
| 150 | |
---|
| 151 | class ApplicantImageNameChooser(grok.Adapter): |
---|
| 152 | """A file id chooser for :class:`Applicant` objects. |
---|
| 153 | |
---|
| 154 | `context` is an :class:`Applicant` instance. |
---|
| 155 | |
---|
| 156 | The :class:`ApplicantImageNameChooser` can build/check file ids |
---|
| 157 | for :class:`Applicant` objects suitable for use with |
---|
| 158 | :class:`ExtFileStore` instances. The delivered file_id contains |
---|
| 159 | the file id marker for :class:`Applicant` object and the |
---|
| 160 | registration number or access code of the context applicant. Also |
---|
| 161 | the name of the connected applicant container will be part of the |
---|
| 162 | generated file id. |
---|
| 163 | |
---|
| 164 | This chooser is registered as an adapter providing |
---|
| 165 | :class:`waeup.sirp.interfaces.IFileStoreNameChooser`. |
---|
| 166 | |
---|
| 167 | File store name choosers like this one are only convenience |
---|
| 168 | components to ease the task of creating file ids for applicant |
---|
| 169 | objects. You are nevertheless encouraged to use them instead of |
---|
| 170 | manually setting up filenames for applicants. |
---|
| 171 | |
---|
| 172 | .. seealso:: :mod:`waeup.sirp.imagestorage` |
---|
| 173 | |
---|
| 174 | """ |
---|
| 175 | grok.context(IApplicant) |
---|
| 176 | grok.implements(IFileStoreNameChooser) |
---|
| 177 | |
---|
[7067] | 178 | def checkName(self, name=None, attr=None): |
---|
[7063] | 179 | """Check whether the given name is a valid file id for the context. |
---|
| 180 | |
---|
| 181 | Returns ``True`` only if `name` equals the result of |
---|
| 182 | :meth:`chooseName`. |
---|
[7067] | 183 | |
---|
| 184 | The `attr` parameter is not taken into account for |
---|
| 185 | :class:`Applicant` context as the single passport image is the |
---|
| 186 | only file we store for applicants. |
---|
[7063] | 187 | """ |
---|
| 188 | return name == self.chooseName() |
---|
| 189 | |
---|
[7105] | 190 | def chooseName(self, name=None, attr=None): |
---|
[7063] | 191 | """Get a valid file id for applicant context. |
---|
| 192 | |
---|
| 193 | *Example:* |
---|
| 194 | |
---|
[7240] | 195 | For an applicant with applicant_id. ``'app2001_1234'`` |
---|
[7063] | 196 | and stored in an applicants container called |
---|
| 197 | ``'mycontainer'``, this chooser would create: |
---|
| 198 | |
---|
[7240] | 199 | ``'__img-applicant__mycontainer/app2001_1234.jpg'`` |
---|
[7063] | 200 | |
---|
| 201 | meaning that the passport image of this applicant would be |
---|
| 202 | stored in the site-wide file storage in path: |
---|
| 203 | |
---|
[7240] | 204 | ``mycontainer/app2001_1234.jpg`` |
---|
[7063] | 205 | |
---|
| 206 | If the context applicant has no parent, ``'_default'`` is used |
---|
| 207 | as parent name. |
---|
[7067] | 208 | |
---|
| 209 | The `attr` parameter is not taken into account for |
---|
| 210 | :class:`Applicant` context as the single passport image is the |
---|
| 211 | only file we store for applicants. |
---|
| 212 | |
---|
[7063] | 213 | """ |
---|
| 214 | parent_name = getattr( |
---|
| 215 | getattr(self.context, '__parent__', None), |
---|
| 216 | '__name__', '_default') |
---|
| 217 | marked_filename = '__%s__%s/%s.jpg' % ( |
---|
| 218 | APPLICANT_IMAGE_STORE_NAME, |
---|
[7240] | 219 | parent_name, self.context.applicant_id) |
---|
[7063] | 220 | return marked_filename |
---|
| 221 | |
---|
| 222 | |
---|
| 223 | class ApplicantImageStoreHandler(DefaultFileStoreHandler, grok.GlobalUtility): |
---|
| 224 | """Applicant specific image handling. |
---|
| 225 | |
---|
| 226 | This handler knows in which path in a filestore to store applicant |
---|
| 227 | images and how to turn this kind of data into some (browsable) |
---|
| 228 | file object. |
---|
| 229 | |
---|
| 230 | It is called from the global file storage, when it wants to |
---|
| 231 | get/store a file with a file id starting with |
---|
| 232 | ``__img-applicant__`` (the marker string for applicant images). |
---|
| 233 | |
---|
| 234 | Like each other file store handler it does not handle the files |
---|
| 235 | really (this is done by the global file store) but only computes |
---|
| 236 | paths and things like this. |
---|
| 237 | """ |
---|
| 238 | grok.implements(IFileStoreHandler) |
---|
| 239 | grok.name(APPLICANT_IMAGE_STORE_NAME) |
---|
| 240 | |
---|
| 241 | def pathFromFileID(self, store, root, file_id): |
---|
| 242 | """All applicants images are filed in directory ``applicants``. |
---|
| 243 | """ |
---|
| 244 | marker, filename, basename, ext = store.extractMarker(file_id) |
---|
[7121] | 245 | sub_root = os.path.join(root, 'applicants') |
---|
| 246 | return super(ApplicantImageStoreHandler, self).pathFromFileID( |
---|
| 247 | store, sub_root, basename) |
---|
[7063] | 248 | |
---|
| 249 | def createFile(self, store, root, filename, file_id, file): |
---|
| 250 | """Create a browsable file-like object. |
---|
| 251 | """ |
---|
[7121] | 252 | ext = os.path.splitext(filename)[1].lower() |
---|
| 253 | if ext not in ['.jpg', '.png']: |
---|
| 254 | raise ValueError('Only .jpg and .png allowed') |
---|
| 255 | # call super method to ensure that any old files with |
---|
| 256 | # different filename extension are deleted. |
---|
| 257 | file, path, file_obj = super( |
---|
| 258 | ApplicantImageStoreHandler, self).createFile( |
---|
| 259 | store, root, filename, file_id, file) |
---|
[7321] | 260 | return file, path, SIRPImageFile( |
---|
[7121] | 261 | file_obj.filename, file_obj.data) |
---|
[7240] | 262 | |
---|
| 263 | @grok.subscribe(IApplicant, grok.IObjectAddedEvent) |
---|
| 264 | def handle_applicant_added(applicant, event): |
---|
| 265 | """If an applicant is added local and site roles are assigned. |
---|
| 266 | """ |
---|
| 267 | role_manager = IPrincipalRoleManager(applicant) |
---|
| 268 | role_manager.assignRoleToPrincipal( |
---|
| 269 | 'waeup.local.ApplicationOwner', applicant.applicant_id) |
---|
| 270 | # Assign current principal the global Applicant role |
---|
| 271 | role_manager = IPrincipalRoleManager(grok.getSite()) |
---|
| 272 | role_manager.assignRoleToPrincipal( |
---|
| 273 | 'waeup.Applicant', applicant.applicant_id) |
---|
| 274 | |
---|
| 275 | # Assign global applicant role for new applicant (alternative way) |
---|
| 276 | #account = IUserAccount(applicant) |
---|
| 277 | #account.roles = ['waeup.Applicant'] |
---|
| 278 | |
---|
| 279 | return |
---|
| 280 | |
---|
| 281 | @grok.subscribe(IApplicant, grok.IObjectRemovedEvent) |
---|
| 282 | def handle_applicant_removed(applicant, event): |
---|
| 283 | """If an applicant is removed a message is logged. |
---|
| 284 | """ |
---|
| 285 | comment = 'Applicant record removed' |
---|
| 286 | target = applicant.applicant_id |
---|
| 287 | # In some tests we don't have a principal |
---|
| 288 | try: |
---|
| 289 | user = get_current_principal().id |
---|
| 290 | except (TypeError, AttributeError): |
---|
| 291 | return |
---|
| 292 | try: |
---|
| 293 | grok.getSite()['applicants'].logger.info('%s - %s - %s' % ( |
---|
| 294 | user, target, comment)) |
---|
| 295 | except KeyError: |
---|
| 296 | # If we delete an entire university instance there won't be |
---|
| 297 | # an applicants subcontainer |
---|
| 298 | return |
---|
| 299 | return |
---|