def __init__(self, values=None, vocabulary=None, source=None, **kw): """Initialize object.""" if vocabulary is not None: assert (isinstance(vocabulary, string_types) 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, string_types): 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
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
def _constructField(self, attributes): if 'vocabulary' in attributes: try: component = resolve(attributes['vocabulary']) if IBaseVocabulary.providedBy(component): attributes['vocabulary'] = component elif (ISource.providedBy(component) or IContextSourceBinder.providedBy(component)): attributes['source'] = component del attributes['vocabulary'] except ImportError: # regular named vocabulary, leave as is pass return super(QueryChoiceHandler, self)._constructField(attributes)
def test_degree(self): e = self.e assert IBaseVocabulary.providedBy(self.e) for a in self.e: ITokenizedTerm.providedBy(a) break else: assert False assert 0 in e assert -1 not in e assert e.getTermByToken("Specialist").value == 6 assert e.getTermByToken("Specialist").value == e.enum.Specialist assert e.getTerm(e.enum.Specialist).value == e.enum.Specialist assert e[e.enum.Specialist].value == e.enum.Specialist assert e[6].value == e.enum.Specialist
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