Example #1
0
def test_vocabulary(dummy_request):
    from guillotina.schema.vocabulary import SimpleVocabulary
    vocab = SimpleVocabulary.fromItems((
        (u"Foo", "id_foo"),
        (u"Bar", "id_bar")))
    res = json_compatible(vocab)
    assert type(res) == list
Example #2
0
def get_vocabulary(prop, params):
    # Vocabulary option
    if 'vocabulary' in prop:
        if isinstance(prop['vocabulary'], dict):
            params['vocabulary'] = SimpleVocabulary.fromItems(
                [x for x in prop['vocabulary'].items()])
        elif prop['vocabulary'].startswith('appsettings:'):
            params['source'] = AppSettingSource(prop['vocabulary'].replace(
                'appsettings:', ''))
        else:
            params['vocabulary'] = prop['vocabulary']
Example #3
0
def get_vocabulary(prop, params):
    # Vocabulary option
    if "vocabulary" in prop:
        if isinstance(prop["vocabulary"], dict):
            params["vocabulary"] = SimpleVocabulary([
                SimpleTerm(token, title=value)
                for (token, value) in prop["vocabulary"].items()
            ])
        elif prop["vocabulary"].startswith("appsettings:"):
            params["source"] = AppSettingSource(prop["vocabulary"].replace(
                "appsettings:", ""))
        else:
            params["vocabulary"] = prop["vocabulary"]
Example #4
0
    def __init__(self, values=None, vocabulary=None, source=None, **kw):
        """Initialize object."""
        if vocabulary is not None:
            if (not isinstance(vocabulary, str) 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, str):
            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
Example #5
0
    def __init__(self, values=None, vocabulary=None, source=None, **kw):
        """Initialize object."""
        if vocabulary is not None:
            if (not isinstance(vocabulary, str) 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, str):
            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
Example #6
0
def test_vocabulary():
    from guillotina.schema.vocabulary import SimpleVocabulary
    vocab = SimpleVocabulary.fromItems(
        ((u"Foo", "id_foo"), (u"Bar", "id_bar")))
    res = getAdapter(vocab, interface=IValueToJson)
    assert type(res) == list