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

Jim Fulton jim@zope.com
Thu, 13 Mar 2003 13:35:01 -0500


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

Added Files:
      Tag: local-utility-branch
	configure.zcml __init__.py 
Log Message:
committing to branch

=== Added File Zope3/src/zope/app/browser/services/utility/configure.zcml ===
<zopeConfigure xmlns='http://namespaces.zope.org/browser'>

<menuItem
      for="zope.app.interfaces.container.IAdding"
      menu="add_component"
      action="zope.app.services.UtilityService"
      title="Utility Service"
      permission="zope.ManageServices"
      />

</zopeConfigure>


=== Added File Zope3/src/zope/app/browser/services/utility/__init__.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.
##############################################################################
"""Utility views

$Id: __init__.py,v 1.1.2.1 2003/03/13 18:35:00 jim Exp $
"""
__metaclass__ = type

from zope.app.form.utility import setUpWidget, getWidgetsData, setUpWidgets
from zope.app.form.utility import getWidgetsData
from zope.app.interfaces.services.utility import IUtilityConfiguration
from zope.app.services.utility import UtilityConfiguration
from zope.app.browser.form.add import AddView
from zope.proxy.context import ContextWrapper
from zope.component import getServiceManager
from zope.app.interfaces.form import WidgetsError

class Add:

    errors = ()

    def __init__(self, context, request):
        self.context = context
        self.request = request


    # We have two forms
    # The first form has a next button
    # the second form has a previous button and a finish button

    def widgets(self):
        return [getattr(self, name) for name in IUtilityConfiguration]


    def _form1(self):
        # Initial display
        setUpWidget(self, 'interface', IUtilityConfiguration['interface'],
                    vname='edit')
        setUpWidget(self, 'name', IUtilityConfiguration['name'],
                    vname='edit')
        self.buttons = (
            '<input type="submit" name="NEXT_SUBMIT" value="Next" />')

        for name in ('permission', 'componentGlobalName', 'componentPath',
                     'title', 'description', 'status'):
            setUpWidget(self, name, IUtilityConfiguration[name],
                        vname='hidden')

        return ''

    def _form2(self):
        # next button

        # Check to make sure the interface is valid:
        setUpWidget(self, 'interface', IUtilityConfiguration['interface'],
                    vname='readonly')

        try:
            getWidgetsData(self, IUtilityConfiguration, names=['interface'])
        except WidgetsError, errors:
            # Invalid interface. Go back

            # Save the errors for display
            self.errors = errors

            # remove the widget so we can recreate an edit widget:
            del self.interface

            self._form1()
            return u"An invalid interface was selected"

        setUpWidget(self, 'name', IUtilityConfiguration['name'],
                    vname='readonly')

        for name in ('permission', 'componentGlobalName', 'componentPath',
                     'title', 'description', 'status'):
            setUpWidget(self, name, IUtilityConfiguration[name],
                        vname='edit')

        # Limit the component paths to paths of components that
        # implement the interface:
        services_manager = getServiceManager(self.context.context)
        self.componentPath.allowed_values = [
            info['path']
            for info
            in service_manager.queryComponent(type=self.interface.getData())
            ]

        self.buttons = (
            '<input type="submit" value="Previous" /> '
            '<input type="submit" name="FINISH_SUBMIT" value="Next" />'
            )

        return ''

    def _finish(self):
        # finish button

        # Set up the widgets as for _form2.
        r = self._form2()

        # If there were errors, we need to go back to form 1, which
        # _form2 has already done.
        if r:
            return r

        try:
            data = getWidgetsData(self, IUtilityConfiguration,
                                  required=0)
        except WidgetsError, errors:
            self.errors = errors
            return u"An error occured."

        if data.get('componentPath') and data.get('componentGlobalName'):
            return u"Can't specify a global name and a path."

        config = UtilityConfiguration(**data)
        wrapped_config = ContextWrapper(config, self.context.context)
        if not config.interface.isImplementedBy(wrapped_config.getComponent()):
            return (u"The specified component doesn't implement "
                    u"the selected interface")

        config = self.context.add(config)

        self.request.response.redirect(self.context.nextURL())

        return ''

    def update(self):
        if "NEXT_SUBMIT" in self.request:
            return self._form2()
        
        elif "FINISH_SUBMIT" in self.request:
            return self._finish()

        else:
            return self._form1()