def _get_vocabulary(self, field): """Try to determine the vocabulary for a field, if possible. Loosely based on z3c.form.widget.Widget.update(). """ field_vocab = None vf = None if isinstance(field, Choice): # First check whether an override is defined in the configuration # for vocabularies we can't (or don't want to) serialize override = self._get_vocab_override(field) if override is not None: return override elif field.vocabulary: if IVocabularyFactory.providedBy(field.vocabulary): vf = field.vocabulary # we (sometimes) use IContextSourceBinder independent of # context - for fields where we don't we should define an # override elif IContextSourceBinder.providedBy(field.vocabulary): vf = field.vocabulary elif field.vocabularyName: try: vf = getUtility( IVocabularyFactory, name=field.vocabularyName) except ComponentLookupError: pass if vf is not None: # Ignore context dependent vocabularies vocabulary = vf(None) terms = [t.value for t in vocabulary._terms] field_vocab = terms return field_vocab
def getter(context, formatter, vocabulary=vocabulary): # if this is a vocabulary factory, we need to instantiate with context # # !+ but, when context is irrelevant, call-it-as-factory to get a # context-bound instance seems unnecessarily inefficient! # # !+ VDEXVocabularyMixin-based FlatVDEXVocabularyFactory and # TreeVDEXVocabulary (but this is as yet never included in a listing) # already implement getTerm() -- that is all that is needed here, and # the term returned is already localized, so we really do not need to # call-it-as-factory to bind it to context on each lookup... if not isinstance(vocabulary, VDEXVocabularyMixin): if IVocabularyFactory.providedBy(vocabulary): vocabulary = vocabulary(context) try: return vocabulary.getTerm(getattr(context, name)).title except LookupError: # !+NONE_LOOKUPERROR(mr, jul-2012) probably a vdex, # and getattr(context, name)... value = getattr(context, name) m = "LookpError: vocabulary [%s] term value [%s] " \ "for context [%s.%s]" % ( vocabulary, value, context, name) # we should only have a LookupError on a None value (and it is not # defined in the vocabulary) assert value is None, m log.warn(m) return None
def getter(context, formatter, vocabulary=vocabulary): # if this is a vocabulary factory, we need to instantiate with context # !+ but, when context is irrelevant, call-it-as-factory to get a # context-bound instance seems unnecessarily inefficient! # !+ VDEXVocabularyMixin-based FlatVDEXVocabularyFactory and # TreeVDEXVocabulary (but this is as yet never included in a listing) # already implement getTerm() -- that is all that is needed here, and # the term returned is already localized, so we really do not need to # call-it-as-factory to bind it to context on each lookup... #if not isinstance(vocabulary, VDEXVocabularyMixin): if IVocabularyFactory.providedBy(vocabulary): vocabulary = vocabulary(context) try: return vocabulary.getTerm(getattr(context, name)).title except LookupError: # !+NONE_LOOKUPERROR(mr, jul-2012) probably a vdex, # and getattr(context, name)... value = getattr(context, name) m = "******** LookupError: vocabulary [%s, length:%s] term value [%s] " \ "for context [%s.%s] -- vocabulary terms: %s" % ( vocabulary, len(vocabulary), value, context, name, [ (t.value, t.token, t.title) for t in vocabulary._terms ]) log.error(m) # we should only have a LookupError on a None value (and it is not # defined in the vocabulary), in which case we return None if value is not None: raise
def _get_vocab_term(context, field, value): """ Get vocab term dict returns: {'token': token, 'title': title} """ vocab_term = { 'token': None, 'title': None, } vocab_term['token'] = value factory = getUtility(IVocabularyFactory, field) if not factory: return vocab_term # collective.taxonomy support: if hasattr(factory, 'translate'): vocab_term['title'] = _get_taxonomy_vocab_title( context, factory, value, ) elif IVocabularyFactory.providedBy(factory): vocab_term['title'] = _get_vocab_title( context, factory, value, ) return vocab_term
def test_vocab_s_d_g_vocabulary(self): vocab_name = 'collective.behavior.sdg.SDGsVocabulary' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual(vocabulary.getTerm('13').title, u'Climate action')
def test_vocab_renewal_periods(self): vocab_name = "collective.contract_management.RenewalPeriods" factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual(vocabulary.getTerm("3").title, _(u"3 months"))
def test_vocab_reminder_types(self): vocab_name = "collective.contract_management.ReminderTypes" factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual(vocabulary.getTerm("14").title, _(u"14 days"))
def _get_vocabulary(self, field): """Try to determine the vocabulary for a field, if possible. Loosely based on z3c.form.widget.Widget.update(). """ # XXX: Refactor this method field_vocab = None vf = None if isinstance(field, Choice): # First check whether an override is defined in the configuration # for vocabularies we can't (or don't want to) serialize override = self._get_vocab_override(field) if override is not None: return override elif field.vocabulary: if IVocabularyFactory.providedBy(field.vocabulary): vocabulary = field.vocabulary(None) elif IContextSourceBinder.providedBy(field.vocabulary): vocabulary = field.vocabulary(None) else: vocabulary = field.vocabulary elif field.vocabularyName: try: vf = getUtility( IVocabularyFactory, name=field.vocabularyName) except ComponentLookupError: pass vocabulary = vf(None) # Determine whether term order is significant or not order_significant = True wrapped_vocab = vocabulary if IElephantVocabulary.providedBy(vocabulary): # VDEX vocaby might be potentially wrapped by Elephant vocabs. # In that case, we need to inspect the wrapped vocab to # determine whether order is significant or not wrapped_vocab = queryUtility( IVocabularyFactory, name=field.vocabulary.vocab) if isinstance(wrapped_vocab, VdexVocabulary): order_significant = wrapped_vocab.vdex.isOrderSignificant() # Build list of terms if vocabulary is not None: terms = [t.value for t in vocabulary._terms] if not order_significant: # For VDEX vocabularies with orderSignificant="false" we # explicitly sort terms to get a stable sort order terms = sorted(terms) field_vocab = terms return field_vocab
def test_vocab_plone_users(self): vocab_name = "Products.EasyNewsletter.PloneUsers" factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm(self.jane.getId()).title, _("Jane Doe - [email protected]") )
def test_vocabulary(self): vocab_name = "example.z3cformwidgets.AvailableThings" factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_network_operators(self): vocab_name = 'docpool.doksys.NetworkOperators' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_countries(self): vocab_name = 'collective.vocabularies.iso.Countries' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_output_templates(self): vocab_name = "Products.EasyNewsletter.OutputTemplates" factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm("output_default").title, _("Default output template"), )
def test_vocab_partners(self): vocab_name = 'foecluster.km.Partners' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_aggregation_templates(self): vocab_name = "Products.EasyNewsletter.AggregationTemplates" factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm("aggregation_generic_listing").title, _("Generic Listing"), )
def test_vocab_voc_ore_inizio(self): vocab_name = 'rg.prenotazioni.VocOreInizio' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocabulary(self): vocab_name = "example.z3cformwidgets.OtherFiles" factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('a').title, _(u'a'), )
def test_vocab_political_parties(self): vocab_name = 'sinar.plone.vocabularies.PoliticalParties' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_salutations(self): vocab_name = 'Products.EasyNewsletter.Salutations' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('ms').title, _(u'Ms'), )
def test_vocab_available_things(self): vocab_name = 'gisweb.trigeau.AvailableThings' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_class_room(self): vocab_name = 'cshm.content.ClassRoom' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_submission_method(self): vocab_name = 'ocds.contenttypes.SubmissionMethod' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_tender_status(self): vocab_name = 'ocds.contenttypes.TenderStatus' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_crop_categories(self): vocab_name = 'gaipa.backend.CropCategories' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm(self.mycrop.absolute_url_path()).title, u'My Crop', )
def test_vocab_resource_type(self): vocab_name = 'sinar.resource.ResourceType' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_interest_level(self): vocab_name = 'politikus.bods.InterestLevel' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_map_category(self): vocab_name = 'alpha.content.MapCategory' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_organizations_vocab(self): vocab_name = 'popolo.contenttypes.OrganizationsVocab' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm('sony-a7r-iii').title, _(u'Sony Aplha 7R III'), )
def test_vocab_plone_groups(self): vocab_name = 'Products.EasyNewsletter.PloneGroups' factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual( vocabulary.getTerm(self.vip.getId()).title, _(u'VIP'), )
def vocab_test(self, vocab_name): api.content.transition(obj=self.item1, transition='activate') factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual(len(vocabulary), 1) self.assertEqual( vocabulary.getTerm(self.item1.UID()).title, self.item1.Title(), )
def test_dynamic_terms(self): self.add_samples() facets = queryUtility(IFacetSettings) for facet in facets.values(): if facet.query_vocabulary: dynamic = queryUtility(IVocabularyFactory, name=facet.name) assert dynamic is not None assert IVocabularyFactory.providedBy(dynamic) vocab = dynamic(self.portal) hybrid = facet(self.portal) #may be superset including vocab for t in vocab: assert t.value in [e.value for e in hybrid] assert len(vocab) > 0 #this is true for sample content
def getChoices(self, form): source = self.source if source is None: factory = self.vocabularyFactory assert factory is not None, \ "No vocabulary source available." if (IContextSourceBinder.providedBy(factory) or IVocabularyFactory.providedBy(factory)): source = factory(form.context) elif IFormSourceBinder.providedBy(factory): source = factory(form) assert IVocabularyTokenized.providedBy(source), \ "No valid vocabulary available, %s is not valid for %s" % ( source, self) return source
def test_vocabulary(self): """Validate the vocabulary.""" vocab_name = 'pkan.dcatapde.vocabularies.AllDcatObjects' api.content.transition(obj=self.item1, transition='activate') api.content.transition(obj=self.item3, transition='activate') factory = getUtility(IVocabularyFactory, vocab_name) self.assertTrue(IVocabularyFactory.providedBy(factory)) vocabulary = factory(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual(len(vocabulary), 2) self.assertEqual( vocabulary.getTerm(self.item1.UID()).title, self.item1.Title(), ) self.assertEqual( vocabulary.getTerm(self.item3.UID()).title, self.item3.Title(), )
def test_implements(self): instance = VATRatesVocabulary() from zope.schema.interfaces import IVocabularyFactory self.assertTrue(IVocabularyFactory.providedBy(instance))
def test_component_is_registered(self): factory = queryUtility(IVocabularyFactory, name='ftw.task.users') self.assertNotEqual(factory, None) self.assertTrue(IVocabularyFactory.providedBy(factory))
def test_interfaces(self): self.assertTrue(IVocabularyFactory.providedBy(self._get_class())) self.assertTrue(IVocabulary.providedBy(self._make_one()))
def test_component_is_registered(self): factory = queryUtility(IVocabularyFactory, name="ftw.task.users") self.assertNotEqual(factory, None) self.assertTrue(IVocabularyFactory.providedBy(factory))