Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

12. VIEW CONFIGURATION

1

from pyramid.response import Response

2

 

3

def hello_world(request):

4

return Response(’hello!’)

5

 

6

# config is assumed to be an instance of the

7

# pyramid.config.Configurator class

8

config.add_view(hello_world, route_name=’hello’)

 

 

The first argument, view, is required. It must either be a Python object which is the view itself or a dotted Python name to such an object. In the above example, view is the hello_world function. All other arguments are optional. See pyramid.config.Configurator.add_view() for more information.

When you use only add_view() to add view configurations, you don’t need to issue a scan in order for the view configuration to take effect.

12.2 @view_defaults Class Decorator

latex-note.png

This feature is new in Pyramid 1.3.

If you use a class as a view, you can use the pyramid.view.view_defaults class decorator on the class to provide defaults to the view configuration information used by every @view_config decorator that decorates a method of that class.

For instance, if you’ve got a class that has methods that represent “REST actions”, all which are mapped to the same route, but different request methods, instead of this:

1

2

3

4

5

from pyramid.view import view_config from pyramid.response import Response

class RESTView(object):

def __init__(self, request):

146

12.2. @VIEW_DEFAULTS CLASS DECORATOR

6self.request = request

7

8 @view_config(route_name=’rest’, request_method=’GET’)

9def get(self):

10 return Response(’get’)

11

12@view_config(route_name=’rest’, request_method=’POST’)

13def post(self):

14return Response(’post’)

15

16@view_config(route_name=’rest’, request_method=’DELETE’)

17def delete(self):

18return Response(’delete’)

You can do this:

1 from pyramid.view import view_defaults

2 from pyramid.view import view_config

3 from pyramid.response import Response

4

5 @view_defaults(route_name=’rest’)

6 class RESTView(object):

7 def __init__(self, request):

8self.request = request

9

10@view_config(request_method=’GET’)

11def get(self):

12return Response(’get’)

13

14@view_config(request_method=’POST’)

15def post(self):

16return Response(’post’)

17

18@view_config(request_method=’DELETE’)

19def delete(self):

20return Response(’delete’)

In the above example, we were able to take the route_name=’rest’ argument out of the call to each individual @view_config statement, because we used a @view_defaults class decorator to provide the argument as a default to each view method it possessed.

Arguments passed to @view_config will override any default passed to @view_defaults.

The view_defaults class decorator can also provide defaults to the pyramid.config.Configurator.add_view() directive when a decorated class is passed to that directive as its view argument. For example, instead of this:

147

12. VIEW CONFIGURATION

1

from pyramid.response import Response

2

from pyramid.config import Configurator

3

 

4

class RESTView(object):

5

def __init__(self, request):

6self.request = request

7

8def get(self):

9return Response(’get’)

10

11def post(self):

12return Response(’post’)

13

14def delete(self):

15return Response(’delete’)

16

17if __name__ == ’__main__’:

18config = Configurator()

19config.add_route(’rest’, ’/rest’)

20config.add_view(

21RESTView, route_name=’rest’, attr=’get’, request_method=’GET’)

22config.add_view(

23RESTView, route_name=’rest’, attr=’post’, request_method=’POST’)

24config.add_view(

25RESTView, route_name=’rest’, attr=’delete’, request_method=’DELETE’)

To reduce the amount of repetion in the config.add_view statements, we can move the route_name=’rest’ argument to a @view_default class decorator on the RESTView class:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

from pyramid.view import view_config from pyramid.response import Response from pyramid.config import Configurator

@view_defaults(route_name=’rest’) class RESTView(object):

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

def get(self):

return Response(’get’)

def post(self):

return Response(’post’)

def delete(self):

148

12.2. @VIEW_DEFAULTS CLASS DECORATOR

17 return Response(’delete’)

18

19if __name__ == ’__main__’:

20config = Configurator()

21config.add_route(’rest’, ’/rest’)

22config.add_view(RESTView, attr=’get’, request_method=’GET’)

23config.add_view(RESTView, attr=’post’, request_method=’POST’)

24config.add_view(RESTView, attr=’delete’, request_method=’DELETE’)

pyramid.view.view_defaults accepts the same set of arguments that pyramid.view.view_config does, and they have the same meaning. Each argument passed to view_defaults provides a default for the view configurations of methods of the class it’s decorating.

Normal Python inheritance rules apply to defaults added via view_defaults. For example:

1 @view_defaults(route_name=’rest’) 2 class Foo(object):

3pass

4

5 class Bar(Foo):

6pass

The Bar class above will inherit its view defaults from the arguments passed to the view_defaults decorator of the Foo class. To prevent this from happening, use a view_defaults decorator without any arguments on the subclass:

1 @view_defaults(route_name=’rest’) 2 class Foo(object):

3pass

4

5 @view_defaults()

6 class Bar(Foo):

7pass

The view_defaults decorator only works as a class decorator; using it against a function or a method will produce nonsensical results.

12.2.1 Configuring View Security

If an authorization policy is active, any permission attached to a view configuration found during view lookup will be verified. This will ensure that the currently authenticated user possesses that permission

149

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