Skip to content

Commit

Permalink
Add missing method.
Browse files Browse the repository at this point in the history
`CollectionSequenceDataConverter` uses `getValue()` so this message has been
implemented along the other methods for missing values.
  • Loading branch information
Michael Howitz committed May 29, 2016
1 parent 27f5436 commit 63edef9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/z3c/form/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ def getTermByToken(self, token):
raise LookupError(token)

def getValue(self, token):
return self.terms.getValue(token)
try:
return self.terms.getValue(token)
except KeyError:
raise LookupError(token)

def __iter__(self):
for value in self.source:
Expand Down Expand Up @@ -294,7 +297,21 @@ def getTermByToken(self, token):
# fall back on LookupError, otherwise we might accept
# any crap coming from the request
return term
raise LookupError(token)
raise

def getValue(self, token):
try:
return super(MissingCollectionTermsMixin, self).getValue(token)
except LookupError:
if self._canQueryCurrentValue():
for value in self._queryCurrentValue():
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
# any crap coming from the request
return value
raise


class MissingCollectionTermsVocabulary(MissingCollectionTermsMixin,
Expand Down
13 changes: 13 additions & 0 deletions src/z3c/form/term.txt
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,14 @@ The same goes with looking up a term by value:
...
LookupError: 42

The same goes with looking up a value by the token:

>>> terms.getValue('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.

Expand Down Expand Up @@ -598,6 +606,11 @@ Override ``makeMissingTerm`` if you want your own.
>>> missingTerm.title
u'Missing: ${value}'

We can get the value for a missing term:

>>> terms.getValue('42')
42

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

>>> missingTerm = terms.getTermByToken('99')
Expand Down

0 comments on commit 63edef9

Please sign in to comment.