def __init__(self, *items): """Initialize the vocabulary given a list of terms. The vocabulary keeps a reference to the list of terms passed in; it should never be modified while the vocabulary is used. Also it is possible to initialize vocabulary with sequence of items, in this case constructor automatically creates `Term` objects. """ terms = [] for rec in items: if ITerm.providedBy(rec): terms.append(rec) continue if isinstance(rec, string_types): rec = (rec, ) if not hasattr(rec, '__iter__'): rec = (rec, ) if len(rec) == 2: terms.append(self.create_term(rec[0], rec[1], rec[1])) else: terms.append(self.create_term(*rec)) self.by_value = {} self.by_token = {} self._terms = terms for term in self._terms: if term.value in self.by_value: raise ValueError('term values must be unique: %s' % repr(term.value)) if term.token in self.by_token: raise ValueError('term tokens must be unique: %s' % repr(term.token)) self.by_value[term.value] = term self.by_token[term.token] = term
def __init__(self, *items): """Initialize the vocabulary given a list of terms. The vocabulary keeps a reference to the list of terms passed in; it should never be modified while the vocabulary is used. Also it is possible to initialize vocabulary with sequence of items, in this case constructor automatically creates `Term` objects. """ terms = [] for rec in items: if ITerm.providedBy(rec): terms.append(rec) continue if isinstance(rec, string_types): rec = (rec,) if not hasattr(rec, '__iter__'): rec = (rec,) if len(rec) == 2: terms.append(self.create_term(rec[0], rec[1], rec[1])) else: terms.append(self.create_term(*rec)) self.by_value = {} self.by_token = {} self._terms = terms for term in self._terms: if term.value in self.by_value: raise ValueError( 'term values must be unique: %s' % repr(term.value)) if term.token in self.by_token: raise ValueError( 'term tokens must be unique: %s' % repr(term.token)) self.by_value[term.value] = term self.by_token[term.token] = term
def update_items(self): self.items = [] for count, term in enumerate(self.vocabulary): checked = self.is_checked(term) id = '%s-%i' % (self.id, count) label = term.token desc = None if ITerm.providedBy(term): label = term.title if term.title is not None else term.token desc = term.description self.items.append( {'id': id, 'name': self.name, 'value': term.token, 'label': label, 'description': desc, 'checked': checked})