[Zope3-checkins] CVS: Zope3/src/zope/app/presentation - __init__.py:1.1 configure.zcml:1.1 presentation.py:1.1 tests.py:1.1

Jim Fulton jim at zope.com
Mon Mar 8 14:40:27 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/presentation
In directory cvs.zope.org:/tmp/cvs-serv28862/src/zope/app/presentation

Added Files:
	__init__.py configure.zcml presentation.py tests.py 
Log Message:
Moved the adapter and presentation services from zope.app.services to
their own packages under zope.app. Also folded
zope.app.services.surrogate into the new adapter module,
zope.app.adapter.adapter.


=== Added File Zope3/src/zope/app/presentation/__init__.py ===
##############################################################################
#
# Copyright (c) 2004 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.
#
##############################################################################
"""Presentation-component support

$Id: __init__.py,v 1.1 2004/03/08 19:40:26 jim Exp $
"""

from zope.app.presentation.presentation import IPageRegistration
from zope.app.presentation.presentation import PageRegistration
from zope.app.presentation.presentation import LocalPresentationService


=== Added File Zope3/src/zope/app/presentation/configure.zcml ===
<configure
    xmlns='http://namespaces.zope.org/zope'
    xmlns:event='http://namespaces.zope.org/event'
    xmlns:fssync='http://namespaces.zope.org/fssync'
    i18n_domain="zope"
    >


<content class=".presentation.LocalPresentationService">
  <factory permission="zope.ManageServices" />
  <require
      permission="zope.ManageServices"
      interface="zope.app.interfaces.services.registration.IRegistry"
      attributes="getRegisteredMatching"
      />
</content>

<content class=".presentation.ViewRegistration">
  <require
      permission="zope.ManageServices"
      interface=".presentation.IViewRegistration"
      set_schema="zope.app.interfaces.services.registration.IRegistration"
      />
  <require
      permission="zope.ManageServices"
      interface="zope.app.container.interfaces.IRemoveNotifiable"
      />
</content>

<content class=".presentation.PageRegistration">
  <require
      permission="zope.ManageServices"
      interface=".presentation.IPageRegistration"
      set_schema=".presentation.IPageRegistration"
      />
  <require
      permission="zope.ManageServices"
      interface="zope.app.container.interfaces.IRemoveNotifiable"
      />
</content>

<modulealias module="zope.app.presentation.presentation"
             alias="zope.app.services.presentation"
             />

<include package=".browser" />

</configure>


=== Added File Zope3/src/zope/app/presentation/presentation.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.
#
##############################################################################
"""Local presentation service

$Id: presentation.py,v 1.1 2004/03/08 19:40:26 jim Exp $
"""

from zope.app import zapi
from zope.app.i18n import ZopeMessageIDFactory as _
from zope.component.presentation import IDefaultViewName
from zope.security.checker import NamesChecker, ProxyFactory
import persistent.dict
import zope.app.component.interfacefield
import zope.app.component.nextservice
import zope.app.container.contained
import zope.app.interfaces.services.registration
import zope.app.interfaces.services.service
import zope.app.adapter
import zope.app.services.field
import zope.app.interfaces.services.interface
import zope.app.adapter
import zope.app.services.zpt
import zope.component.interfaces
import zope.configuration.exceptions
import zope.interface
import zope.proxy
import zope.publisher.interfaces.browser
import zope.schema
from zope.app.container.interfaces import IAddNotifiable
from zope.app.interfaces.dependable import IDependable, DependencyError
from zope.app.interfaces.services.registration import IRegistered

# XXX How do we define skins and layers here?
# For now, we will leave skin and layer definition to services above,
# which effectively means to the global service.

