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

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

Show containers by default. Only anonymous users must not see hidden containers.

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