コード例 #1
0
 def serialize(self):
     result = super(DefaultChoiceSchemaFieldSerializer, self).serialize()
     if self.field.vocabularyName is not None:
         container = get_current_container()
         result["vocabulary"] = {
             "@id": f"{get_object_url(container, self.request)}/@vocabularies/{self.field.vocabularyName}"
         }
         vocabulary_registry = getVocabularyRegistry()
         try:
             vocab = vocabulary_registry.get(None, self.field.vocabularyName)
             result["enum"] = vocab.keys()
         except AttributeError:
             pass
         result["type"] = "string"
     else:
         if isinstance(self.field.vocabulary, SimpleVocabulary):
             result["choices"] = [
                 (x.token, x.value) for x in self.field.vocabulary._terms
             ]
             result["enum"] = self.field.vocabulary.by_token.keys()
             result["enumNames"] = self.field.vocabulary.by_value.keys()
         elif ISource.providedBy(self.field.vocabulary):
             result["choices"] = self.field.vocabulary.values
             result["enum"] = self.field.vocabulary.keys()
             result["enumNames"] = [v for k, v in self.field.vocabulary.values]
     return result
コード例 #2
0
ファイル: _field.py プロジェクト: nazrulworld/guillotina
    def bind(self, object):
        """See guillotina.schema._bootstrapinterfaces.IField."""
        clone = super(Choice, self).bind(object)
        # get registered vocabulary if needed:
        if IContextSourceBinder.providedBy(self.vocabulary):
            clone.vocabulary = self.vocabulary(object)
        elif clone.vocabulary is None and self.vocabularyName is not None:
            vr = getVocabularyRegistry()
            clone.vocabulary = vr.get(object, self.vocabularyName)

        if not ISource.providedBy(clone.vocabulary):
            raise ValueError("Invalid clone vocabulary")

        return clone
コード例 #3
0
ファイル: _field.py プロジェクト: nazrulworld/guillotina
    def bind(self, object):
        """See guillotina.schema._bootstrapinterfaces.IField."""
        clone = super(Choice, self).bind(object)
        # get registered vocabulary if needed:
        if IContextSourceBinder.providedBy(self.vocabulary):
            clone.vocabulary = self.vocabulary(object)
        elif clone.vocabulary is None and self.vocabularyName is not None:
            vr = getVocabularyRegistry()
            clone.vocabulary = vr.get(object, self.vocabularyName)

        if not ISource.providedBy(clone.vocabulary):
            raise ValueError('Invalid clone vocabulary')

        return clone
コード例 #4
0
ファイル: _field.py プロジェクト: nazrulworld/guillotina
    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
コード例 #5
0
ファイル: _field.py プロジェクト: worasit/guillotina
    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