Ignore:
Timestamp:
4 Dec 2011, 12:59:40 (13 years ago)
Author:
Henrik Bettermann
Message:

Add tests for applicant batch importer.

Make reg_no filed unique.

Two tests still fail because the importer only accepts the application_number as location field and does not yet search for registration numbers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/applicants/interfaces.py

    r7262 r7263  
    2222
    2323from zope import schema
    24 from zope.interface import Interface, Attribute
    25 from zope.component import getUtilitiesFor
     24from zope.interface import Interface, Attribute, implements, directlyProvides
     25from zope.component import getUtilitiesFor, queryUtility
     26from zope.catalog.interfaces import ICatalog
     27from zope.schema.interfaces import ValidationError, ISource, IContextSourceBinder
    2628from zc.sourcefactory.basic import BasicSourceFactory
     29from waeup.sirp.schema import TextLineChoice
    2730from waeup.sirp.interfaces import (
    2831    IWAeUPObject, year_range, validate_email, academic_sessions_vocab)
     
    4043MAX_UPLOAD_SIZE = 1024 * 20
    4144
     45class RegNumInSource(ValidationError):
     46    """Registration number exists already
     47    """
     48    # The docstring of ValidationErrors is used as error description
     49    # by zope.formlib.
     50    pass
     51
     52class RegNumberSource(object):
     53    implements(ISource)
     54    cat_name = 'applicants_catalog'
     55    field_name = 'reg_no'
     56    validation_error = RegNumInSource
     57    def __init__(self, context):
     58        self.context = context
     59        return
     60
     61    def __contains__(self, value):
     62        cat = queryUtility(ICatalog, self.cat_name)
     63        if cat is None:
     64            return True
     65        kw = {self.field_name: (value, value)}
     66        results = cat.searchResults(**kw)
     67        for entry in results:
     68            if entry.applicant_id != self.context.applicant_id:
     69                # XXX: sources should simply return False.
     70                #      But then we get some stupid error message in forms
     71                #      when validation fails.
     72                raise self.validation_error(value)
     73                #return False
     74        return True
     75
     76def contextual_reg_num_source(context):
     77    source = RegNumberSource(context)
     78    return source
     79directlyProvides(contextual_reg_num_source, IContextSourceBinder)
     80
    4281class ApplicantContainerProviderSource(BasicSourceFactory):
    4382    """A source offering all available applicants container types.
     
    259298        readonly = False,
    260299        )
    261     reg_no = schema.TextLine(
     300    reg_no = TextLineChoice(
    262301        title = u'JAMB Registration Number',
    263         readonly = True,
    264         required = False,
     302        readonly = False,
     303        required = True,
     304        default = None,
     305        source = contextual_reg_num_source,
    265306        )
    266307    access_code = schema.TextLine(
Note: See TracChangeset for help on using the changeset viewer.