Skip to content

Commit

Permalink
Refactor for re-use and to reduce duplicate code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Howitz committed May 24, 2016
1 parent 60ff2c6 commit 797cd3e
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/z3c/form/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,21 @@ 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"""

def _canQueryCurrentValue(self):
return (interfaces.IContextAware.providedBy(self.widget) and
not self.widget.ignoreContext)

def _queryCurrentValue(self):
return zope.component.getMultiAdapter(
(self.widget.context, self.field),
interfaces.IDataManager).query()

def getTerm(self, value):
try:
return super(MissingTermsMixin, self).getTerm(value)
except LookupError:
if (interfaces.IContextAware.providedBy(self.widget) and
not self.widget.ignoreContext):
curValue = zope.component.getMultiAdapter(
(self.widget.context, self.field),
interfaces.IDataManager).query()
if self._canQueryCurrentValue():
curValue = self._queryCurrentValue()
if curValue == value:
return self._makeMissingTerm(value)

Expand All @@ -164,11 +170,8 @@ def getTermByToken(self, token):
try:
return super(MissingTermsMixin, self).getTermByToken(token)
except LookupError:
if (interfaces.IContextAware.providedBy(self.widget) and
not self.widget.ignoreContext):
value = zope.component.getMultiAdapter(
(self.widget.context, self.field),
interfaces.IDataManager).query()
if self._canQueryCurrentValue():
value = self._queryCurrentValue()
term = self._makeMissingTerm(value)
if term.token == token:
# check if the given token matches the value, if not
Expand Down

0 comments on commit 797cd3e

Please sign in to comment.