====================== Component registration ====================== Registration of objects as components is quite simple. Currently, any object can be registered as a utility. (In the future, registering objects as adapter factories probably will be supported, but only if those objects implement interfaces.) To see how this works, we'll create some objects and register them. We'll use the Sample class defined in zope.app.component.browser.tests to define 3 sample objects: >>> from zope.app.component.browser.tests import Sample >>> folder = getRootFolder() >>> folder['sample1'] = Sample() >>> folder['sample2'] = Sample() >>> folder['sample3'] = Sample() >>> import transaction >>> transaction.commit() All objects have a "Registration" tab that is accessable to people with the zope.ManageSite permission: >>> from zope.testbrowser.testing import Browser >>> browser = Browser() >>> browser.addHeader('Authorization', 'Basic mgr:mgrpw') >>> browser.addHeader('Accept-Language', 'test') >>> browser.open('http://localhost/sample1/@@SelectedManagementView.html') >>> browser.getLink('[[zope][Registration]]').click() When we first visit the registration tab, we see that there are no registrations: >>> print browser.contents >> browser.getControl('Register this object').click() This will being up a form that provides for selection from the interfaces the object provides and provides and entries for name to provide the object as and a comment: >>> print browser.contents ....ISample ...[[zope][Register As]]... ...[[zope][The name under which the utility will be known.]]... ...[[zope][Comment]]... The most specific interface is selected, which is usually what we want. If we just enter a comment and submit the form: >>> browser.getControl('[[zope][Comment]]').value = 'unnamed sample' >>> browser.getControl('[[zope][Register]]').click() We'll be redirected to the registrations page and the new registration will be shown: >>> print browser.contents [[zope][comment: unnamed sample]]... We can create multiple registrations by clicking on *Register this object again*: >>> browser.getControl('Register this object again').click() >>> browser.getControl('[[zope][Register As]]').value = 'one' >>> browser.getControl('[[zope][Register]]').click() >>> print browser.contents [[zope][comment: unnamed sample]]... ...[[zope][zope.app.component.browser.tests.ISample utility named 'one']]... Each entry has a checkbox for selecting it. This can be used to unregister an object. We'll unregister the utility named "one": >>> browser.getControl(name='ids:list').getControl( ... value='Rem9wZS5hcHAuY29tcG9uZW50LmJyb3dzZXIudGVzdHMuSVNhbXBsZSBvbmU' ... ).selected = True >>> browser.getControl('[[zope][unregister-button (Unregister)]]').click() >>> "utility named 'one'" not in browser.contents True If there is already an object registered, new registrations will simply override the old. We can see this by creating a registration for sample2 and then overriding it's registration by registering sample3. First, we register sample2: >>> browser.open('http://localhost/sample2/registration.html') >>> browser.getControl('Register this object').click() >>> browser.getControl('[[zope][Register As]]').value = 'two' >>> browser.getControl('[[zope][Register]]').click() We can see all of the registrations for a site by visting the site-manager's registrations page: >>> browser.open( ... 'http://localhost/++etc++site/@@SelectedManagementView.html') >>> browser.getLink('[[zope][Registrations]]').click() >>> print browser.contents [[zope][comment: unnamed sample]]... ...sample2...[[zope][zope.app.component.browser.tests.ISample utility named 'two']]... This shows all of the registrations for the site, including our sample registrations. The display includes a link to each component. Now, we'll register sample 3: >>> browser.open('http://localhost/sample3/registration.html') >>> browser.getControl('Register this object').click() >>> browser.getControl('[[zope][Register As]]').value = 'two' >>> browser.getControl('[[zope][Register]]').click() and note that now sample 3, rather than sample 2 is shown in the listing of registered components for the site: >>> browser.open('http://localhost/++etc++site/@@registrations.html') >>> print browser.contents [[zope][comment: unnamed sample]]... ...sample3...[[zope][zope.app.component.browser.tests.ISample utility named 'two']]... >>> 'sample2' not in browser.contents True And if we look at sample2's registrations, we'll see it's not registered: >>> browser.open('http://localhost/sample2/registration.html') >>> print browser.contents >> browser.open('http://localhost/++etc++site/@@registrations.html') >>> browser.getControl(name='ids:list').getControl( ... value='Rem9wZS5hcHAuY29tcG9uZW50LmJyb3dzZXIudGVzdHMuSVNhbXBsZSB0d28' ... ).selected = True >>> browser.getControl('[[zope][unregister-button (Unregister)]]').click() >>> 'sample3' not in browser.contents True >>> browser.open('http://localhost/sample3/registration.html') >>> print browser.contents >> del folder['sample1'] >>> transaction.commit() It remains registered, and can be unregistered: >>> browser.open('http://localhost/++etc++site/@@registrations.html') >>> print browser.contents [[zope][zope.app.component.browser.tests.ISample utility]]
[[zope][comment: unnamed sample]]... >>> browser.getControl(name='ids:list').getControl( ... value="Rem9wZS5hcHAuY29tcG9uZW50LmJyb3dzZXIudGVzdHMuSVNhbXBsZSA" ... ).selected = True >>> browser.getControl('[[zope][unregister-button (Unregister)]]').click() >>> 'ISample' not in browser.contents True The registration view for an object only shows the registrations in the immediately enclosing site. To see this, we register sample2: >>> browser.open('http://localhost/sample2/registration.html') >>> browser.getControl('Register this object').click() >>> browser.getControl('[[zope][Register]]').click() Now we'll create a subsite and move sample2 there: >>> browser.open('http://localhost/@@SelectedManagementView.html') >>> browser.getLink('[[zope][Folder]]').click() >>> browser.getControl(name='new_value').value = 'subsite' >>> browser.getControl('Apply').click() >>> browser.getLink('subsite').click() >>> browser.getLink('[[zope][Make a site]').click() >>> browser.open('http://localhost/@@SelectedManagementView.html') >>> browser.getControl(name='ids:list').getControl(value='sample2' ... ).selected = True >>> browser.getControl('[[zope][container-cut-button (Cut)]]').click() >>> browser.getLink('subsite').click() >>> browser.getControl('[[zope][container-paste-button (Paste)]]').click() sample2's registration page now indicates that sample2 is unregistered: >>> browser.open('http://localhost/subsite/registration.html') >>> print browser.contents >> browser.open('http://localhost/++etc++site/@@registrations.html') >>> print browser.contents