source: main/waeup.sirp/trunk/src/waeup/sirp/applicants/applicant.py @ 7341

Last change on this file since 7341 was 7341, checked in by Henrik Bettermann, 13 years ago

Do not store a reference to a certificate but a dictionary with the code, title, department title and faculty title of certificates. In this way we can preserve the information even if the certificate has been removed.

The assignment of dynamic roles is not necessary in the application section. We can assign local roles in applicants containers. That's sufficient.

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