class LocalPresentationService(
    zope.app.adapter.LocalAdapterBasedService,
    ):

    zope.interface.implements(
        zope.component.interfaces.IPresentationService,
        zope.app.interfaces.services.service.ISimpleService,
        zope.app.interfaces.services.registration.IRegistry,
        zope.app.interfaces.services.interface.IInterfaceBasedRegistry,
        )

    next = base = None

    def __init__(self):
        self.layers = persistent.dict.PersistentDict()
        self.base = zapi.getService(None, zapi.servicenames.Presentation)

    def setNext(self, next, global_):
        if next is None:
            self.delegate = global_
        else:
            self.delegate = next
            
        self.next = next
        self.base = global_
        for layername in self.layers:
            nextlayer = next.queryLayer(layername)
            globlayer = global_.queryLayer(layername)
            self.layers[layername].setNext(nextlayer, globlayer)

    def defaultSkin(self):
        return self.delegate.defaultSkin
    defaultSkin = property(defaultSkin)

    def querySkin(self, name):
        return self.delegate.querySkin(name)

    def queryLayer(self, name):
        r = self.layers.get(name)
        if r is not None:
            return r
        return self.delegate.queryLayer(name)

    def queryView(self, object, name, request,
                  providing=zope.interface.Interface, default=None):
        """Look for a named view for a given object and request

        The request must implement IPresentationRequest.

        The default will be returned if the component can't be found.
        """
        skin = request.getPresentationSkin() or self.defaultSkin
        layers = self.querySkin(skin)
        if not layers:
            return default
        
        objects = object, request
        for layername in layers:
            layer = self.layers.get(layername)
            if layer is None:
                layer = self.delegate.queryLayer(layername)
                if layer is None:
                    raise ValueError("Bad layer", layer)

            r = layer.queryMultiAdapter(objects, providing, name)
            if r is not None:
                return r
        return default


    def queryResource(self, name, request, providing=zope.interface.Interface,
                      default=None):
        """Look up a named resource for a given request
        
        The request must implement IPresentationRequest.
        
        The default will be returned if the component can't be found.
        """
        skin = request.getPresentationSkin() or self.defaultSkin
        layers = self.querySkin(skin)
        if not layers:
            return default

        for layername in layers:
            layer = self.layers.get(layername)
            if layer is None:
                layer = self.delegate.queryLayer(layername)
            if layer is None:
                raise ValueError("Bad layer", layer)

            r = layer.queryNamedAdapter(request, providing,
                                        name)
            if r is not None:
                return r

        return default

    def queryMultiView(self, objects, name, request,
                       providing=zope.interface.Interface, default=None):
        """Adapt the given objects and request

        The first argument is a sequence of objects to be adapted with the
        request.
        """

        skin = request.getPresentationSkin() or self.defaultSkin
        layers = self.querySkin(skin)
        if not layers:
            return default

        objects = objects + (request, )
        for layername in layers:
            layer = self.layers.get(layername)
            if layer is None:
                layer = self.delegate.queryLayer(layername)
            if layer is None:
                raise ValueError("Bad layer", layer)

            r = layer.queryMultiAdapter(objects, providing, name)
            if r is not None:
                return r
        return default

    def queryDefaultViewName(self, object, request, default=None):
        skin = request.getPresentationSkin() or self.defaultSkin
        layers = self.querySkin(skin)
        if not layers:
            return default

        objects = object, request
        for layername in layers:
            layer = self.layers.get(layername)
            if layer is None:
                layer = self.delegate.queryLayer(layername)
            if layer is None:
                raise ValueError("Bad layer", layer)
            r = layer.queryMultiAdapter(objects, IDefaultViewName, raw=True)
            if r is not None:
                return r
        return default


    def queryRegistrationsFor(self, registration, default=None):
        layername = registration.layer
        layer = self.layers.get(layername)
        if layer is None:
            return default
        return layer.queryRegistrationsFor(registration, default)

    def createRegistrationsFor(self, registration):
        layername = registration.layer
        layer = self.layers.get(layername)
        if layer is None:
            if self.next is None:
                next = None
            else:
                next = self.next.queryLayer(layername)
            base = self.base.queryLayer(layername)
            if base is None:
                raise ValueError("Undefined layer", layername)
            layer = LocalLayer(base, next, self, layername)
            self.layers[layername] = layer
            
        return layer.createRegistrationsFor(registration)

    def getRegistrationsForInterface(self, required):
        # XXX relying on global service for layer definitions

        iro = required.__iro__ + (None,)
        for layername in self.base._layers:
            layer = self.queryLayer(layername)
            if isinstance(layer, LocalLayer):
                while layer is not None:
                    for iface in iro:
                        stacks = layer.stacks.get(iface)
                        if not stacks:
                            continue
                        for stack in stacks.itervalues():
                            registration = stack.active()
                            if registration is not None:
                                yield registration
                    layer = layer.next
                layer = self.base.queryLayer(layername)
            if layer is None:
                continue

            for (req, provided, with, name, factories
                 ) in layer.getRegisteredMatching(required=required):
                # XXX just do views for now. We need a more general
                # solution
                if len(with) == 1:
                    yield GlobalViewRegistration(req, with[0], factories,
                                                 layername, name)
                    
                

