Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать
12 class BlogEntry(object):
13 def __init__(self, title, body, author): 14 self.title = title
15 self.body = body
16 self.author = author
17 self.created = datetime.datetime.now()
18
19 entry = BlogEntry(’title’, ’body’, ’author’) 20 directlyProvides(entry, IBlogEntry1)
21 alsoProvides(entry, IBlogEntry2)
zope.interface.alsoProvides() will augment the set of interfaces directly provided by an instance instead of overwriting them like zope.interface.directlyProvides() does.
For more information about how resource interfaces can be used by view configuration, see Using Resource Interfaces In View Configuration.
24.10 Finding a Resource With a Class or Interface in Lineage
Use the find_interface() API to locate a parent that is of a particular Python class, or which implements some interface.
For example, if your resource tree is composed as follows:
271

24.10. FINDING A RESOURCE WITH A CLASS OR INTERFACE IN LINEAGE

14

15

16

entry = BlogEntry(’title’, ’body’, ’author’) directlyProvides(entry, IBlogEntry)

zope.interface.directlyProvides() will replace any existing interface that was previously provided by an instance. If a resource object already has instance-level interface declarations that you don’t want to replace, use the zope.interface.alsoProvides() function:

1

import datetime

2

from zope.interface import alsoProvides

3

from zope.interface import directlyProvides

4

from zope.interface import Interface

5

 

6

class IBlogEntry1(Interface):

7pass

8

 

9

class IBlogEntry2(Interface):

10

pass

11

 

24. RESOURCES

1

2

3

4

5

6

class Thing1(object): pass class Thing2(object): pass

a = Thing1() b = Thing2()

b.__parent__ = a

Calling find_interface(a, Thing1) will return the a resource because a is of class Thing1 (the resource passed as the first argument is considered first, and is returned if the class or interface spec matches).

Calling find_interface(b, Thing1) will return the a resource because a is of class Thing1 and a is the first resource in b‘s lineage of this class.

Calling find_interface(b, Thing2) will return the b resource.

The second argument to find_interface may also be a interface instead of a class. If it is an interface, each resource in the lineage is checked to see if the resource implements the specificed interface (instead of seeing if the resource is of a class). See also Resources Which Implement Interfaces.

24.11 Pyramid API Functions That Act Against Resources

A resource object is used as the context provided to a view. See Traversal and URL Dispatch for more information about how a resource object becomes the context.

The APIs provided by pyramid.traversal are used against resource objects. These functions can be used to find the “path” of a resource, the root resource in a resource tree, or to generate a URL for a resource.

The APIs provided by pyramid.location are used against resources. These can be used to walk down a resource tree, or conveniently locate one resource “inside” another.

Some APIs in pyramid.security accept a resource object as a parameter. For example, the has_permission() API accepts a resource object as one of its arguments; the ACL is obtained from this resource or one of its ancestors. Other APIs in the pyramid.security module also accept context as an argument, and a context is always a resource.

272

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]