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 dict2dynatree(context, source, selected, only_leaves, show_key=False): """ Recursively parse the dictionary as we get it from the IVocabulary, and transform it to a a dictionary as needed for dynatree. source: dictionary as provided by getVocabularyDict from Products.ATVocabularyManager or a zope.schema.interfaces.ITreeVocabulary selected: List of keys that should be preselected only_leaves: Whether only leaves should be selectable or also tree nodes """ if not source: return [] if not ITreeVocabulary.providedBy(source) and not isinstance(source, dict): raise ValueError("Source must either be dict or treevocabulary") retval = [] for key in source: description = None if ITokenizedTerm.providedBy(key): subtree = source[key] title = key.title or key.value if hasattr(key, 'description'): description = key.description key = key.token else: # dict title, subtree = source[key] title = _translate(context, title) description = _translate(context, description) children = dict2dynatree( context, subtree, selected, only_leaves, show_key ) if show_key: title = "(%s) %s" % (key, title) record = {} record['title'] = title if description is not None: record['tooltip'] = description record['key'] = key record['children'] = children record['select'] = key in selected record['isFolder'] = bool(children) record['hideCheckbox'] = bool(children) and only_leaves record['expand'] = key in selected or \ isSomethingSelectedInChildren(children, selected) retval.append(record) return retval
def testRDBVocabulary(self): vocab = UnitRDBVocabulary() unit = Unit.load(unitId=1200) self.assertTrue(rdb.IObjectVocabulary.providedBy(vocab)) self.assertEqual(2, len(vocab)) self.assertEqual(2, len([o for o in vocab])) self.assertTrue(unit in vocab) self.assertTrue(1200 in vocab) self.assertTrue('1200' in vocab) self.assertFalse(200 in vocab) term = ITokenizedTerm(unit) self.assertTrue(term in vocab) self.assertEqual(unit, vocab[1200]) self.assertEqual(unit, vocab['1200']) self.assertEqual(unit, vocab[{'unitId': 1200}]) try: vocab[200] except PersistenceError: pass except Exception, e: self.fail('`%s` raised instead of `PersistenceError`' % e.__name__) else: self.fail('Object for token `200` should not be found') term = vocab.getTerm(unit) self.assertTrue(ITokenizedTerm.providedBy(term)) self.assertEqual('1200', term.token) self.assertEqual(unit, term.value) self.assertRaises(PersistenceError, vocab.getTerm, Unit(unitId=200)) term = vocab.getTermByToken(1200) self.assertTrue(ITokenizedTerm.providedBy(term)) self.assertEqual('1200', term.token) self.assertEqual(unit, term.value) self.assertRaises(PersistenceError, vocab.getTermByToken, '200') self.assertEqual(unit, vocab.get(1200)) self.assertEqual(unit, vocab.get('1200')) self.assertEqual(None, vocab.get(200)) f = frozenset tokens = vocab.keys() self.assertEqual(f(('800', '1200')), f(tokens)) values = vocab.values() self.assertEqual(2, len(values)) self.assertTrue(unit in values) self.assertEqual(f(zip(tokens, values)), f(vocab.items()))
def test_fromValues(self): from zope.interface import Interface from zope.schema.interfaces import ITokenizedTerm class IStupid(Interface): pass VALUES = [1, 4, 2, 9] vocabulary = self._getTargetClass().fromValues(VALUES) self.assertEqual(len(vocabulary), len(VALUES)) for value, term in zip(VALUES, vocabulary): self.assertTrue(ITokenizedTerm.providedBy(term)) self.assertEqual(term.value, value) for value in VALUES: self.assertTrue(value in vocabulary)
def test_fromItems(self): from zope.interface import Interface from zope.schema.interfaces import ITokenizedTerm class IStupid(Interface): pass ITEMS = [('one', 1), ('two', 2), ('three', 3), ('fore!', 4)] vocabulary = self._getTargetClass().fromItems(ITEMS) self.assertEqual(len(vocabulary), len(ITEMS)) for item, term in zip(ITEMS, vocabulary): self.assertTrue(ITokenizedTerm.providedBy(term)) self.assertEqual(term.token, item[0]) self.assertEqual(term.value, item[1]) for item in ITEMS: self.assertTrue(item[1] in vocabulary)
def dict2dynatree( input_dict, selected, only_leaves, show_key=False, ): ''' Recursively parse the dictionary as we get it from the IVocabulary, and transform it to a a dictionary as needed for dynatree. input_dict: dictionary as provided by getVocabularyDict from Products.ATVocabularyManager or a zope.schema.TreeVocabulary selected: List of keys that should be preselected only_leaves: Whether only leaves should be selectable or also tree nodes ''' if not input_dict: return [] retval = [] for key in input_dict.keys(): if ITokenizedTerm.providedBy(key): title = key.title or key.value children = dict2dynatree(input_dict[key], selected, only_leaves, show_key) key = key.token else: (title, children) = input_dict[key] children = dict2dynatree(children, selected, only_leaves, show_key) new_item = {} # we have to have boolItems if show_key: title = '(%s) %s' % (key, title) new_item['title'] = title new_item['key'] = key new_item['children'] = children new_item['select'] = key in selected new_item['isFolder'] = bool(children) new_item['hideCheckbox'] = bool(children) and only_leaves new_item['expand'] = key in selected \ or isSomethingSelectedInChildren(children, selected) retval.append(new_item) return retval
def dict2dynatree(input_dict, selected, only_leaves, show_key=False): """ Recursively parse the dictionary as we get it from the IVocabulary, and transform it to a a dictionary as needed for dynatree. input_dict: dictionary as provided by getVocabularyDict from Products.ATVocabularyManager or a zope.schema.TreeVocabulary selected: List of keys that should be preselected only_leaves: Whether only leaves should be selectable or also tree nodes """ if not input_dict: return [] retval = [] for key in input_dict.keys(): if ITokenizedTerm.providedBy(key): title = key.title or key.value children = dict2dynatree(input_dict[key], selected, only_leaves, show_key) key = key.token else: title, children = input_dict[key] children = dict2dynatree(children, selected, only_leaves, show_key) new_item = {} # we have to have boolItems if show_key: title = "(%s) %s" % (key, title) new_item['title'] = title new_item['key'] = key new_item['children'] = children new_item['select'] = key in selected new_item['isFolder'] = bool(children) new_item['hideCheckbox'] = bool(children) and only_leaves new_item['expand'] = (key in selected or isSomethingSelectedInChildren( children, selected)) retval.append(new_item) return retval