class GlobalViewRegistration:
    """Registrations representing global view service thingies."""

    zope.interface.implements(
        zope.app.interfaces.services.registration.IRegistration)

    serviceType = zapi.servicenames.Presentation
    status = zope.app.interfaces.services.registration.ActiveStatus

    def __init__(self, req, ptype, factories, layer, viewName):
        self.required = req
        self.ptype = ptype
        self.factories = factories
        self.layer = layer
        self.viewName = viewName

    def usageSummary(self):
        if self.required is None:
            ifname = _("any-interface", "Anything")
        else:
            ifname = self.required.getName()
        summary = _("${view_name} ${ptype} View for ${iface_name}")
        if self.layer and self.layer != "default":
            summary = _(
                "${view_name} ${ptype} View for ${iface_name} in layer ${layer}"
                )
        summary.mapping = {'view_name':  self.viewName,
                           'ptype':      self.ptype.getName(),
                           'iface_name': ifname,
                           'layer':      self.layer}
        return summary

    def implementationSummary(self):
        # XXX This should report the ZCML that it came from.
        return _("Registered by ZCML")


class LocalLayer(
    zope.app.adapter.LocalAdapterRegistry,
    zope.app.container.contained.Contained,
    ):

    def __init__(self, base, next, parent, name):
        zope.app.adapter.LocalAdapterRegistry.__init__(
            self, base, next)
        self.__parent__ = parent
        self.__name__ = name


class IViewRegistration(zope.app.adapter.IAdapterRegistration):

    required = zope.app.component.interfacefield.InterfaceField(
        title = u"For interface",
        description = u"The interface of the objects being viewed",
        readonly = True,
        required = True,
        basetype = None
        )

    requestType = zope.app.component.interfacefield.InterfaceField(
        title = u"Request type",
        description = u"The type of requests the view works with",
        readonly = True,
        required = True,
        )

    layer = zope.schema.BytesLine(
        title = u"Layer",
        description = u"The skin layer the view is registered for",
        required = False,
        readonly = True,
        min_length = 1,
        default = "default",
        )

class ViewRegistration(zope.app.services.registration.SimpleRegistration):

    zope.interface.implements(IViewRegistration)

    serviceType = zapi.servicenames.Presentation

    provided = zope.interface.Interface

    # For usageSummary(); subclass may override
    _what = _("view-component", 'View')

    def __init__(self,
                 required, name, requestType,
                 factoryName, permission, layer='default'):
        self.required = required
        self.requestType = requestType
        self.factoryName = factoryName
        self.name = name
        self.layer = layer
        self.permission = permission

    def usageSummary(self):
        if self.required is None:
            ifname = _('any-interface', "Anything")
        else:
            ifname = self.required.getName()

        pname = self.requestType.getName()
        summary = _("${view_name} for ${pname} {what} {iface_name}")
        if self.layer and self.layer != "default":
            summary = _(
                "${view_name} for ${pname} ${what} ${iface_name}"
                " in layer ${layer}"
                )
        summary.mapping = {'view_name':  self.name,
                           'pname':      pname,
                           'what':       self._what,
                           'iface_name': ifname,
                           'layer':      self.layer}
        return summary

    def with(self):
        return (self.requestType, )
    with = property(with)

    def factories(self):
        folder = self.__parent__.__parent__
        return (folder.resolve(self.factoryName), )
    factories = property(factories)

