skip to navigation
skip to content

dolmen.request 0.3

Grok-like configuration for View components

Downloads ↓

Dolmen.request helps you type the request as it comes in (think add marker interfaces).

This will trigger the selection of right view or adapters in publishing process, so you can choose a different layout (skinning) or a different redering (eg. json vs html)

As the request is typed an event is also triggered.

Changes

0.3 (2012-05-06)

  • Using now the latest changes of the cromlech stack.

0.2 (2012-04-29)

  • Added a configure.zcml to grok the traverser component.

0.1 (2012-04-28)

  • initial release

Usage Overview

Let's import some stuff:

>>> from cromlech.browser import IRequest, IView
>>> from cromlech.browser.testing import TestRequest, TestView
>>> from cromlech.dawnlight import DawnlightPublisher
>>> import grokcore.component as grok
>>> from grokcore.component import subscribe
>>> from zope.interface import implements, Interface
>>> from zope.component import provideAdapter, provideHandler
>>> from zope.component import eventtesting

We set up cromlech.dawnlight and our package:

>>> eventtesting.setUp()
>>> grok.testing.grok("cromlech.dawnlight")
>>> grok.testing.grok("dolmen.request")

We have an application with an index page [1]

>>> class Application(object):
...
...     def test_serve(self, uri):
...           req = TestRequest(path=uri)
...           publisher = DawnlightPublisher()
...           resp = publisher.publish(req, app)
...           assert resp.status_int == 200
...           return resp.body


>>> class IndexView(TestView):
...    def render(self, *args, **kwargs):
...        return "Hi mate !"


>>> provideAdapter(
...     IndexView, (Application, IRequest), IView, name='index')

Dolmen.request helps you have different version of your site based on the request type. You can declare a request type and register it under a name with request_type:

>>> from cromlech.browser.interfaces import ITypedRequest
>>> from dolmen.request.decorators import request_type

>>> @request_type('json')
... class IJSONRequest(ITypedRequest):
...     """REST/JSON request"""

You may equally use the skin directive, for semantic purpose, if the request type is there to change site skin

>>> from dolmen.request.interfaces import ISkin
>>> from dolmen.request.decorators import skin

>>> @skin('corporate')
... class ICorporateSkin(ISkin):
...     """Corporate skin"""

Thanks to that, you can override default behaviour by using the request specific interface for registering publisher consumers, views, viewlets…

For example let's add the json index:

>>> class JSONView(TestView):
...    def render(self, *args,  **kwargs):
...        return "{'msg':'Greatings'}"

>>> provideAdapter(
...     JSONView, (Application, IJSONRequest), IView, 'index')

And an index for the corporate version of our site

>>> class CorporateView(TestView):
...    def render(self, *args,  **kwargs):
...        return "Welcome"

>>> provideAdapter(
...     CorporateView, (Application, ICorporateSkin),
...     IView, 'index')

Dolmen.request provides a request_type traverser, as a simple way of typing the request. So the application works normally:

>>> app = Application()

>>> app.test_serve("/")
'Hi mate !'

But passing through the tarverser you can get a different version:

>>> app.test_serve("/++request_type++json/")
"{'msg':'Greatings'}"

>>> app.test_serve("/++request_type++corporate/")
'Welcome'

Sometime what you need to do some special stuff for certain request type. It was mean for example to inject a css with fanstatic .need or what your application may need.

For example, let's log the corporate access:

>>> from dolmen.request.interfaces import IRequestTypedEvent
>>> def alert_on_customer_access(req, event):
...     print "Alert customer in !"
>>> provideHandler(
...     alert_on_customer_access, (ICorporateSkin, IRequestTypedEvent))

Now if we use our skin (or not):

>>> app.test_serve("/")
'Hi mate !'

>>> app.test_serve("/++request_type++corporate/")
Alert customer in !
'Welcome'
[1]Note that we use provideAdapter instead of context / name directives for sake of simplicity (no need of grokking).
 
File Type Py Version Uploaded on Size # downloads
dolmen.request-0.3.tar.gz (md5) Source 2012-05-07 5KB 0