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

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

Doing the same (see last revisions) in the applicants package.

  • Property svn:keywords set to Id
File size: 19.6 KB
Line 
1## $Id: interfaces.py 7844 2012-03-12 11:38:09Z 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
30from waeup.kofa.interfaces import (
31    IKofaObject, year_range, validate_email, academic_sessions_vocab)
32from waeup.kofa.interfaces import MessageFactory as _
33from waeup.kofa.payments.interfaces import IOnlinePayment
34from waeup.kofa.schoolgrades import ResultEntryField
35from waeup.kofa.students.vocabularies import (
36    lgas_vocab, CertificateSource, GenderSource)
37from waeup.kofa.university.vocabularies import (
38    course_levels, AppCatSource)
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
137class ApplicantContainerProviderSource(BasicSourceFactory):
138    """A source offering all available applicants container types.
139
140    The values returned by this source are names of utilities that can
141    create :class:`ApplicantContainer` instances. So, if you get a
142    name like ``'myactype'`` from this source, then you can do:
143
144      >>> from zope.component import getUtility
145      >>> p = getUtility(IApplicantsContainerProvider, name=myactype)
146      >>> my_applicants_container = p.factory()
147
148    Or you can access class-attributes like
149
150      >>> my_applicants_container.container_title
151      'Pretty'
152
153    """
154    def getValues(self):
155        """Returns a list of ``(<name>, <provider>)`` tuples.
156
157        Here ``<name>`` is the name under which an
158        :class:``ApplicantContainerProvider`` was registered as a
159        utility and ``<provider>`` is the utility itself.
160        """
161        return getUtilitiesFor(IApplicantsContainerProvider)
162
163    def getToken(self, value):
164        """Return the name of the ``(<name>, <provider>)`` tuple.
165        """
166        return value[0]
167
168    def getTitle(self, value):
169        """Get a 'title - description' string for a container type.
170        """
171        factory = value[1].factory
172        return "%s - %s" % (
173            factory.container_title, factory.container_description)
174
175class IApplicantsUtils(Interface):
176    """A collection of methods which are subject to customization.
177    """
178
179    APP_TYPES_DICT = Attribute(' dict of application types')
180
181class IApplicantsRoot(IKofaObject, IContainer):
182    """A container for university applicants containers.
183    """
184    pass
185
186class IApplicantsContainer(IKofaObject):
187    """An applicants container contains university applicants.
188
189    """
190
191    container_title = Attribute(
192        u'classattribute: title for type of container')
193    container_description = Attribute(
194        u'classattribute: description for type of container')
195
196
197    code = schema.TextLine(
198        title = _(u'Code'),
199        default = u'-',
200        required = True,
201        readonly = True,
202        )
203
204    title = schema.TextLine(
205        title = _(u'Title'),
206        required = True,
207        default = u'-',
208        readonly = True,
209        )
210
211    prefix = schema.Choice(
212        title = _(u'Application Target'),
213        required = True,
214        default = None,
215        source = ApplicationTypeSource(),
216        readonly = True,
217        )
218
219    entry_level = schema.Choice(
220        title = _(u'Entry Level'),
221        vocabulary = course_levels,
222        default = 100,
223        required = True,
224        )
225
226    year = schema.Choice(
227        title = _(u'Year of Entrance'),
228        required = True,
229        default = None,
230        values = year_range(),
231        readonly = True,
232        )
233
234    provider = schema.Choice(
235        title = _(u'Applicants Container Type'),
236        required = True,
237        default = None,
238        source = ApplicantContainerProviderSource(),
239        readonly = True,
240        )
241
242    # Maybe Uniben still needs this ...
243    #ac_prefix = schema.Choice(
244    #    title = u'Activation code prefix',
245    #    required = True,
246    #    default = None,
247    #    source = ApplicationPinSource(),
248    #    )
249
250    application_category = schema.Choice(
251        title = _(u'Category for the grouping of certificates'),
252        required = True,
253        default = None,
254        source = AppCatSource(),
255        )
256
257    description = schema.Text(
258        title = _(u'Human readable description in reST format'),
259        required = False,
260        default = u'''This text can been seen by anonymous users.
261Here we put mult-lingual information about the study courses provided, the application procedure and deadlines.
262>>de<<
263Dieser Text kann von anonymen Benutzern gelesen werden.
264Hier koennen mehrsprachige Informationen fuer Antragsteller hinterlegt werden.'''
265        )
266
267    description_dict = schema.Dict(
268        title = u'Content as language dictionary with values in html format',
269        required = False,
270        default = {},
271        )
272
273    startdate = schema.Date(
274        title = _(u'Application Start Date'),
275        required = False,
276        default = None,
277        )
278
279    enddate = schema.Date(
280        title = _(u'Application Closing Date'),
281        required = False,
282        default = None,
283        )
284
285    strict_deadline = schema.Bool(
286        title = _(u'Forbid additions after deadline (enddate)'),
287        required = True,
288        default = True,
289        )
290
291    def archive(id=None):
292        """Create on-dist archive of applicants stored in this term.
293
294        If id is `None`, all applicants are archived.
295
296        If id contains a single id string, only the respective
297        applicants are archived.
298
299        If id contains a list of id strings all of the respective
300        applicants types are saved to disk.
301        """
302
303    def clear(id=None, archive=True):
304        """Remove applicants of type given by 'id'.
305
306        Optionally archive the applicants.
307
308        If id is `None`, all applicants are archived.
309
310        If id contains a single id string, only the respective
311        applicants are archived.
312
313        If id contains a list of id strings all of the respective
314        applicant types are saved to disk.
315
316        If `archive` is ``False`` none of the archive-handling is done
317        and respective applicants are simply removed from the
318        database.
319        """
320
321class IApplicantsContainerAdd(IApplicantsContainer):
322    """An applicants container contains university applicants.
323    """
324    prefix = schema.Choice(
325        title = _(u'Application Target'),
326        required = True,
327        default = None,
328        source = ApplicationTypeSource(),
329        readonly = False,
330        )
331
332    year = schema.Choice(
333        title = _(u'Year of Entrance'),
334        required = True,
335        default = None,
336        values = year_range(),
337        readonly = False,
338        )
339
340    provider = schema.Choice(
341        title = _(u'Applicants Container Type'),
342        required = True,
343        default = None,
344        source = ApplicantContainerProviderSource(),
345        readonly = False,
346        )
347
348IApplicantsContainerAdd[
349    'prefix'].order =  IApplicantsContainer['prefix'].order
350IApplicantsContainerAdd[
351    'year'].order =  IApplicantsContainer['year'].order
352IApplicantsContainerAdd[
353    'provider'].order =  IApplicantsContainer['provider'].order
354
355class IApplicantBaseData(IKofaObject):
356    """The data for an applicant.
357
358    This is a base interface with no field
359    required. For use with importers, forms, etc., please use one of
360    the derived interfaces below, which set more fields to required
361    state, depending on use-case.
362
363    This base interface is also implemented by the
364    :class:`waeup.kofa.students.StudentApplication` class in the
365    students package. Thus, these are the data which are saved after
366    admission.
367    """
368    applicant_id = schema.TextLine(
369        title = _(u'Applicant Id'),
370        required = False,
371        readonly = False,
372        )
373    reg_number = TextLineChoice(
374        title = _(u'JAMB Registration Number'),
375        readonly = False,
376        required = True,
377        default = None,
378        source = contextual_reg_num_source,
379        )
380    #access_code = schema.TextLine(
381    #    title = u'Activation Code',
382    #    required = False,
383    #    readonly = True,
384    #    )
385    firstname = schema.TextLine(
386        title = _(u'First Name'),
387        required = True,
388        )
389    middlename = schema.TextLine(
390        title = _(u'Middle Name'),
391        required = False,
392        )
393    lastname = schema.TextLine(
394        title = _(u'Last Name (Surname)'),
395        required = True,
396        )
397    date_of_birth = schema.Date(
398        title = _(u'Date of Birth'),
399        required = True,
400        )
401    lga = schema.Choice(
402        source = lgas_vocab,
403        title = _(u'State/LGA'),
404        default = 'foreigner',
405        required = True,
406        )
407    sex = schema.Choice(
408        title = _(u'Sex'),
409        source = GenderSource(),
410        default = u'm',
411        required = True,
412        )
413    email = schema.ASCIILine(
414        title = _(u'Email Address'),
415        required = True,
416        constraint=validate_email,
417        )
418    phone = schema.TextLine(
419        title = _(u'Phone'),
420        description = u'',
421        required = False,
422        )
423    course1 = schema.Choice(
424        title = _(u'1st Choice Course of Study'),
425        source = CertificateSource(),
426        required = True,
427        )
428    course2 = schema.Choice(
429        title = _(u'2nd Choice Course of Study'),
430        source = CertificateSource(),
431        required = False,
432        )
433    school_grades = schema.List(
434        title = _(u'School Grades'),
435        value_type = ResultEntryField(),
436        required = True,
437        default = [],
438        )
439
440    #
441    # Data to be imported after screening
442    #
443    screening_score = schema.Int(
444        title = _(u'Screening Score'),
445        required = False,
446        )
447    screening_venue = schema.TextLine(
448        title = _(u'Screening Venue'),
449        required = False,
450        )
451    course_admitted = schema.Choice(
452        title = _(u'Admitted Course of Study'),
453        source = CertificateSource(),
454        default = None,
455        required = False,
456        )
457    notice = schema.Text(
458        title = _(u'Notice'),
459        required = False,
460        )
461
462class IApplicantProcessData(IApplicantBaseData):
463    """An applicant.
464
465    Here we add process attributes and methods to the base data.
466    """
467
468    history = Attribute('Object history, a list of messages')
469    state = Attribute('The application state of an applicant')
470    display_fullname = Attribute('The fullname of an applicant')
471    application_date = Attribute('Date of submission, used for export only')
472    password = Attribute('Encrypted password of a applicant')
473    application_number = Attribute('The key under which the record is stored')
474
475    def loggerInfo(ob_class, comment):
476        """Adds an INFO message to the log file
477        """
478
479    student_id = schema.TextLine(
480        title = _(u'Student Id'),
481        required = False,
482        readonly = False,
483        )
484    locked = schema.Bool(
485        title = _(u'Form locked'),
486        default = False,
487        )
488
489class IApplicant(IApplicantProcessData):
490    """An applicant.
491
492    This is basically the applicant base data. Here we repeat the
493    fields from base data if we have to set the `required` attribute
494    to True (which is the default).
495    """
496
497class IApplicantEdit(IApplicantProcessData):
498    """An applicant.
499
500    Here we can repeat the fields from base data and set the
501    `required` and `readonly` attributes to True to further restrict
502    the data access. Or we can allow only certain certificates to be
503    selected by choosing the appropriate source.
504
505    We cannot omit fields here. This has to be done in the
506    respective form page.
507    """
508
509    course1 = schema.Choice(
510        title = _(u'1st Choice Course of Study'),
511        source = AppCatCertificateSource(),
512        required = True,
513        )
514    course2 = schema.Choice(
515        title = _(u'2nd Choice Course of Study'),
516        source = AppCatCertificateSource(),
517        required = False,
518        )
519    screening_score = schema.Int(
520        title = _(u'Screening Score'),
521        required = False,
522        readonly = True,
523        )
524    screening_venue = schema.TextLine(
525        title = _(u'Screening Venue'),
526        required = False,
527        readonly = True,
528        )
529    course_admitted = schema.Choice(
530        title = _(u'Admitted Course of Study'),
531        source = CertificateSource(),
532        default = None,
533        required = False,
534        readonly = True,
535        )
536    notice = schema.Text(
537        title = _(u'Notice'),
538        required = False,
539        readonly = True,
540        )
541
542    def createStudent():
543        """Create a student object from applicatnt data
544        and copy applicant object.
545        """
546
547class IApplicantUpdateByRegNo(IApplicant):
548    """Representation of an applicant.
549
550    Skip regular reg_number validation if reg_number is used for finding
551    the applicant object.
552    """
553    reg_number = schema.TextLine(
554        title = u'Registration Number',
555        default = None,
556        required = False,
557        )
558
559class IApplicantOnlinePayment(IOnlinePayment):
560    """An applicant payment via payment gateways.
561
562    """
563    p_year = schema.Choice(
564        title = _(u'Payment Session'),
565        source = academic_sessions_vocab,
566        required = False,
567        )
568
569IApplicantOnlinePayment['p_year'].order = IApplicantOnlinePayment[
570    'p_year'].order
571
572class IApplicantsContainerProvider(Interface):
573    """A provider for applicants containers.
574
575    Applicants container providers are meant to be looked up as
576    utilities. This way we can find all applicant container types
577    defined somewhere.
578
579    Each applicants container provider registered as utility provides
580    one container type and one should be able to call the `factory`
581    attribute to create an instance of the requested container type.
582
583    .. THE FOLLOWING SHOULD GO INTO SPHINX DOCS (and be tested)
584
585    Samples:
586
587    Given, you had an IApplicantsContainer implementation somewhere
588    and you would like to make it findable on request, then you would
589    normally create an appropriate provider utility like this::
590
591      import grok
592      from waeup.kofa.applicants.interfaces import IApplicantsContainerProvider
593
594      class MyContainerProvider(grok.GlobalUtility):
595          grok.implements(IApplicantsContainerProvider)
596          grok.name('MyContainerProvider') # Must be unique
597          factory = MyContainer # A class implementing IApplicantsContainer
598                                # or derivations thereof.
599
600    This utility would be registered on startup and could then be used
601    like this:
602
603      >>> from zope.component import getAllUtilitiesRegisteredFor
604      >>> from waeup.kofa.applicants.interfaces import (
605      ...     IApplicantsContainerProvider)
606      >>> all_providers = getAllUtilitiesRegisteredFor(
607      ...     IApplicantsContainerProvider)
608      >>> all_providers
609      [<MyContainerProvider object at 0x...>]
610
611    You could look up this specific provider by name:
612
613      >>> from zope.component import getUtility
614      >>> p = getUtility(IApplicantsContainerProvider, name='MyProvider')
615      >>> p
616      <MyContainerProvider object at 0x...>
617
618    An applicants container would then be created like this:
619
620      >>> provider = all_providers[0]
621      >>> container = provider.factory()
622      >>> container
623      <MyContainer object at 0x...>
624
625    """
626    factory = Attribute("A class that can create instances of the "
627                        "requested container type")
Note: See TracBrowser for help on using the repository browser.