def test_setVocabularyRegistry(self): from guillotina.schema.vocabulary import setVocabularyRegistry from guillotina.schema.vocabulary import getVocabularyRegistry r = _makeDummyRegistry() setVocabularyRegistry(r) self.assertTrue(getVocabularyRegistry() is r)
async def get_block_schema(context, request): key = request.matchdict["key"] vocabulary_registry = getVocabularyRegistry() try: vocab = vocabulary_registry.get(context, key) except VocabularyRegistryError: return HTTPNotFound() title_filter = request.query.get("title") if title_filter: title_filter = title_filter.lower() token_filter = request.query.get("token") if token_filter: token_filter = token_filter.lower() result = {} result["@id"] = join(IAbsoluteURL(context)(), "@vocabularies", key) result["items"] = [] for term in vocab.keys(): if token_filter and token_filter not in str(term).lower(): continue new_title = vocab.getTerm(term) if title_filter and title_filter not in str(new_title).lower(): continue result["items"].append({"title": new_title, "token": term}) result["items_total"] = len(result["items"]) return result
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
def setUp(self): from guillotina.schema.vocabulary import _clear from guillotina.schema.vocabulary import getVocabularyRegistry from guillotina.schema.tests.states import StateVocabulary _clear() vr = getVocabularyRegistry() vr.register("states", StateVocabulary)
def load_vocabulary(_context, vocabulary): conf = vocabulary['config'] klass = vocabulary['klass'] from guillotina.schema.vocabulary import getVocabularyRegistry vocabulary_registry = getVocabularyRegistry() from guillotina.schema.interfaces import ISource classImplements(klass, ISource) vocabulary_registry.register(conf['name'], klass)
def load_vocabulary(_context, vocabulary): conf = vocabulary['config'] klass = vocabulary['klass'] from guillotina.schema.vocabulary import getVocabularyRegistry vocabulary_registry = getVocabularyRegistry() from guillotina.schema.interfaces import ISource classImplements(klass, ISource) vocabulary_registry.register(conf['name'], klass)
async def get_vocabularies(context, request): result = [] vocabulary_registry = getVocabularyRegistry() for key, item in vocabulary_registry._map.items(): result.append({ "@id": join(IAbsoluteURL(context)(), "@vocabularies", key), "title": key }) return result
def _validate(self, value): # Pass all validations during initialization if self._init_field: return super(Choice, self)._validate(value) vocabulary = self.vocabulary if vocabulary is None: vr = getVocabularyRegistry() try: vocabulary = vr.get(None, self.vocabularyName) except VocabularyRegistryError: raise ValueError("Can't validate value without vocabulary") if value not in vocabulary: raise ConstraintNotSatisfied(value, self.__name__)
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
async def get_block_schema(context, request): key = request.matchdict['key'] vocabulary_registry = getVocabularyRegistry() try: vocab = vocabulary_registry.get(context, key) except VocabularyRegistryError: return HTTPNotFound() result = {} result['@id'] = join(IAbsoluteURL(context)(), "@vocabularies", key) result['items'] = [] for term in vocab.keys(): result['items'].append({'title': vocab.getTerm(term), 'token': term}) return result
def _validate(self, value): # Pass all validations during initialization if self._init_field: return super(Choice, self)._validate(value) vocabulary = self.vocabulary if vocabulary is None: vr = getVocabularyRegistry() try: vocabulary = vr.get(None, self.vocabularyName) except VocabularyRegistryError: raise ValueError("Can't validate value without vocabulary") if value not in vocabulary: raise ConstraintNotSatisfied(value, self.__name__)
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
async def get_tile_schema(context, request): key = request.matchdict['key'] vocabulary_registry = getVocabularyRegistry() try: vocab = vocabulary_registry.get(context, key) except VocabularyRegistryError: return HTTPNotFound() result = {} for term in vocab.keys(): result[term] = { '@id': join(IAbsoluteURL(context)(), "@vocabularies", key, term), 'title': vocab.getTerm(term), 'token': term } return result
def test_registered_vocabulary(dummy_request): vr = getVocabularyRegistry() vocabulary = vr.get(None, 'testvocab') assert vocabulary is not None assert 0 in vocabulary assert vocabulary.getTerm(0) == 'value'
def test_setVocabularyRegistry(self): from guillotina.schema.vocabulary import setVocabularyRegistry from guillotina.schema.vocabulary import getVocabularyRegistry r = _makeDummyRegistry() setVocabularyRegistry(r) self.assertTrue(getVocabularyRegistry() is r)
def test_getVocabularyRegistry(self): from guillotina.schema.interfaces import IVocabularyRegistry from guillotina.schema.vocabulary import getVocabularyRegistry r = getVocabularyRegistry() self.assertTrue(IVocabularyRegistry.providedBy(r))
def test_registered_vocabulary(dummy_request): vr = getVocabularyRegistry() vocabulary = vr.get(None, 'testvocab') assert vocabulary is not None assert 0 in vocabulary assert vocabulary.getTerm(0) is 'value'
def test_getVocabularyRegistry(self): from guillotina.schema.interfaces import IVocabularyRegistry from guillotina.schema.vocabulary import getVocabularyRegistry r = getVocabularyRegistry() self.assertTrue(IVocabularyRegistry.providedBy(r))