class IPageRegistration(IViewRegistration):

    factoryName = zope.schema.BytesLine(
        title=u"Page class",
        required = False,
        )

    template = zope.app.interfaces.services.registration.ComponentPath(
        title = u"Page template",
        required = False,
        )

    attribute = zope.schema.TextLine(
        title = u"Class attribute",
        required = False,
        )

    factories = zope.interface.Attribute(
        "A sequence of factories to be called to construct an adapter"
        )

    def validate(self):
        """Verifies that the registration is valid.

        Raises a ConfigurationError if the validation is failed.
        """

class PageRegistration(ViewRegistration):

    zope.interface.implements(IPageRegistration, IAddNotifiable)

    # We only care about browser pages
    requestType = zope.publisher.interfaces.browser.IBrowserRequest

    # For usageSummary()
    _what = _("page-component", "Page")

    def __init__(self,
                 required, name, permission,
                 factoryName=None, template=None, attribute=None,
                 layer='default'):

        # XXX A Interface comes out of the interface widget
        # wrapped on a proxy currently, which is not pickable
        required = zope.proxy.removeAllProxies(required)

        super(PageRegistration, self).__init__(
            required, name, self.requestType,
            factoryName, permission, layer)

        self.template = template
        self.attribute = attribute

    def implementationSummary(self):
        L = []
        if self.template:
            prefix = "/++etc++site/"
            t = self.template
            i = t.rfind(prefix)
            if i >= 0:
                t = t[i + len(prefix):]
            L.append("template=%s" % t)
        if self.factoryName:
            L.append("class=%s" % self.factoryName)
        if self.attribute:
            L.append("attribute=%s" % self.attribute)
        return ", ".join(L)

    def validate(self):
        if self.template and self.attribute:
            raise zope.configuration.exceptions.ConfigurationError(
                "PageRegistration for %s view name %s: "
                "Cannot have both 'template' and 'attribute' at the same "
                "time." %
                (self.required, self.name))

        if not self.template and not self.attribute:
            raise zope.configuration.exceptions.ConfigurationError(
                "PageRegistration for %s view name %s: "
                "Should have a 'template' or 'attribute' attribute." %
                (self.required, self.name))

        if not self.factoryName and self.attribute:
            raise zope.configuration.exceptions.ConfigurationError(
                "PageRegistration for %s view name %s: "
                "Cannot have an 'attribute' without a 'factoryName'." %
                (self.required, self.name))

    def factories(self):

        self.validate()

        sm = zapi.getServiceManager(self)

        if self.factoryName:
            folder = self.__parent__.__parent__
            class_ = folder.resolve(self.factoryName)
        else:
            class_  = DefaultClass



        if self.attribute:
            return (AttrViewFactory(class_, self.attribute), )

        else:

            if self.template[0]=='/':
                # This is needed because we need to do an unrestricted zapi.
                # traverse
                root = zope.proxy.removeAllProxies(zapi.getRoot(sm))
                template = zapi.traverse(root, self.template)
            else:
                template = zapi.traverse(self.__parent__.__parent__,
                                         self.template)
            return (TemplateViewFactory(class_, template, self.permission), )

    factories = property(factories)


    def addNotify(self, event):
        "See IAddNotifiable"
        template = zapi.traverse(self.__parent__.__parent__,self.template)
        dependents = IDependable(template)
        objectpath = zapi.getPath(self)
        dependents.addDependent(objectpath)
        # Also update usage, if supported
        adapter = IRegistered(template, None)
        if adapter is not None:
            adapter.addUsage(objectpath)


    def removeNotify(self, event):
        "See IRemoveNotifiable"
        super(PageRegistration, self).removeNotify(event)
        template = zapi.traverse(self.__parent__.__parent__,self.template)
        dependents = IDependable(template)
        objectpath = zapi.getPath(self)
        dependents.addDependent(objectpath)
        # Also update usage, if supported
        adapter = IRegistered(template, None)
        if adapter is not None:
            adapter.removeUsage(zapi.getPath(self))


class TemplateViewFactory:

    def __init__(self, cls, template, permission):
        self.cls, self.template, self.permission = cls, template, permission

    def __call__(self, object, request):
        checker = NamesChecker(__call__ = self.permission)
        template = BoundTemplate(self.template, self.cls(object, request))
        return ProxyFactory(template, checker)

