creating django middleware

142 views
Skip to first unread message

ershadul

unread,
Nov 12, 2008, 7:18:19 AM11/12/08
to Django users
Dear all,
please consider the following code-block:

class SQLAlchemySessionMiddleware(object):
"""
This class instantiates a sqlalchemy session and destroys
"""
def process_request(self, request):
request.db_session = session()
return None

def process_response(self, request, response):
session_destroy(request.db_session)
return response

Consider that session() is method that returns a new object sqlalchemy
session.
I want to inject it before view is processed and destroy it upon
exiting the view.

I have added the path of this middleware class before XView
middleware in settings.py.
But i got a error that
request has not attribute 'db_session' ( process_response function)
How can i do it?

Steve Holden

unread,
Nov 12, 2008, 7:42:44 AM11/12/08
to django...@googlegroups.com
ershadul wrote:
> Dear all,
> please consider the following code-block:
>
> class SQLAlchemySessionMiddleware(object):
> """
> This class instantiates a sqlalchemy session and destroys
> """
> def process_request(self, request):
> request.db_session = session()
> return None
>
>
The last statement in your method is completely redundant, and I'd
suggest you remove it.

How are you verifying that your middleware's "process_request()" method
is being called?
[...]

regards
Steve

Jeff FW

unread,
Nov 12, 2008, 8:57:14 AM11/12/08
to Django users
I also wrote middleware for SQLAlchemy--I'd post it, but it depends on
other libraries that I wrote that I can't really share. What I found
is that, at least while using the dev server, the process_response
method would get called when serving media files, even if the
process_request hadn't. So, simple solution: wrap the session_destroy
call in a try/except block. Here's mine:

def process_response(self, request, response):
try:
request.db_session.commit()
request.db_session.close()
except AttributeError:
pass
return response

Also, may I suggest adding a process_exception method? Here's mine:

def process_exception(self, request, exception):
request.db_session.rollback()
request.db_session.close()

-Jeff

ershadul

unread,
Nov 13, 2008, 1:04:38 AM11/13/08
to Django users
Dear ,
I dont know whether my process_request() is being called or not?
Can you inform me please, how can i verify that my middleware's
process_request() is called ?

Steve Holden

unread,
Nov 13, 2008, 9:18:15 AM11/13/08
to django...@googlegroups.com

Try putting print statements in your code. Running the test server, of
course, so you will see any console output.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

ershadul

unread,
Nov 14, 2008, 3:39:13 AM11/14/08
to Django users
Dear Steve Holden,
Please consider the following block i wrote:

def process_request(self, request):
request.db_session = session()
request.db_session.time_stamp = str(datetime.datetime.now())
print 'process_request', request.db_session.time_stamp

def process_response(self, request, response):
try:
print 'process_response', request.db_session.time_stamp
session_destroy(request.db_session)
except AttributeError:
print 'process_response: db_session not found'
#pass
return response

.... For some links i got the message 'db_session not found' , not for
all request.
that is for some requests process_response is called at first.
Look at the output at console:
These are OK.
Django version 1.0-alpha-SVN-unknown, using settings
'divineba.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
process_request 2008-11-14 14:33:07.437000
process_response 2008-11-14 14:33:07.437000
[14/Nov/2008 14:33:07] "GET /journal/view HTTP/1.1" 200 22616
process_request 2008-11-14 14:33:07.734000
process_response 2008-11-14 14:33:07.734000
[14/Nov/2008 14:33:07] "GET /js/jquery.js HTTP/1.1" 304 0
process_request 2008-11-14 14:33:07.781000
process_response 2008-11-14 14:33:07.781000
[14/Nov/2008 14:33:07] "GET /js/jquery.autocomplete.js HTTP/1.1" 304 0

But for some links we got the following output:

Django version 1.0-alpha-SVN-unknown, using settings
'divineba.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
process_response process_response: db_session not found
[14/Nov/2008 14:36:14] "GET /account/chart HTTP/1.1" 301 0
process_request 2008-11-14 14:36:14.531000
process_response 2008-11-14 14:36:14.531000
[14/Nov/2008 14:36:14] "GET /account/chart/ HTTP/1.1" 200 6717

Look at the 4th line:
"process_response process_response: db_session not found"

Why for some links process_response is called before process_request?
Logically each process_request is called before process_response.
Can you provide any suggestion please ?

On Nov 13, 6:18 am, Steve Holden <holden...@gmail.com> wrote:
> ershadul wrote:
> > Dear ,
> > I dont know whether my process_request() is being called or not?
> > Can you inform me please, how can i verify that mymiddleware's
> > process_request() is called ?
>
> > On Nov 12, 4:42 am, Steve Holden <holden...@gmail.com> wrote:
> >> ershadul wrote:
> >>> Dear all,
> >>> please consider the following code-block:
> >>> class SQLAlchemySessionMiddleware(object):
> >>>     """
> >>>     This class instantiates a sqlalchemy session and destroys
> >>>     """
> >>>     def process_request(self, request):
> >>>         request.db_session = session()
> >>>         return None
> >> The last statement in your method is completely redundant, and I'd
> >> suggest you remove it.
>
> >> How are you verifying that yourmiddleware's"process_request()" method

Malcolm Tredinnick

unread,
Nov 14, 2008, 4:25:57 AM11/14/08
to django...@googlegroups.com

There's no guarantee that the request method is going to be called. If
another middleware earlier in the stack returned a response, the
remainder of the middleware stack (including your process_request()
method) is skipped -- in the request processing path.

However, at that point, the full response processing path, including all
middleware functions, is executed. So if something, say, the cache
middleware intercepted the request before it got to your middleware,
your request handler won't be run. But your response handler will be.
You need to be able to handle that situation (your response handler
needs to check that something like db_session exists before accessing
it).

Regards,
Malcolm

Reply all
Reply to author
Forward
0 new messages