Skip to content

Commit

Permalink
adding tests and fixing on the way
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Groszer committed Sep 7, 2012
1 parent ef304f3 commit 4d94984
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 16 deletions.
25 changes: 16 additions & 9 deletions src/z3c/form/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,16 +133,23 @@ def getTerm(self, value):
try:
return self.terms.getTerm(value)
except LookupError:
return self.makeMissingTerm(value)
if (interfaces.IContextAware.providedBy(self.widget) and
not self.widget.ignoreContext):
curValue = zope.component.getMultiAdapter(
(self.widget.context, self.field),
interfaces.IDataManager).query()
if curValue == value:
return self._makeMissingTerm(value)

raise

def makeToken(self, value):
# create a unique valid ASCII token
def _makeToken(self, value):
"""create a unique valid ASCII token"""
return util.createCSSId(unicode(value))

def makeMissingTerm(self, value):
"""Return a term that should be displayed for the missing token or
raise LookupError if it's really invalid"""
return vocabulary.SimpleTerm(value, self.makeToken(value),
def _makeMissingTerm(self, value):
"""Return a term that should be displayed for the missing token"""
return vocabulary.SimpleTerm(value, self._makeToken(value),
title=_(u'Missing: ${value}', mapping=dict(value=unicode(value))))

def getTermByToken(self, token):
Expand All @@ -152,9 +159,9 @@ def getTermByToken(self, token):
if (interfaces.IContextAware.providedBy(self.widget) and
not self.widget.ignoreContext):
value = zope.component.getMultiAdapter(
(self.widget.context, self.widget.field),
(self.widget.context, self.field),
interfaces.IDataManager).query()
term = self.makeMissingTerm(value)
term = self._makeMissingTerm(value)
if term.token == token:
# check if the given token matches the value, if not
# fall back on LookupError, otherwise we might accept
Expand Down
80 changes: 76 additions & 4 deletions src/z3c/form/term.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,84 @@ stored objects still reference that term.
>>> terms = term.ChoiceTerms(
... None, request, None, ratingField, widget)
>>> term = terms.getTermByToken('42')
>>> term.token
Traceback (most recent call last):
...
LookupError: 42

The same goes with looking up a term by value:

>>> term = terms.getTerm('42')
Traceback (most recent call last):
...
LookupError: 42

Ooops, well this works only if the context has the right value for us.
This is because we don't want to accept any crap that's coming from HTML.

>>> class IPerson(zope.interface.Interface):
... gender = zope.schema.Choice(title=u'Gender', vocabulary='Genders')
>>> class Person(object):
... zope.interface.implements(IPerson)
... gender = None
>>> gendersVocabulary = vocabulary.SimpleVocabulary([
... vocabulary.SimpleVocabulary.createTerm(1, 'male', u'Male'),
... vocabulary.SimpleVocabulary.createTerm(2, 'female', u'Female'),
... ])
>>> def GendersVocabulary(obj):
... return ratings
>>> vr.register('Genders', GendersVocabulary)

>>> ctx = Person()
>>> ctx.gender = 42

>>> genderWidget = z3c.form.widget.Widget(request)
>>> genderWidget.context = ctx
>>> from z3c.form import interfaces
>>> zope.interface.alsoProvides(genderWidget, interfaces.IContextAware)
>>> from z3c.form.datamanager import AttributeField
>>> zope.component.provideAdapter(AttributeField)

>>> terms = term.ChoiceTerms(
... ctx, request, None, IPerson['gender'], genderWidget)

Here we go:

>>> missingTerm = terms.getTermByToken('42')

We get the term, we passed the token, the value is coming from the context.

>>> missingTerm.token
'42'
>>> term.value
>>> missingTerm.value
42

We cannot figure the title, so we construct one.
Override ``makeMissingTerm`` if you want your own.

>>> missingTerm.title
u'Missing: ${value}'

Still we raise LookupError if the token does not fit the context's value:

>>> missingTerm = terms.getTermByToken('99')
Traceback (most recent call last):
...
LookupError: 99

The same goes with looking up a term by value.
We get the term if the context's value fits:

>>> missingTerm = terms.getTerm(42)
>>> missingTerm.token
'42'
>>> term.title
u'Missing: ${token}'

And an exception if it does not:

>>> missingTerm = terms.getTerm(99)
Traceback (most recent call last):
...
LookupError: 99


Bool fields
+++++++++++
Expand Down
4 changes: 2 additions & 2 deletions src/z3c/form/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def validate(self, value):
"""See interfaces.IValidator"""
if value is interfaces.NOT_CHANGED:
# no need to validate unchanged values
return True
return

context = self.context
field = self.field
Expand All @@ -61,7 +61,7 @@ def validate(self, value):

if widget and not util.changedWidget(widget, value, field=field):
# if new value == old value, no need to validate
return True
return
return field.validate(value)

def __repr__(self):
Expand Down
3 changes: 2 additions & 1 deletion src/z3c/form/validator.txt
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ field is required the other one not:

>>> class IPhoto(zope.interface.Interface):
... data = zope.schema.Bytes(
... title=u'Photo')
... title=u'Photo',
... required=True)
...
... thumb = zope.schema.Bytes(
... title=u'Thumbnail',
Expand Down

0 comments on commit 4d94984

Please sign in to comment.