Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Commit

Permalink
Make middleware return response immediately after a Demostorage push/…
Browse files Browse the repository at this point in the history
…pop.

- Also VB in preparation for release.
  • Loading branch information
Satchit Haridas committed Mar 7, 2013
1 parent 5929e66 commit 734e749
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
name, version = 'zc.zodbwsgi', '0.2.1'
name, version = 'zc.zodbwsgi', '0.3.0'

install_requires = ['setuptools', 'repoze.retry', 'ZConfig', 'ZODB3']
extras_require = dict(
Expand Down
37 changes: 29 additions & 8 deletions src/zc/zodbwsgi/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ demostorage_manage_header
support for the underlying demostorage.

If a value is provided, it'll check for that header in the request. If found
and its value is "push" or "pop" it'll perform the relevant operation.

Note that the push/pop action is performed _before_ the route inside the
application is invoked.
and its value is "push" or "pop" it'll perform the relevant operation. The
middleware will return a response indicating the action taken _without_
processing the rest of the pipeline.

Also note that this only works if the underlying storage is a DemoStorage.

Expand Down Expand Up @@ -320,6 +319,9 @@ Now, if we run the app, the request won't be retried:
demostorage_manage_header
-------------------------

Providing an value for this options enables hooks that allow one to push/pop
the underlying demostorage.

::

[app:main]
Expand All @@ -346,13 +348,19 @@ demostorage_manage_header
>>> testapp.get('/inc')
<200 OK text/html body="{'x': 1}">

>>> testapp.get('/', {}, headers={'X-FOO': 'push'})
<200 OK text/html body="{'x': 1}">
If the push or pop header is provided, the middleware returns a response
immediately without sending it to the end of the pipeline.

>>> testapp.get('/', {}, headers={'X-FOO': 'push'}).body
'Demostorage pushed\n'

>>> testapp.get('/inc')
<200 OK text/html body="{'x': 2}">

>>> testapp.get('/', {}, {'X-FOO': 'pop'})
>>> testapp.get('/', {}, {'X-FOO': 'pop'}).body
'Demostorage popped\n'

>>> testapp.get('/')
<200 OK text/html body="{'x': 1}">

This also works with multiple dbs.
Expand Down Expand Up @@ -411,10 +419,16 @@ This also works with multiple dbs.
>>> testapp.get('/inc').body
"{'two': {'y': 1}, 'one': {'x': 1}}"

>>> testapp.get('/inc', {}, {'X-FOO': 'push'}).body
>>> testapp.get('/', {}, {'X-FOO': 'push'}).body
'Demostorage pushed\n'

>>> testapp.get('/inc').body
"{'two': {'y': 2}, 'one': {'x': 2}}"

>>> testapp.get('/', {}, {'X-FOO': 'pop'}).body
'Demostorage popped\n'

>>> testapp.get('/').body
"{'two': {'y': 1}, 'one': {'x': 1}}"


Expand Down Expand Up @@ -458,6 +472,13 @@ returned.
Changes
=======

0.3.0 (2012-03-07)
------------------

- Using the demostorage hook now returns a response immediately without
processing the rest of the pipeline. Makes use of this feature less
confusing.

0.2.1 (2012-03-06)
------------------

Expand Down
6 changes: 6 additions & 0 deletions src/zc/zodbwsgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,26 @@ def __call__(self, environ, start_response):
# https://github.com/zopefoundation/zc.zodbwsgi/issues/3

action = environ.get('HTTP_' + self.demostorage_manage_header)
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
if action == 'push':
databases = {}
for name, db in self.database.databases.items():
DB(db.storage.push(),
databases=databases,
database_name=name)
self.database = databases[self.database.database_name]
start_response(status, response_headers)
return ['Demostorage pushed\n']
elif action == 'pop':
databases = {}
for name, db in self.database.databases.items():
DB(db.storage.pop(),
databases=databases,
database_name=name)
self.database = databases[self.database.database_name]
start_response(status, response_headers)
return ['Demostorage popped\n']
if self.transaction_management:
tm = environ[self.transaction_key] = transaction.TransactionManager()
conn = environ[self.key] = self.database.open(tm)
Expand Down

0 comments on commit 734e749

Please sign in to comment.