本文整理匯總了Python中zope.schema.interfaces.IContextSourceBinder.providedBy方法的典型用法代碼示例。如果您正苦於以下問題:Python IContextSourceBinder.providedBy方法的具體用法?Python IContextSourceBinder.providedBy怎麽用?Python IContextSourceBinder.providedBy使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類zope.schema.interfaces.IContextSourceBinder
的用法示例。
在下文中一共展示了IContextSourceBinder.providedBy方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from zope.schema.interfaces import IContextSourceBinder [as 別名]
# 或者: from zope.schema.interfaces.IContextSourceBinder import providedBy [as 別名]
def __init__(self, values=None, vocabulary=None, source=None, **kw):
"""Initialize object."""
if vocabulary is not None:
assert (isinstance(vocabulary, basestring)
or IBaseVocabulary.providedBy(vocabulary))
assert source is None, (
"You cannot specify both source and vocabulary.")
elif source is not None:
vocabulary = source
assert not (values is None and vocabulary is None), (
"You must specify either values or vocabulary.")
assert values is None or vocabulary is None, (
"You cannot specify both values and vocabulary.")
self.vocabulary = None
self.vocabularyName = None
if values is not None:
self.vocabulary = SimpleVocabulary.fromValues(values)
elif isinstance(vocabulary, (unicode, str)):
self.vocabularyName = vocabulary
else:
assert (ISource.providedBy(vocabulary) or
IContextSourceBinder.providedBy(vocabulary))
self.vocabulary = vocabulary
# Before a default value is checked, it is validated. However, a
# named vocabulary is usually not complete when these fields are
# initialized. Therefore signal the validation method to ignore
# default value checks during initialization of a Choice tied to a
# registered vocabulary.
self._init_field = (bool(self.vocabularyName) or
IContextSourceBinder.providedBy(self.vocabulary))
super(Choice, self).__init__(**kw)
self._init_field = False
示例2: choicePersistentFieldAdapter
# 需要導入模塊: from zope.schema.interfaces import IContextSourceBinder [as 別名]
# 或者: from zope.schema.interfaces.IContextSourceBinder import providedBy [as 別名]
def choicePersistentFieldAdapter(context):
"""Special handling for Choice fields.
"""
instance = persistentFieldAdapter(context)
if instance is None:
return None
if ISource.providedBy(context.vocabulary) or \
IContextSourceBinder.providedBy(context.vocabulary):
safe = False
# Attempt to reverse engineer a 'values' argument
if isinstance(context.vocabulary, SimpleVocabulary):
values = []
safe = True
for term in context.vocabulary:
if term.token == str(term.value) and is_primitive(term.value):
values.append(term.value)
else:
safe = False
break
if safe:
instance._values = values
if not safe:
__traceback_info__ = "Persistent fields only support named vocabularies " + \
"or vocabularies based on simple value sets."
return None
return instance
示例3: _get_vocabulary
# 需要導入模塊: from zope.schema.interfaces import IContextSourceBinder [as 別名]
# 或者: from zope.schema.interfaces.IContextSourceBinder import providedBy [as 別名]
def _get_vocabulary(self, field):
"""Try to determine the vocabulary for a field, if possible.
Loosely based on z3c.form.widget.Widget.update().
"""
# XXX: Refactor this method
field_vocab = None
vf = None
if isinstance(field, Choice):
# First check whether an override is defined in the configuration
# for vocabularies we can't (or don't want to) serialize
override = self._get_vocab_override(field)
if override is not None:
return override
elif field.vocabulary:
if IVocabularyFactory.providedBy(field.vocabulary):
vocabulary = field.vocabulary(None)
elif IContextSourceBinder.providedBy(field.vocabulary):
vocabulary = field.vocabulary(None)
else:
vocabulary = field.vocabulary
elif field.vocabularyName:
try:
vf = getUtility(
IVocabularyFactory, name=field.vocabularyName)
except ComponentLookupError:
pass
vocabulary = vf(None)
# Determine whether term order is significant or not
order_significant = True
wrapped_vocab = vocabulary
if IElephantVocabulary.providedBy(vocabulary):
# VDEX vocaby might be potentially wrapped by Elephant vocabs.
# In that case, we need to inspect the wrapped vocab to
# determine whether order is significant or not
wrapped_vocab = queryUtility(
IVocabularyFactory, name=field.vocabulary.vocab)
if isinstance(wrapped_vocab, VdexVocabulary):
order_significant = wrapped_vocab.vdex.isOrderSignificant()
# Build list of terms
if vocabulary is not None:
terms = [t.value for t in vocabulary._terms]
if not order_significant:
# For VDEX vocabularies with orderSignificant="false" we
# explicitly sort terms to get a stable sort order
terms = sorted(terms)
field_vocab = terms
return field_vocab
示例4: __init__
# 需要導入模塊: from zope.schema.interfaces import IContextSourceBinder [as 別名]
# 或者: from zope.schema.interfaces.IContextSourceBinder import providedBy [as 別名]
def __init__(self, values=None, vocabulary=None, source=None, **kw):
"""Initialize object."""
if vocabulary is not None:
if (not isinstance(vocabulary, string_types)
and not IBaseVocabulary.providedBy(vocabulary)):
raise ValueError('vocabulary must be a string or implement '
'IBaseVocabulary')
if source is not None:
raise ValueError(
"You cannot specify both source and vocabulary.")
elif source is not None:
vocabulary = source
if (values is None and vocabulary is None):
raise ValueError(
"You must specify either values or vocabulary."
)
if values is not None and vocabulary is not None:
raise ValueError(
"You cannot specify both values and vocabulary."
)
self.vocabulary = None
self.vocabularyName = None
if values is not None:
self.vocabulary = SimpleVocabulary.fromValues(values)
elif isinstance(vocabulary, string_types):
self.vocabularyName = vocabulary
else:
if (not ISource.providedBy(vocabulary)
and not IContextSourceBinder.providedBy(vocabulary)):
raise ValueError('Invalid vocabulary')
self.vocabulary = vocabulary
# Before a default value is checked, it is validated. However, a
# named vocabulary is usually not complete when these fields are
# initialized. Therefore signal the validation method to ignore
# default value checks during initialization of a Choice tied to a
# registered vocabulary.
self._init_field = (bool(self.vocabularyName) or
IContextSourceBinder.providedBy(self.vocabulary))
super(Choice, self).__init__(**kw)
self._init_field = False
示例5: bind
# 需要導入模塊: from zope.schema.interfaces import IContextSourceBinder [as 別名]
# 或者: from zope.schema.interfaces.IContextSourceBinder import providedBy [as 別名]
def bind(self, object):
clone = zope.schema.Field.bind(self, object)
# get registered vocabulary if needed:
if IContextSourceBinder.providedBy(self.vocabulary):
clone._vocabulary = self.vocabulary(object)
assert zope.schema.interfaces.ISource.providedBy(clone.vocabulary)
elif clone.vocabulary is None and self.vocabularyName is not None:
vr = zope.schema.vocabulary.getVocabularyRegistry()
clone._vocabulary = vr.get(object, self.vocabularyName)
assert zope.schema.interfaces.ISource.providedBy(clone.vocabulary)
return clone
示例6: bind
# 需要導入模塊: from zope.schema.interfaces import IContextSourceBinder [as 別名]
# 或者: from zope.schema.interfaces.IContextSourceBinder import providedBy [as 別名]
def bind(self, object):
"""See zope.schema._bootstrapinterfaces.IField."""
clone = super(Choice, self).bind(object)
# get registered vocabulary if needed:
if IContextSourceBinder.providedBy(self.vocabulary):
clone.vocabulary = self.vocabulary(object)
assert ISource.providedBy(clone.vocabulary)
elif clone.vocabulary is None and self.vocabularyName is not None:
vr = getVocabularyRegistry()
clone.vocabulary = vr.get(object, self.vocabularyName)
assert ISource.providedBy(clone.vocabulary)
return clone
示例7: __init__
# 需要導入模塊: from zope.schema.interfaces import IContextSourceBinder [as 別名]
# 或者: from zope.schema.interfaces.IContextSourceBinder import providedBy [as 別名]
def __init__(self, value_type, vocabulary, **kw):
# complain if value_type is not a field
if value_type is not None and not IField.providedBy(value_type):#IGNORE:E1101
raise ValueError("'value_type' must be field instance.")
self.value_type = value_type
self.vocabulary = None
self.vocabularyName = None
if isinstance(vocabulary, (unicode, str)):
self.vocabularyName = vocabulary
else:
assert (ISource.providedBy(vocabulary) or #IGNORE:E1101
IContextSourceBinder.providedBy(vocabulary)) #IGNORE:E1101
self.vocabulary = vocabulary
self._init_field = bool(self.vocabularyName)
Field.__init__(self, **kw) # initializing List or Choice would mess up
# the vocabulary
self._init_field = False
示例8: _resolve_vocabulary
# 需要導入模塊: from zope.schema.interfaces import IContextSourceBinder [as 別名]
# 或者: from zope.schema.interfaces.IContextSourceBinder import providedBy [as 別名]
def _resolve_vocabulary(self, value):
# Find the vocabulary we should use, raising
# an exception if this isn't possible, and returning
# an ISource otherwise.
vocabulary = self.vocabulary
if IContextSourceBinder.providedBy(vocabulary) and self.context is not None:
vocabulary = vocabulary(self.context)
elif vocabulary is None and self.vocabularyName is not None:
vr = getVocabularyRegistry()
try:
vocabulary = vr.get(self.context, self.vocabularyName)
except LookupError:
raise MissingVocabularyError(
"Can't validate value without vocabulary named %r" % (self.vocabularyName,)
).with_field_and_value(self, value)
if not ISource.providedBy(vocabulary):
raise InvalidVocabularyError(vocabulary).with_field_and_value(self, value)
return vocabulary
示例9: __call__
# 需要導入模塊: from zope.schema.interfaces import IContextSourceBinder [as 別名]
# 或者: from zope.schema.interfaces.IContextSourceBinder import providedBy [as 別名]
def __call__(self):
self.request.response.setHeader(
'Content-Type', 'application/json; charset=utf-8')
field = self.request['field']
slavename = self.request['name']
slaveid = self.request['slaveID']
masterid = self.request['masterID']
value = self.request['value']
for slave in self.widget.getSlaves():
# Loop until we find the slave we want
if slave['name'] != slavename:
continue
action = slave.get('action')
if action not in ['vocabulary', 'value', 'attr']:
continue
# --- VALUE --------------------------------------------------------
if action == 'value':
value = self.getVocabulary(slave, value, '')
return json.dumps(translate(value, self.request))
# --- ATTR- --------------------------------------------------------
if action == 'attr':
result = self.getVocabulary(slave, value, None)
if isinstance(result, dict) and 'attr' in result and 'value' in result:
return json.dumps(result)
else:
raise ValueError('Bad attr dictionary for %s.' % slavename)
# --- VOCABULARY ---------------------------------------------------
vocabulary = self.getVocabulary(slave, value)
if isinstance(vocabulary, (tuple, list)):
vocabulary = self.createVocaabulary(vocabulary)
widget = self.widget.form.widgets.get(slave['name'])
if widget is None:
raise ValueError('Can not find widget: %s' % slave['name'])
if (IContextSourceBinder.providedBy(vocabulary)
or IVocabularyTokenized.providedBy(vocabulary)):
if hasattr(widget.field, 'value_type'):
widget.field.value_type.vocabulary = vocabulary
else:
widget.field.vocabulary = vocabulary
widget.terms = None
widget.updateTerms()
widget.update()
# widget may define items as a property or as a method
items = widget.items if not callable(widget.items) else widget.items()
responseJSON = {'items': items}
# disable select box if term length = 'disable_length'
#if len(widget.terms) == slave.get('disable_length', None):
# responseJSON['disabled'] = True
return json.dumps(responseJSON)
raise ValueError('No such master-slave combo')