Skip to content

Commit

Permalink
It ist wrong that MissingCollectionTermsMixin extends `MissingTerms…
Browse files Browse the repository at this point in the history
…Mixin` because the `super()` call then leads to the wrong method (aka the one on `MissingTermsMixin` instead of the one on the class where the mix-in is used on.
  • Loading branch information
Michael Howitz committed May 28, 2016
1 parent 3829fea commit 27f5436
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/z3c/form/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ def __init__(self, context, request, form, field, vocabulary, widget):
self.terms = vocabulary


class MissingTermsMixin(object):
"""This can be used in case previous values/tokens get missing
from the vocabulary and you still need to display/keep the values"""
class MissingTermsBase(object):
"""Base class for MissingTermsMixin classes."""

def _canQueryCurrentValue(self):
return (interfaces.IContextAware.providedBy(self.widget) and
Expand All @@ -144,17 +143,6 @@ def _queryCurrentValue(self):
(self.widget.context, self.field),
interfaces.IDataManager).query()

def getTerm(self, value):
try:
return super(MissingTermsMixin, self).getTerm(value)
except LookupError:
if self._canQueryCurrentValue():
curValue = self._queryCurrentValue()
if curValue == value:
return self._makeMissingTerm(value)

raise

def _makeToken(self, value):
"""create a unique valid ASCII token"""
return util.createCSSId(util.toUnicode(value))
Expand All @@ -166,6 +154,21 @@ def _makeMissingTerm(self, value):
value, self._makeToken(value),
title=_(u'Missing: ${value}', mapping=dict(value=uvalue)))


class MissingTermsMixin(MissingTermsBase):
"""This can be used in case previous values/tokens get missing
from the vocabulary and you still need to display/keep the values"""

def getTerm(self, value):
try:
return super(MissingTermsMixin, self).getTerm(value)
except LookupError:
if self._canQueryCurrentValue():
curValue = self._queryCurrentValue()
if curValue == value:
return self._makeMissingTerm(value)
raise

def getTermByToken(self, token):
try:
return super(MissingTermsMixin, self).getTermByToken(token)
Expand Down Expand Up @@ -266,7 +269,7 @@ def __init__(self, context, request, form, field, vocabulary, widget):
self.terms = vocabulary


class MissingCollectionTermsMixin(MissingTermsMixin):
class MissingCollectionTermsMixin(MissingTermsBase):
"""`MissingTermsMixin` adapted to collections."""

def getTerm(self, value):
Expand Down

0 comments on commit 27f5436

Please sign in to comment.