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