class AttrViewFactory:

    def __init__(self, cls, attr):
        self.cls, self.attr = cls, attr

    def __call__(self, object, request):
        attr = getattr(self.cls(object, request), self.attr)
        return ProxyFactory(attr)

class DefaultClass:

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

class BoundTemplate:

    def __init__(self, template, view):
        self.template = template
        self.view = view

    def __call__(self, template_usage=u'', *args, **kw):
        if not template_usage:
            kw["template_usage"] = template_usage
        return self.template.render(self.view, *args, **kw)

#BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB

from zope.app.event.function import Subscriber
import persistent
import sys
from zope.interface.adapter import ReadProperty

ViewRegistration.required    = ReadProperty(lambda self: self.forInterface)
ViewRegistration.factoryName = ReadProperty(lambda self: self.class_)
ViewRegistration.name        = ReadProperty(lambda self: self.viewName)

class ViewService(persistent.Persistent):
    pass

def fixup(event):
    # We delay this evil hackery until database open to prevent someone
    # successfully importing IBrowserPresentation through a normal import
    sys.modules['zope.app.services.view'] = sys.modules[__name__]
    IBrowserRequest = zope.publisher.interfaces.browser.IBrowserRequest
    zope.publisher.interfaces.browser.IBrowserPresentation = IBrowserRequest 
    
fixup = Subscriber(fixup)


=== Added File Zope3/src/zope/app/presentation/tests.py ===
##############################################################################
#
# Copyright (c) 2001, 2002 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.
#
##############################################################################
"""Test the presentation module

$Id: tests.py,v 1.1 2004/03/08 19:40:26 jim Exp $
"""

from unittest import TestCase, TestSuite, main, makeSuite
from zope.testing.doctestunit import DocTestSuite
from zope.app.tests.placelesssetup import setUp, tearDown

from zope.app import zapi
from zope.interface import Interface, directlyProvides, implements
from zope.interface.verify import verifyObject

from zope.app.folder import rootFolder
from zope.app.services.zpt import IZPTTemplate
from zope.app.services.service import ServiceManager
from zope.app.services.servicenames import Presentation
from zope.app.services.tests.iregistry import TestingIRegistry
from zope.app.services.tests.placefulsetup import PlacefulSetup
from zope.app.presentation.presentation import ViewRegistration
from zope.app.presentation.presentation import PageRegistration
from zope.app.presentation.presentation import BoundTemplate
from zope.app.presentation.presentation import LocalPresentationService
from zope.app.tests import setup
from zope.app.traversing import traverse

from zope.component.exceptions import ComponentLookupError
from zope.component.interfaces import IServiceService
from zope.component.interfaces import IPresentationService
from zope.app.tests import ztapi
from zope.configuration.exceptions import ConfigurationError

from zope.proxy import removeAllProxies

from zope.publisher.browser import TestRequest
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.app.container.contained import contained

from zope.app.interfaces.dependable import IDependable
from zope.app.interfaces.annotation import IAttributeAnnotatable
from zope.app.dependable import Dependable

class I1(Interface):
    pass

class I1E(I1):
    pass

I2 = IBrowserRequest

class I3(Interface):
    pass

class I4(Interface):
    pass


class Registration:
    required = I1
    requestType = I2
    name = 'test'
    layer = 'default'
    serviceType = Presentation
    provided = Interface

    with = property(lambda self: (self.requestType, ))
    factories = property(lambda self: (self.factory, ))

    def activated(self): pass
    def deactivated(self): pass

class C: pass

class PhonyTemplate:
    __name__ = __parent__ = None
    implements(IZPTTemplate)

class A:
    def __init__(self, object, request):
        self.context = object
        self.request = request

    run = PhonyTemplate()

