source: main/waeup.kofa/trunk/src/waeup/kofa/applicants/interfaces.py @ 8570

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

Refresh title when saving containers.

  • Property svn:keywords set to Id
File size: 16.4 KB
Line 
1## $Id: interfaces.py 8562 2012-05-30 12:00:39Z 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##
18"""Interfaces of the university application package.
19"""
20from grokcore.content.interfaces import IContainer
21from zc.sourcefactory.basic import BasicSourceFactory
22from zc.sourcefactory.contextual import BasicContextualSourceFactory
23from zope import schema
24from zope.component import getUtilitiesFor, queryUtility, getUtility
25from zope.catalog.interfaces import ICatalog
26from zope.interface import Interface, Attribute, implements, directlyProvides
27from zope.schema.interfaces import (
28    ValidationError, ISource, IContextSourceBinder)
29from waeup.kofa.schema import TextLineChoice, FormattedDate
30from waeup.kofa.interfaces import (
31    IKofaObject, year_range, validate_email,
32    SimpleKofaVocabulary)
33from waeup.kofa.interfaces import MessageFactory as _
34from waeup.kofa.payments.interfaces import IOnlinePayment
35from waeup.kofa.schema import PhoneNumber
36from waeup.kofa.students.vocabularies import GenderSource
37from waeup.kofa.university.vocabularies import (
38    course_levels, AppCatSource, CertificateSource)
39
40#: Maximum upload size for applicant passport photographs (in bytes)
41MAX_UPLOAD_SIZE = 1024 * 20
42
43class RegNumInSource(ValidationError):
44    """Registration number exists already
45    """
46    # The docstring of ValidationErrors is used as error description
47    # by zope.formlib.
48    pass
49
50class RegNumberSource(object):
51    implements(ISource)
52    cat_name = 'applicants_catalog'
53    field_name = 'reg_number'
54    validation_error = RegNumInSource
55    def __init__(self, context):
56        self.context = context
57        return
58
59    def __contains__(self, value):
60        cat = queryUtility(ICatalog, self.cat_name)
61        if cat is None:
62            return True
63        kw = {self.field_name: (value, value)}
64        results = cat.searchResults(**kw)
65        for entry in results:
66            if entry.applicant_id != self.context.applicant_id:
67                # XXX: sources should simply return False.
68                #      But then we get some stupid error message in forms
69                #      when validation fails.
70                raise self.validation_error(value)
71                #return False
72        return True
73
74def contextual_reg_num_source(context):
75    source = RegNumberSource(context)
76    return source
77directlyProvides(contextual_reg_num_source, IContextSourceBinder)
78
79
80class AppCatCertificateSource(CertificateSource):
81    """An application certificate source delivers all courses which belong to
82    a certain application_category.
83    """
84    def getValues(self, context):
85        # appliction category not available when certificate was deleted.
86        # shouldn't that info be part of applicant info instead?
87        # when we cannot determine the appcat, we will display all courses.
88        appcat = getattr(getattr(context, '__parent__', None),
89                         'application_category', None)
90        catalog = getUtility(ICatalog, name='certificates_catalog')
91        result = catalog.searchResults(
92            application_category=(appcat,appcat))
93        result = sorted(result, key=lambda value: value.code)
94        curr_course = context.course1
95        if curr_course is not None and curr_course not in result:
96            # display also current course even if it is not catalogued
97            # (any more)
98            result = [curr_course,] + result
99        return result
100
101class ApplicationTypeSource(BasicContextualSourceFactory):
102    """An application type source delivers screening types defined in the
103    portal.
104    """
105    def getValues(self, context):
106        appcats_dict = getUtility(
107            IApplicantsUtils).APP_TYPES_DICT
108        return sorted(appcats_dict.keys())
109
110    def getToken(self, context, value):
111        return value
112
113    def getTitle(self, context, value):
114        appcats_dict = getUtility(
115            IApplicantsUtils).APP_TYPES_DICT
116        return appcats_dict[value][0]
117
118# Maybe Uniben still needs this ...
119#class ApplicationPinSource(BasicContextualSourceFactory):
120#    """An application pin source delivers PIN prefixes for application
121#    defined in the portal.
122#    """
123#    def getValues(self, context):
124#        apppins_dict = getUtility(
125#            IApplicantsUtils).APP_TYPES_DICT
126#        return sorted(appcats_dict.keys())
127#
128#    def getToken(self, context, value):
129#        return value
130#
131#    def getTitle(self, context, value):
132#        apppins_dict = getUtility(
133#            IApplicantsUtils).APP_TYPES_DICT
134#        return u"%s (%s)" % (
135#            apppins_dict[value][1],self.apppins_dict[value][0])
136
137application_modes_vocab = SimpleKofaVocabulary(
138    (_('Create Application Records'), 'create'),
139    (_('Update Application Records'), 'update'),
140    )
141
142class IApplicantsUtils(Interface):
143    """A collection of methods which are subject to customization.
144    """
145
146    APP_TYPES_DICT = Attribute(' dict of application types')
147
148class IApplicantsRoot(IKofaObject, IContainer):
149    """A container for university applicants containers.
150    """
151
152    description = schema.Text(
153        title = _(u'Human readable description in HTML format'),
154        required = False,
155        default = u'''This text can been seen by anonymous users.
156Here we put multi-lingual general information about the application procedure.
157>>de<<
158Dieser Text kann von anonymen Benutzern gelesen werden.
159Hier koennen mehrsprachige Informationen fuer Antragsteller hinterlegt werden.'''
160        )
161
162    description_dict = Attribute(
163        """Content as language dictionary with values in HTML format.""")
164
165class IApplicantsContainer(IKofaObject):
166    """An applicants container contains university applicants.
167
168    """
169
170    code = schema.TextLine(
171        title = _(u'Code'),
172        required = True,
173        readonly = True,
174        )
175
176    title = schema.TextLine(
177        title = _(u'Title'),
178        required = True,
179        readonly = False,
180        )
181
182    prefix = schema.Choice(
183        title = _(u'Application Target'),
184        required = True,
185        source = ApplicationTypeSource(),
186        readonly = True,
187        )
188
189    year = schema.Choice(
190        title = _(u'Year of Entrance'),
191        required = True,
192        values = year_range(),
193        readonly = True,
194        )
195
196    mode = schema.Choice(
197        title = _(u'Application Mode'),
198        vocabulary = application_modes_vocab,
199        required = True,
200        )
201
202    # Maybe Uniben still needs this ...
203    #ac_prefix = schema.Choice(
204    #    title = u'Activation code prefix',
205    #    required = True,
206    #    default = None,
207    #    source = ApplicationPinSource(),
208    #    )
209
210    application_category = schema.Choice(
211        title = _(u'Category for the grouping of certificates'),
212        required = True,
213        source = AppCatSource(),
214        )
215
216    description = schema.Text(
217        title = _(u'Human readable description in HTML format'),
218        required = False,
219        default = u'''This text can been seen by anonymous users.
220Here we put multi-lingual information about the study courses provided, the application procedure and deadlines.
221>>de<<
222Dieser Text kann von anonymen Benutzern gelesen werden.
223Hier koennen mehrsprachige Informationen fuer Antragsteller hinterlegt werden.'''
224        )
225
226    description_dict = Attribute(
227        """Content as language dictionary with values in HTML format.""")
228
229    startdate = schema.Datetime(
230        title = _(u'Application Start Date'),
231        required = False,
232        description = _('Example:') + u'2011-12-01 18:30:00+01:00',
233        )
234
235    enddate = schema.Datetime(
236        title = _(u'Application Closing Date'),
237        required = False,
238        description = _('Example:') + u'2011-12-31 23:59:59+01:00',
239        )
240
241    strict_deadline = schema.Bool(
242        title = _(u'Forbid additions after deadline (enddate)'),
243        required = False,
244        default = True,
245        )
246
247    application_fee = schema.Float(
248        title = _(u'Application Fee'),
249        default = 0.0,
250        required = False,
251        )
252
253    def archive(id=None):
254        """Create on-dist archive of applicants stored in this term.
255
256        If id is `None`, all applicants are archived.
257
258        If id contains a single id string, only the respective
259        applicants are archived.
260
261        If id contains a list of id strings all of the respective
262        applicants types are saved to disk.
263        """
264
265    def clear(id=None, archive=True):
266        """Remove applicants of type given by 'id'.
267
268        Optionally archive the applicants.
269
270        If id is `None`, all applicants are archived.
271
272        If id contains a single id string, only the respective
273        applicants are archived.
274
275        If id contains a list of id strings all of the respective
276        applicant types are saved to disk.
277
278        If `archive` is ``False`` none of the archive-handling is done
279        and respective applicants are simply removed from the
280        database.
281        """
282
283class IApplicantsContainerAdd(IApplicantsContainer):
284    """An applicants container contains university applicants.
285    """
286    prefix = schema.Choice(
287        title = _(u'Application Target'),
288        required = True,
289        source = ApplicationTypeSource(),
290        readonly = False,
291        )
292
293    year = schema.Choice(
294        title = _(u'Year of Entrance'),
295        required = True,
296        values = year_range(),
297        readonly = False,
298        )
299
300IApplicantsContainerAdd[
301    'prefix'].order =  IApplicantsContainer['prefix'].order
302IApplicantsContainerAdd[
303    'year'].order =  IApplicantsContainer['year'].order
304
305class IApplicantBaseData(IKofaObject):
306    """The data for an applicant.
307
308    This is a base interface with no field
309    required. For use with processors, forms, etc., please use one of
310    the derived interfaces below, which set more fields to required
311    state, depending on use-case.
312    """
313
314    history = Attribute('Object history, a list of messages')
315    state = Attribute('The application state of an applicant')
316    display_fullname = Attribute('The fullname of an applicant')
317    application_date = Attribute('Date of submission, used for export only')
318    password = Attribute('Encrypted password of a applicant')
319    application_number = Attribute('The key under which the record is stored')
320
321    applicant_id = schema.TextLine(
322        title = _(u'Applicant Id'),
323        required = False,
324        readonly = False,
325        )
326    reg_number = TextLineChoice(
327        title = _(u'Registration Number'),
328        readonly = False,
329        required = True,
330        source = contextual_reg_num_source,
331        )
332    #access_code = schema.TextLine(
333    #    title = u'Activation Code',
334    #    required = False,
335    #    readonly = True,
336    #    )
337    firstname = schema.TextLine(
338        title = _(u'First Name'),
339        required = True,
340        )
341    middlename = schema.TextLine(
342        title = _(u'Middle Name'),
343        required = False,
344        )
345    lastname = schema.TextLine(
346        title = _(u'Last Name (Surname)'),
347        required = True,
348        )
349    date_of_birth = FormattedDate(
350        title = _(u'Date of Birth'),
351        required = False,
352        #date_format = u'%d/%m/%Y', # Use grok-instance-wide default
353        show_year = True,
354        )
355    sex = schema.Choice(
356        title = _(u'Sex'),
357        source = GenderSource(),
358        required = True,
359        )
360    email = schema.ASCIILine(
361        title = _(u'Email Address'),
362        required = False,
363        constraint=validate_email,
364        )
365    phone = PhoneNumber(
366        title = _(u'Phone'),
367        description = u'',
368        required = False,
369        )
370    course1 = schema.Choice(
371        title = _(u'1st Choice Course of Study'),
372        source = AppCatCertificateSource(),
373        required = True,
374        )
375    course2 = schema.Choice(
376        title = _(u'2nd Choice Course of Study'),
377        source = AppCatCertificateSource(),
378        required = False,
379        )
380    #school_grades = schema.List(
381    #    title = _(u'School Grades'),
382    #    value_type = ResultEntryField(),
383    #    required = False,
384    #    default = [],
385    #    )
386
387    notice = schema.Text(
388        title = _(u'Notice'),
389        required = False,
390        )
391    screening_venue = schema.TextLine(
392        title = _(u'Screening Venue'),
393        required = False,
394        )
395    screening_score = schema.Int(
396        title = _(u'Screening Score'),
397        required = False,
398        )
399    student_id = schema.TextLine(
400        title = _(u'Student Id'),
401        required = False,
402        readonly = False,
403        )
404    course_admitted = schema.Choice(
405        title = _(u'Admitted Course of Study'),
406        source = CertificateSource(),
407        required = False,
408        )
409    locked = schema.Bool(
410        title = _(u'Form locked'),
411        default = False,
412        )
413
414class IApplicant(IApplicantBaseData):
415    """An applicant.
416
417    This is basically the applicant base data. Here we repeat the
418    fields from base data if we have to set the `required` attribute
419    to True (which is the default).
420    """
421
422    def loggerInfo(ob_class, comment):
423        """Adds an INFO message to the log file
424        """
425
426    def createStudent():
427        """Create a student object from applicatnt data
428        and copy applicant object.
429        """
430
431class IApplicantEdit(IApplicant):
432    """An applicant interface for editing.
433
434    Here we can repeat the fields from base data and set the
435    `required` and `readonly` attributes to True to further restrict
436    the data access. Or we can allow only certain certificates to be
437    selected by choosing the appropriate source.
438
439    We cannot omit fields here. This has to be done in the
440    respective form page.
441    """
442
443    email = schema.ASCIILine(
444        title = _(u'Email Address'),
445        required = True,
446        constraint=validate_email,
447        )
448    course1 = schema.Choice(
449        title = _(u'1st Choice Course of Study'),
450        source = AppCatCertificateSource(),
451        required = True,
452        )
453    course2 = schema.Choice(
454        title = _(u'2nd Choice Course of Study'),
455        source = AppCatCertificateSource(),
456        required = False,
457        )
458    screening_score = schema.Int(
459        title = _(u'Screening Score'),
460        required = False,
461        readonly = True,
462        )
463    screening_venue = schema.TextLine(
464        title = _(u'Screening Venue'),
465        required = False,
466        readonly = True,
467        )
468    course_admitted = schema.Choice(
469        title = _(u'Admitted Course of Study'),
470        source = CertificateSource(),
471        required = False,
472        readonly = True,
473        )
474    notice = schema.Text(
475        title = _(u'Notice'),
476        required = False,
477        readonly = True,
478        )
479
480IApplicantEdit['email'].order = IApplicantEdit[
481    'sex'].order
482
483class IApplicantUpdateByRegNo(IApplicant):
484    """Representation of an applicant.
485
486    Skip regular reg_number validation if reg_number is used for finding
487    the applicant object.
488    """
489    reg_number = schema.TextLine(
490        title = u'Registration Number',
491        required = False,
492        )
493
494class IApplicantRegisterUpdate(IApplicant):
495    """Representation of an applicant for first-time registration.
496
497    This interface is used when apllicants use the registration page to
498    update their records.
499    """
500    reg_number = schema.TextLine(
501        title = u'Registration Number',
502        required = True,
503        )
504
505    firstname = schema.TextLine(
506        title = _(u'First Name'),
507        required = True,
508        )
509
510    email = schema.ASCIILine(
511        title = _(u'Email Address'),
512        required = True,
513        constraint=validate_email,
514        )
515
516class IApplicantOnlinePayment(IOnlinePayment):
517    """An applicant payment via payment gateways.
518
519    """
520
521    def doAfterApplicantPayment():
522        """Process applicant after payment was made.
523
524        """
525
526    def doAfterApplicantPaymentApproval():
527        """Process applicant after payment was approved.
528
529        """
530
531    def approveApplicantPayment():
532        """Approve payment and process applicant.
533
534        """
Note: See TracBrowser for help on using the repository browser.