.. -*-doctest-*- ========================================== Choice Fields with Vocabularies or Sources ========================================== The schema editor allows the user to add Choice fields while also specifying a vocabulary or source of the values which can be selected. Log in as a user who can edit content type schemata and open the schema editor. >>> user = app.acl_users.userFolderAddUser( ... 'root', 'secret', ['Manager'], []) >>> from Products.Five import testbrowser >>> browser = testbrowser.Browser() >>> browser.handleErrors = False >>> browser.addHeader('Authorization', 'Basic root:secret') Open the schema editor in the browser. >>> portal_url = 'http://nohost' >>> browser.open(portal_url + '/@@schemaeditor') >>> 'Edit @@schemaeditor' in browser.contents True Add a Choice field. >>> browser.getLink('Add new field').click() >>> browser.getControl('Title').value = 'Country' >>> browser.getControl('Short Name').value = 'country' >>> browser.getControl('Field type').getControl( ... value='label_choice_field').selected = True >>> browser.getControl('Add').click() [event: ObjectAddedEvent on Choice] [event: FieldAddedEvent on DummySchemaContext] >>> browser.url 'http://nohost/@@schemaeditor/@@add-field' Open the new fields edit form. >>> browser.open(portal_url + '/@@schemaeditor') >>> browser.getLink(url='country').click() The edit form for choice fields includes a widget for specifying the vocabulary. >>> ctl = browser.getControl('Possible values') >>> ctl If duplicate values are entered an error is raised. >>> ctl.value = '\n'.join( ... ['Alaska', 'Russia', 'United States', 'United States', ... 'Other']) >>> browser.getControl('Save').click() >>> print browser.contents <...
The 'United States' vocabulary value conflicts with 'United States'.
... Enter valid values and save the field settings. >>> browser.getControl('Possible values').value = '\n'.join( ... ['Alaska', 'Russia', 'United States', "C\xc3\xb4te d'Ivoire", 'Other']) >>> browser.getControl('Save').click() [event: ObjectModifiedEvent on Choice] [event: SchemaModifiedEvent on DummySchemaContext] When the edit form for the content type is loaded, the vocabulary values specified can be selected. >>> browser.open(portal_url + '/@@contexteditor') >>> ctl = browser.getControl('Country') >>> item = ctl.getControl('Russia') >>> item >>> item.selected = True >>> ctl.value ['Russia'] >>> item = ctl.getControl('Alaska') >>> item.selected False >>> item.selected = True >>> ctl.getControl('Russia').selected False >>> ctl.value ['Alaska'] We can select a vocabulary factory instead of values. >>> browser.open(portal_url + '/@@schemaeditor') >>> browser.getLink(url='country').click() >>> browser.getControl('Possible values').value = "" >>> voc_name_ctl = browser.getControl('Vocabulary name') >>> voc_name_ctl.getControl('plone.schemaeditor.test.Countries').selected = True >>> browser.getControl('Save').click() [event: ObjectModifiedEvent on Choice] [event: SchemaModifiedEvent on DummySchemaContext] >>> browser.open(portal_url + '/@@contexteditor') >>> '