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

Last change on this file since 10436 was 10384, checked in by Henrik Bettermann, 12 years ago

'locked' is not required.

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