Ignore:
Timestamp:
18 Sep 2011, 00:07:07 (13 years ago)
Author:
uli
Message:

Add contextual sources for reg_numbers and mat_numbers.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/students/vocabularies.py

    r6744 r6787  
    22"""
    33from datetime import datetime
    4 from zope.component import getUtility
     4from zope.component import getUtility, queryUtility
    55from zope.catalog.interfaces import ICatalog
     6from zope.interface import implements, directlyProvides
     7from zope.schema.interfaces import ISource, IContextSourceBinder
     8from zope.schema.interfaces import ValidationError
    69from zc.sourcefactory.basic import BasicSourceFactory
    710from zc.sourcefactory.contextual import BasicContextualSourceFactory
     
    912from waeup.sirp.students.lgas import LGAS
    1013from waeup.sirp.university.vocabularies import course_levels
     14
     15
    1116
    1217def year_range():
     
    3338    except AttributeError:
    3439        return []
    35    
     40
    3641class StudyLevelSource(BasicContextualSourceFactory):
    3742    """The StudyLevelSource is based on and extends the
     
    130135        if value == 'f':
    131136            return 'Female'
     137
     138class RegNumNotInSource(ValidationError):
     139    """Registration number exists already
     140    """
     141    # The docstring of ValidationErrors is used as error description
     142    # by zope.formlib.
     143    pass
     144
     145class MatNumNotInSource(ValidationError):
     146    """Matriculation number exists already
     147    """
     148    # The docstring of ValidationErrors is used as error description
     149    # by zope.formlib.
     150    pass
     151
     152class RegNumberSource(object):
     153    implements(ISource)
     154    cat_name = 'students_catalog'
     155    field_name = 'reg_number'
     156    validation_error = RegNumNotInSource
     157    def __init__(self, context):
     158        self.context = context
     159        return
     160
     161    def __contains__(self, value):
     162        cat = queryUtility(ICatalog, self.cat_name)
     163        if cat is None:
     164            return True
     165        kw = {self.field_name: (value, value)}
     166        results = cat.searchResults(**kw)
     167        for entry in results:
     168            if entry.student_id != self.context.student_id:
     169                # XXX: sources should simply return False.
     170                #      But then we get some stupid error message in forms
     171                #      when validation fails.
     172                raise self.validation_error(value)
     173                #return False
     174        return True
     175
     176def contextual_reg_num_source(context):
     177    source = RegNumberSource(context)
     178    return source
     179directlyProvides(contextual_reg_num_source, IContextSourceBinder)
     180
     181class MatNumberSource(RegNumberSource):
     182    field_name = 'matric_number'
     183    validation_error = MatNumNotInSource
     184
     185def contextual_mat_num_source(context):
     186    source = MatNumberSource(context)
     187    return source
     188directlyProvides(contextual_mat_num_source, IContextSourceBinder)
Note: See TracChangeset for help on using the changeset viewer.