[Zope3-checkins] CVS: Zope3/src/zope/app/browser/services/utility - registered.py:1.1 configure.zcml:1.11 useconfiguration.py:NONE

Jim Fulton jim@zope.com
Sat, 21 Jun 2003 17:22:37 -0400


Update of /cvs-repository/Zope3/src/zope/app/browser/services/utility
In directory cvs.zope.org:/tmp/cvs-serv2745/src/zope/app/browser/services/utility

Modified Files:
	configure.zcml 
Added Files:
	registered.py 
Removed Files:
	useconfiguration.py 
Log Message:
Major refactoring to reflect change in terminology from
"configuration" to "registration" to refer to the configuration of how
objects are used (as opposed to their internal configuration).


=== Added File Zope3/src/zope/app/browser/services/utility/registered.py ===
##############################################################################
#
# Copyright (c) 2003 Zope Corporation and Contributors.
# All Rights Reserved.
# 
# This software is subject to the provisions of the Zope Public License,
# Version 2.0 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Use-Registration view for utilities.

$Id: registered.py,v 1.1 2003/06/21 21:22:06 jim Exp $
"""

from zope.app.browser.component.interfacewidget import InterfaceWidget
from zope.app.browser.services.registration import AddComponentRegistration
from zope.app.form.widget import CustomWidget
from zope.app.interfaces.container import IZopeContainer
from zope.app.interfaces.services.registration import ActiveStatus
from zope.app.interfaces.services.registration import RegisteredStatus
from zope.app.interfaces.services.registration import UnregisteredStatus
from zope.app.traversing import getPath, getParent, getName
from zope.component import getServiceManager, getView, getAdapter
from zope.interface import providedBy
from zope.proxy import removeAllProxies
from zope.publisher.browser import BrowserView


class UtilityInterfaceWidget(InterfaceWidget):
    """Custom widget to select an interface from the component's interfaces.
    """

    def __call__(self):
        field = self.context
        component = field.context
        result = ['\n<select name="%s">' % self.name]
        for interface in providedBy(component).flattened():
            result.append('  <option value="%s.%s">%s</option>' %
                          (interface.__module__, interface.__name__,
                           interface.__name__))
        result.append('</select>')
        return '\n'.join(result)
        

class AddRegistration(AddComponentRegistration):
    """View for adding a utility registration.


    We could just use AddComponentRegistration, except that we need a
    custom interface widget.

    This is a view on a local utility, configured by an <addform>
    directive.
    """

    interface_widget = CustomWidget(UtilityInterfaceWidget)


class Utilities(BrowserView):

    # self.context is the local utility service

    def update(self):
        """Possibly delete one or more utilities.

        In that case, issue a message.
        """
        selected = self.request.get("selected")
        doActivate = self.request.get("Activate")
        doDeactivate = self.request.get("Deactivate")
        doDelete = self.request.get("Delete")
        if not selected:
            if doActivate or doDeactivate or doDelete:
                return "Please select at least one checkbox"
            return None
        sm = getServiceManager(self.context)
        todo = []
        for key in selected:
            name, ifacename = key.split(":", 1)
            iface = sm.resolve(ifacename)
            todo.append((key, name, iface))
        if doActivate:
            return self._activate(todo)
        if doDeactivate:
            return self._deactivate(todo)
        if doDelete:
            return self._delete(todo)

    def _activate(self, todo):
        done = []
        for key, name, iface in todo:
            registry = self.context.queryRegistrations(name, iface)
            obj = registry.active()
            if obj is None:
                # Activate the first registered registration
                obj = registry.info()[0]['registration']
                obj.status = ActiveStatus
                done.append(obj.usageSummary())
        if done:
            return "Activated: " + ", ".join(done)
        else:
            return "All of the checked utilities were already active"

    def _deactivate(self, todo):
        done = []
        for key, name, iface in todo:
            registry = self.context.queryRegistrations(name, iface)
            obj = registry.active()
            if obj is not None:
                obj.status = RegisteredStatus
                done.append(obj.usageSummary())
        if done:
            return "Deactivated: " + ", ".join(done)
        else:
            return "None of the checked utilities were active"

    def _delete(self, todo):
        errors = []
        for key, name, iface in todo:
            registry = self.context.queryRegistrations(name, iface)
            assert registry
            obj = registry.active()
            if obj is not None:
                errors.append(obj.usageSummary())
                continue
        if errors:
            return ("Can't delete active utilit%s: %s; "
                    "use the Deactivate button to deactivate" %
                    (len(errors) != 1 and "ies" or "y", ", ".join(errors)))

        # 1) Delete the registrations
        services = {}
        done = []
        for key, name, iface in todo:
            registry = self.context.queryRegistrations(name, iface)
            assert registry
            assert registry.active() is None # Phase error
            first = True
            for info in registry.info():
                conf = info['registration']
                obj = conf.getComponent()
                if first:
                    done.append(conf.usageSummary())
                    first = False
                path = getPath(obj)
                services[path] = obj
                conf.status = UnregisteredStatus
                parent = getParent(conf)
                name = getName(conf)
                container = getAdapter(parent, IZopeContainer)
                del container[name]

        # 2) Delete the service objects
        for path, obj in services.items():
            parent = getParent(obj)
            name = getName(obj)
            container = getAdapter(parent, IZopeContainer)
            del container[name]

        return "Deleted: %s" % ", ".join(done)

    def getConfigs(self):
        L = []
        for iface, name, cr in self.context.getRegisteredMatching():
            active = obj = cr.active()
            if obj is None:
                obj = cr.info()[0]['registration'] # Pick a representative
            ifname = _interface_name(iface)
            d = {"interface": ifname,
                 "name": name,
                 "url": "",
                 "summary": obj.usageSummary(),
                 "configurl": ("@@configureutility.html?interface=%s&name=%s"
                               % (ifname, name)),
                 }
            if active is not None:
                d["url"] = str(getView(active.getComponent(),
                                       "absolute_url",
                                       self.request))
            L.append((ifname, name, d))
        L.sort()
        return [d for ifname, name, d in L]


class ConfigureUtility(BrowserView):
    def update(self):
        sm = getServiceManager(self.context)
        iface = sm.resolve(self.request['interface'])
        name = self.request['name']
        cr = self.context.queryRegistrations(name, iface)
        form = getView(cr, "ChangeRegistrations", self.request)
        form.update()
        return form


def _interface_name(iface):
    return "%s.%s" % (iface.__module__, iface.__name__)


=== Zope3/src/zope/app/browser/services/utility/configure.zcml 1.10 => 1.11 ===
--- Zope3/src/zope/app/browser/services/utility/configure.zcml:1.10	Mon Apr 28 12:57:57 2003
+++ Zope3/src/zope/app/browser/services/utility/configure.zcml	Sat Jun 21 17:22:05 2003
@@ -18,18 +18,18 @@
       menu="zmi_views" title="Utilities"
       name="utilities.html"
       template="utilities.pt"
-      class=".useconfiguration.Utilities"
+      class=".registered.Utilities"
       permission="zope.ManageServices"
       />
 
 <!-- Browser directives for individual utility objects -->
 
-  <!-- Configuration page for utility objects.  You get here by
+  <!-- Registration page for utility objects.  You get here by
        clicking on the (configure) link for a particular utility
        in the "Utilities" tab of the utility service.  It shows
-       a menu of different configurations, at most one of which
-       is active.  You can activate a different configuration, or
-       click on an individual configuration to edit it.
+       a menu of different registrations, at most one of which
+       is active.  You can activate a different registration, or
+       click on an individual registration to edit it.
        (Note that this page doesn't really apply to a single utility,
        it applies to a single "utility registration".  That is a
        combination of a name and a provided interface, where the name
@@ -38,7 +38,7 @@
       for="zope.app.interfaces.services.utility.ILocalUtilityService"
       name="configureutility.html"
       template="configureutility.pt"
-      class=".useconfiguration.ConfigureUtility"
+      class=".registered.ConfigureUtility"
       permission="zope.ManageServices"
       />
 
@@ -49,17 +49,17 @@
   <addform
       label="New Utility Registration"
       for="zope.app.interfaces.services.utility.ILocalUtility"
-      name="addConfiguration.html"
-      schema="zope.app.interfaces.services.utility.IUtilityConfiguration"
-      class=".useconfiguration.AddConfiguration"
+      name="addRegistration.html"
+      schema="zope.app.interfaces.services.utility.IUtilityRegistration"
+      class=".registered.AddRegistration"
       permission="zope.ManageServices"
-      content_factory="zope.app.services.utility.UtilityConfiguration"
+      content_factory="zope.app.services.utility.UtilityRegistration"
       arguments="name interface componentPath"
       set_after_add="status"
       fields="name interface componentPath permission status"
       />
 
-  <!-- When editing the configuration of an existing utility object,
+  <!-- When editing the registration of an existing utility object,
        you are taken to this form.  It is similar to the above add
        form, but doesn't let you change the name, interface or path.
        (Thus leaving only permission and registration status.) -->
@@ -67,7 +67,7 @@
       menu="zmi_views" title="Edit"
       label="Edit Utility Registration"
       name="index.html"
-      schema="zope.app.interfaces.services.utility.IUtilityConfiguration"
+      schema="zope.app.interfaces.services.utility.IUtilityRegistration"
       permission="zope.ManageServices"
       fields="name interface componentPath permission status"
       />

=== Removed File Zope3/src/zope/app/browser/services/utility/useconfiguration.py ===