Changeset 8766


Ignore:
Timestamp:
20 Jun 2012, 09:13:49 (13 years ago)
Author:
uli
Message:

Make test error disappear.

File:
1 edited

Legend:

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

    r8471 r8766  
    140140
    141141class RegNumberSource(object):
     142    """A source that accepts any entry for a certain field if not used
     143    already.
     144
     145    Using this kind of source means a way of setting an invariant.
     146
     147    We accept a value iff:
     148    - the value cannot be found in catalog or
     149    - the value can be found as part of some item but the bound item
     150      is the context object itself.
     151    """
    142152    implements(ISource)
    143153    cat_name = 'students_catalog'
    144154    field_name = 'reg_number'
    145155    validation_error = RegNumNotInSource
     156    comp_field = 'student_id'
    146157    def __init__(self, context):
    147158        self.context = context
     
    149160
    150161    def __contains__(self, value):
     162        """We accept all values not already given to other students.
     163        """
    151164        cat = queryUtility(ICatalog, self.cat_name)
    152165        if cat is None:
     
    155168        results = cat.searchResults(**kw)
    156169        for entry in results:
    157             if entry.student_id != self.context.student_id:
    158                 # XXX: sources should simply return False.
    159                 #      But then we get some stupid error message in forms
    160                 #      when validation fails.
     170            if not hasattr(self.context, self.comp_field):
     171                # we have no context with comp_field (most probably
     172                # while adding a new object, where the container is
     173                # the context) which means that the value was given
     174                # already to another object (as _something_ was found in
     175                # the catalog with that value). Fail on first round.
     176                raise self.validation_error(value)
     177            if getattr(entry, self.comp_field) != getattr(
     178                self.context, self.comp_field):
     179                # An entry already given to another student is not in our
     180                # range of acceptable values.
    161181                raise self.validation_error(value)
    162182                #return False
Note: See TracChangeset for help on using the changeset viewer.