def test_vocabulary_wrong_cts(self): vocab = DcatFieldVocabulary([constants.CT_HARVESTER]) vocabulary = vocab(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual(len(vocabulary), 0)
def test_vocabulary_no_cts(self): vocab = DcatFieldVocabulary([]) vocabulary = vocab(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual(len(vocabulary), 0)
def __call__(self): # Binding is necessary for named vocabularies if IField.providedBy(self.field): self.field = self.field.bind(self.context) value = self.get_value() # XXX with elephanvocabulary, "real" vocab is stored on vocabulary.vocab vocab = getattr(self.field.vocabulary, "vocab", self.field.vocabulary) if value is not None and IVocabularyTokenized.providedBy(vocab): try: term = self.field.vocabulary.getTerm(value) # If not found, get it from the z3c.form that maybe uses a MissingTerms adapter... except LookupError: try: view = self.context.restrictedTraverse("@@view") view.update() # widget_name is the field name prefixed with behavior if any widget_name = [field for field in get_schema_fields(self.context, prefix=True) if field[1].__name__ == self.field.__name__][0][0] widget = view.widgets[widget_name] term = widget.terms.getTerm(value) except Exception: # at worse use value as title term = SimpleTerm(value, title=value) finally: value = {"token": term.token, "title": term.title} return json_compatible(value)
def write(self, field, name, type, elementName='field'): element = super(ChoiceHandler, self).write(field, name, type, elementName) # write vocabulary or values list # Named vocabulary if field.vocabularyName is not None and field.vocabulary is None: attributeField = self.fieldAttributes['vocabulary'] child = valueToElement(attributeField, field.vocabularyName, name='vocabulary', force=True) element.append(child) # Listed vocabulary - attempt to convert to a simple list of values elif field.vocabularyName is None and IVocabularyTokenized.providedBy(field.vocabulary): value = [] for term in field.vocabulary: if (not isinstance(term.value, (str, unicode), ) or term.token != term.value.encode('unicode_escape')): raise NotImplementedError(u"Cannot export a vocabulary that is not " "based on a simple list of values") value.append(term.value) attributeField = self.fieldAttributes['values'] child = valueToElement(attributeField, value, name='values', force=True) element.append(child) # Anything else is not allowed - we can't export ISource/IVocabulary or # IContextSourceBinder objects. else: raise NotImplementedError(u"Choice fields with vocabularies not based on " "a simple list of values or a named vocabulary " "cannot be exported") return element
def write(self, field, name, type, elementName='field'): element = super(ChoiceHandler, self).write(field, name, type, elementName) # write vocabulary or values list # Named vocabulary if field.vocabularyName is not None and field.vocabulary is None: attributeField = self.fieldAttributes['vocabulary'] child = valueToElement( attributeField, field.vocabularyName, name='vocabulary', force=True ) element.append(child) # Listed vocabulary - attempt to convert to a simple list of values elif field.vocabularyName is None \ and IVocabularyTokenized.providedBy(field.vocabulary): value = [] for term in field.vocabulary: if (not isinstance(term.value, (str, text_type), ) or term.token.encode('utf-8') != # token is 'str' term.value.encode('unicode_escape')): raise NotImplementedError( u"Cannot export a vocabulary that is not " u"based on a simple list of values" ) if term.title and term.title != term.value: value.append((term.value, term.title)) else: value.append(term.value) attributeField = self.fieldAttributes['values'] if any(map(lambda v: isinstance(v, tuple), value)): _pair = lambda v: v if len(v) == 2 else (v[0],) * 2 value = OrderedDict(map(_pair, value)) attributeField = OrderedDictField( key_type=zope.schema.TextLine(), value_type=zope.schema.TextLine(), ) child = valueToElement( attributeField, value, name='values', force=True ) element.append(child) # Anything else is not allowed - we can't export ISource/IVocabulary or # IContextSourceBinder objects. else: raise NotImplementedError( u"Choice fields with vocabularies not based on " u"a simple list of values or a named vocabulary " u"cannot be exported" ) return element
def __call__(self): # Binding is necessary for named vocabularies if IField.providedBy(self.field): self.field = self.field.bind(self.context) value = self.get_value() value_type = self.field.value_type if ( value is not None and IChoice.providedBy(value_type) and IVocabularyTokenized.providedBy(value_type.vocabulary) ): values = [] for v in value: try: term = value_type.vocabulary.getTerm(v) values.append({"token": term.token, "title": term.title}) except LookupError: log.warning( "Term lookup error: %r %s (%s:%s)" % ( v, self.field.title, self.context.portal_type, self.context.absolute_url(1), ) ) value = values return json_compatible(value)
def test_vocabulary_unexistend_cts(self): vocab = DcatFieldVocabulary(['hello_world']) vocabulary = vocab(self.portal) self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual(len(vocabulary), 0)
def write(self, field, name, type, elementName='field'): element = super(ChoiceHandler, self).write(field, name, type, elementName) # write vocabulary or values list # Named vocabulary if field.vocabularyName is not None and field.vocabulary is None: attributeField = self.fieldAttributes['vocabulary'] child = valueToElement( attributeField, field.vocabularyName, name='vocabulary', force=True ) element.append(child) # Listed vocabulary - attempt to convert to a simple list of values elif field.vocabularyName is None \ and IVocabularyTokenized.providedBy(field.vocabulary): value = [] for term in field.vocabulary: if (not isinstance(term.value, (str, unicode), ) or term.token != term.value.encode('unicode_escape')): raise NotImplementedError( u"Cannot export a vocabulary that is not " u"based on a simple list of values" ) if term.title and term.title != term.value: value.append((term.value, term.title)) else: value.append(term.value) attributeField = self.fieldAttributes['values'] if any(map(lambda v: isinstance(v, tuple), value)): _pair = lambda v: v if len(v) == 2 else (v[0],) * 2 value = OrderedDict(map(_pair, value)) attributeField = OrderedDictField( key_type=zope.schema.TextLine(), value_type=zope.schema.TextLine(), ) child = valueToElement( attributeField, value, name='values', force=True ) element.append(child) # Anything else is not allowed - we can't export ISource/IVocabulary or # IContextSourceBinder objects. else: raise NotImplementedError( u"Choice fields with vocabularies not based on " u"a simple list of values or a named vocabulary " u"cannot be exported" ) return element
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_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_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 setter(self, source): # Verify if this is a source or a vocabulary if IVocabularyTokenized.providedBy(source): self._source = source else: # Be sure to reset the source self._source = None self._vocabularyFactory = source
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.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_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_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_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_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_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_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_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_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_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_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_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_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_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_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 __call__(self, value): if isinstance(value, dict) and "token" in value: value = value["token"] if IVocabularyTokenized.providedBy(self.field.vocabulary): try: value = self.field.vocabulary.getTermByToken(value).value except LookupError: pass self.field.validate(value) return value
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 test_uvf(self): ''' Test the UniqeValueFactory ''' uvf = UniqueValuesFactory('_does_not_matter_here_') # empty index mock_context = MockContext( MockCatalog( values=[] ) ) vocabulary = uvf(mock_context) assert len(vocabulary) == 0 assert IVocabularyTokenized.providedBy(vocabulary) # index with a value mock_context = MockContext( MockCatalog( values = ['value'] ) ) vocabulary = uvf(mock_context) assert IVocabularyTokenized.providedBy(vocabulary) assert len(vocabulary) == 1 assert 'value' in vocabulary
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_returns_vocabulary_with_terms(self): vocabulary_registry = getVocabularyRegistry() vocabulary = vocabulary_registry.get(None, u"Available Listing Views") self.assertTrue(IVocabularyTokenized.providedBy(vocabulary)) self.assertEqual(len(vocabulary), 2)
def __call__(self): self.request.response.setHeader( 'Content-Type', 'application/json; charset=utf-8') field = self.request['field'] slavename = self.request['name'] slaveid = self.request['slaveID'] masterid = self.request['masterID'] value = self.request['value'] for slave in self.widget.getSlaves(): # Loop until we find the slave we want if slave['name'] != slavename: continue action = slave.get('action') if action not in ['vocabulary', 'value', 'attr']: continue # --- VALUE -------------------------------------------------------- if action == 'value': value = self.getVocabulary(slave, value, '') return json.dumps(translate(value, self.request)) # --- ATTR- -------------------------------------------------------- if action == 'attr': result = self.getVocabulary(slave, value, None) if isinstance(result, dict) and 'attr' in result and 'value' in result: return json.dumps(result) else: raise ValueError('Bad attr dictionary for %s.' % slavename) # --- VOCABULARY --------------------------------------------------- vocabulary = self.getVocabulary(slave, value) if isinstance(vocabulary, (tuple, list)): vocabulary = self.createVocaabulary(vocabulary) widget = self.widget.form.widgets.get(slave['name']) if widget is None: raise ValueError('Can not find widget: %s' % slave['name']) if (IContextSourceBinder.providedBy(vocabulary) or IVocabularyTokenized.providedBy(vocabulary)): if hasattr(widget.field, 'value_type'): widget.field.value_type.vocabulary = vocabulary else: widget.field.vocabulary = vocabulary widget.terms = None widget.updateTerms() widget.update() # widget may define items as a property or as a method items = widget.items if not callable(widget.items) else widget.items() responseJSON = {'items': items} # disable select box if term length = 'disable_length' #if len(widget.terms) == slave.get('disable_length', None): # responseJSON['disabled'] = True return json.dumps(responseJSON) raise ValueError('No such master-slave combo')
def test_Interface(self): self.failUnless(IVocabulary.providedBy(self.vocab)) self.failUnless(IVocabularyTokenized.providedBy(self.vocab))
def assertVocabulary(self, voc, values): self.assertTrue(IVocabularyTokenized.providedBy(voc)) self.assertEqual( [(term.value, term.token, term.title) for term in voc], values )