[Zope3-checkins] CVS: Zope3/src/zope/app/browser/services/utility - __init__.py:1.2.16.1 configure.zcml:1.10.12.2

Albertas Agejevas alga@codeworks.lt
Mon, 23 Jun 2003 10:21:19 -0400


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

Modified Files:
      Tag: cw-mail-branch
	__init__.py configure.zcml 
Log Message:
One more sync with HEAD.

=== Zope3/src/zope/app/browser/services/utility/__init__.py 1.2 => 1.2.16.1 ===
--- Zope3/src/zope/app/browser/services/utility/__init__.py:1.2	Fri Mar 21 16:02:18 2003
+++ Zope3/src/zope/app/browser/services/utility/__init__.py	Mon Jun 23 10:19:47 2003
@@ -1,2 +1,201 @@
+##############################################################################
 #
-# This file is necessary to make this directory a package.
+# 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$
+"""
+
+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.12.1 => 1.10.12.2 ===
--- Zope3/src/zope/app/browser/services/utility/configure.zcml:1.10.12.1	Sun Jun 22 10:22:47 2003
+++ Zope3/src/zope/app/browser/services/utility/configure.zcml	Mon Jun 23 10:19:47 2003
@@ -18,7 +18,7 @@
       menu="zmi_views" title="Utilities"
       name="utilities.html"
       template="utilities.pt"
-      class=".registered.Utilities"
+      class=".Utilities"
       permission="zope.ManageServices"
       />
 
@@ -38,7 +38,7 @@
       for="zope.app.interfaces.services.utility.ILocalUtilityService"
       name="configureutility.html"
       template="configureutility.pt"
-      class=".registered.ConfigureUtility"
+      class=".ConfigureUtility"
       permission="zope.ManageServices"
       />
 
@@ -51,7 +51,7 @@
       for="zope.app.interfaces.services.utility.ILocalUtility"
       name="addRegistration.html"
       schema="zope.app.interfaces.services.utility.IUtilityRegistration"
-      class=".registered.AddRegistration"
+      class=".AddRegistration"
       permission="zope.ManageServices"
       content_factory="zope.app.services.utility.UtilityRegistration"
       arguments="name interface componentPath"