source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/applicant.py @ 14222

Last change on this file since 14222 was 13974, checked in by Henrik Bettermann, 8 years ago

Add refereereports property attribute.

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