class TestLocalPresentationService(PlacefulSetup, TestingIRegistry, TestCase):

    def setUp(self):
        sm = PlacefulSetup.setUp(self, site=True)
        self._service = setup.addService(sm, Presentation,
                                         LocalPresentationService())

    def test_defaultSkin(self):
        # XXX we don't let people set the default skin locally yet.
        # So just test that we can get the default from the global service
        zapi.getService(None, Presentation).defineSkin('bob', ['default'])
        zapi.getService(None, Presentation).setDefaultSkin('bob')
        self.assertEqual(self._service.defaultSkin, 'bob')

    def test_querySkin(self):
        # XXX we don't let people define skins locally yet.
        # So just test that we can get the defs from the global service
        globalService = zapi.getService(None, Presentation)
        globalService.defineLayer('bob')
        globalService.defineSkin('bob', ['bob', 'default'])
        self.assertEqual(self._service.querySkin('bob'), ('bob', 'default'))
        
    def test_queryLayer(self):
        # XXX we don't let people define layers locally yet.
        # So just test that we can get the them from the global service
        globalService = zapi.getService(None, Presentation)
        layer = self._service.queryLayer('default')
        self.assertEqual(layer.__parent__, globalService)
        self.test_queryView()
        layer = self._service.queryLayer('default')
        self.assertEqual(layer.__parent__, self._service)

    def test_queryDefaultViewName(self):
        # XXX we don't let people define the default view name locally
        # yet.  So just test that we can get it from the global
        # service
        class O:
            implements(I1)
        o = O()
        r = TestRequest()
        self.assertEqual(self._service.queryDefaultViewName(o, r),
                         None)
        globalService = zapi.getService(None, Presentation)
        globalService.setDefaultViewName(I1, IBrowserRequest, 'foo.html')
        self.assertEqual(self._service.queryDefaultViewName(o, r),
                         'foo.html')

    def test_queryMultiView(self):
        # XXX that we don't let people define multiviews locally yet.
        # So just test that we can get them from the global service
        class X:
            implements(I1)
        class Y:
            implements(I3)
        x = X()
        y = Y()
        r = TestRequest()
        self.assertEqual(self._service.queryMultiView((x, y), 'foo.html', r),
                         None)
        globalService = zapi.getService(None, Presentation)

        class MV:
            def __init__(self, x, y, request):
                self.x, self.y, self.request = x, y, request
                
        globalService.provideAdapter(IBrowserRequest, [MV], 'foo.html',
                                     contexts=(I1, I3))
        v = self._service.queryMultiView((x, y), 'foo.html', r)
        self.assertEqual(v.__class__, MV)
        self.assertEqual(v.request, r)
        self.assertEqual(v.x, x)
        self.assertEqual(v.y, y)
        

    def test_queryResource(self):
        # XXX that we don't let people define resources locally yet.
        # So just test that we can get them from the global service

        r = TestRequest()
        self.assertEqual(self._service.queryResource('logo.gif', r),
                         None)

        class Resource:
            def __init__(self, request):
                self.request = request

        globalService = zapi.getService(None, Presentation)
        globalService.provideResource('logo.gif', IBrowserRequest, Resource)
        
        resource = self._service.queryResource('logo.gif', r)
        self.assertEqual(resource.__class__, Resource)
        self.assertEqual(resource.request, r)

    def test_implements_IPresentationService(self):
        from zope.component.interfaces import IPresentationService

        verifyObject(IPresentationService, self._service)

    def createTestingRegistry(self):
        return contained(LocalPresentationService(), C())

    def createTestingRegistration(self):
        return Registration()

    def test_implements_IPresentationService(self):
        verifyObject(IPresentationService, LocalPresentationService())

    def test_queryView_no_view(self):
        service = self._service
        class O:
            implements(I1)

        o = O()
        request = TestRequest()
        self.assertEqual(service.queryView(o, 'test', request), None)
        self.assertEqual(service.queryView(o, 'test', request, default=42), 42)

    def test_queryView(self):
        sm = traverse(self.rootFolder, '++etc++site')

        registration_manager = traverse(sm, 'default').getRegistrationManager()
        key = registration_manager.addRegistration(Registration())
        registration = traverse(registration_manager, key)

        class O:
            implements(I1)

        registration.factory = A

        registry = self._service.createRegistrationsFor(registration)
        registry.register(registration)
        registry.activate(registration)

        o = O()
        request = TestRequest()

        for r in I1, I1E:
            o = O()
            directlyProvides(o, r)

            view = self._service.queryView(o, 'test', request)
            self.assertEqual(view.__class__, A)
            self.assertEqual(view.context, o)
            self.assertEqual(view.request, request)

    def test_queryView_delegation(self):
        service = self._service

        sm = self.buildFolders(site=True)
        registration_manager = traverse(sm, 'default').getRegistrationManager()
        registration = Registration()
        name = registration_manager.addRegistration(registration)
        registration = traverse(registration_manager, name)

        class O:
            implements(I1)

        o = O()
        request = TestRequest()

        class A2(A): pass

        ztapi.browserView(I1, 'test', A2)

        view = service.queryView(o, 'test', request)
        self.assertEqual(view.__class__, A2)
        self.assertEqual(view.context, o)
        self.assertEqual(view.request, request)

    def test_getRegistrationsForInterface(self):
        self.test_queryView()
        for reg in self._service.getRegistrationsForInterface(I1):
            if reg.required is None:
                continue
            self.assertEqual(reg.required, I1)

        for reg in self._service.getRegistrationsForInterface(I1E):
            if reg.required is None:
                continue
            self.assertEqual(reg.required, I1)


class PhonyServiceManager(ServiceManager):

    implements(IServiceService)

class ModuleFinder:

    def resolve(self, name):
        if name == "Foo.Bar.A":
            return A
        raise ImportError(name)


class TestViewRegistration(PlacefulSetup, TestCase):

    def test_factories(self):
        folder = ModuleFinder()
        folder = contained(folder, folder)
        registration = contained(
            ViewRegistration(I1, 'test', I2, "Foo.Bar.A", 'zope.View'),
            folder,
            )

        self.assertEqual(registration.required, I1)
        self.assertEqual(registration.requestType, I2)

        factory, = registration.factories
        self.assertEqual(factory, A)


class TestPageRegistration(PlacefulSetup, TestCase):

    def setUp(self):
        PlacefulSetup.setUp(self)
        self.rootFolder = rootFolder()
        self.rootFolder.setSiteManager(PhonyServiceManager(self.rootFolder))
        default = traverse(self.rootFolder, '++etc++site/default')
        self.__template = PhonyTemplate()
        default['t'] = self.__template
        self.folder = contained(ModuleFinder(), self.rootFolder)
        self.folder = contained(ModuleFinder(), self.folder)

    def test_factories_template(self):
        registration = contained(
            PageRegistration(I1, 'test', 'zope.View',
                              "Foo.Bar.A",
                              template='/++etc++site/default/t',
                              ),
            self.folder,
            )

        c = C()
        request = TestRequest()
        factory, = registration.factories
        view = factory(c, request)
        self.assertEqual(view.__class__, BoundTemplate)
        self.assertEqual(removeAllProxies(view).template, self.__template)

        view = removeAllProxies(view).view
        self.assert_(issubclass(view.__class__, A))
        self.assertEqual(view.context, c)
        self.assertEqual(view.request, request)
        self.assertEqual(registration.required, I1)
        self.assertEqual(registration.requestType, I2)

    def test_factories_attribute(self):
        registration = contained(
            PageRegistration(
                I1, 'test', 'zope.View', "Foo.Bar.A", attribute='run'),
            self.folder,6
            )
        c = C()
        request = TestRequest()
        factory, = registration.factories
        view = factory(c, request)
        self.assertEquals(view, A.run)

    def test_factories_errors(self):
        registration = contained(
            PageRegistration(I1, 'test', 'zope.View', "Foo.Bar.A"),
            self.folder,
            )
        c = C()
        request = TestRequest()
        self.assertRaises(ConfigurationError, lambda: registration.factories)
        registration.template = '/++etc++site/default/t'
        registration.attribute = 'run'
        self.assertRaises(ConfigurationError, lambda: registration.factories)


def test_PageRegistration_addremoveNotify():
    """for addNotify and removeNotify

    XXX
      - Jim suggested we can write unit test later.

      - It will be easiar to write unit test, for Direct reference.

    """


def test_suite():
    return TestSuite([
        makeSuite(TestLocalPresentationService),
        makeSuite(TestViewRegistration),
        makeSuite(TestPageRegistration),
        ])

if __name__ == '__main__':
    main(defaultTest='test_suite')






More information about the Zope3-Checkins mailing list