Personal tools
You are here: Home Plone XP leocornus.django.ploneproxy Django Middleware Mechanism

Django Middleware Mechanism

We need get good enough understanding on Django's middleware mechanism!  It will be used to set up the HTTP Request before it pass to the back end Plone site:

  • check the PloneAuthenModel
  • issue the Plone cookie if it is needed!

We could do the same work in mod_python's PythonAuthenHandler!  But it is too late for the first request, since it is after the proxy request has been processed!

We need the Plone cookie to be set before the HTTPD proxy request!

Django middleware should be the answer!

What's the requirement?

We need find a way to issue the Plone site cookie before HTTPD server to process the proxy request, so the first proxy pass request to Plone site will have the cookie set up.  This will make sure that user appears logged in within the same request that mod_python do the authentication!  The current implementation issues the Plone cookie at the end of the mod_python request, which is too late for Plone site to pick up the cookie.

overview picture about the backend and middleware implementation for leocornus.django.ploneproxy

Django middleware implementation

The Plone cookie middleware depends on Django seesios middleware and authentication middleware!  The cookie middleware should hook up the response phase.  In the request phase, we don't know we could authenticate user success or not!  In the Response phase, we could check the session is valid or not.  And based on that, we could set up the proper cookie!

For the response phase middleware, the process order is bottom to up.  So make sure the PloneCookieMiddleware is place before both SessionMiddleware and AuthenticationMiddleware.

Struggling the TypeError: translate() takes exactly one argument (2 given)

This is a tricky error!  I spend more than one hour to figure out the problem!

This TypeError happens when I try to set cookie for the Reponse in a Django middleware.  The code is pretty straitforward: get the cookie name and value from database and using the set_cookie method to set it to Response.  Django is using Python's default Cookie module to manipulate cookies.

>>> cookie = CookeModel.objects.get(pk='123')
>>> cookie.cookie_name
>>> cookie.cookie_value
>>> reponse.set_cookie(cookie.cookie_name, cookie.cookie_value,
... path='/') 

The TypeError is come from the set_cookie.  As I track down the code, I found out that the Cookie.SimpleCookie is used to set the cookie.

>>> c = Cookie.SimpleCookie()
>>> c[cookie.cookie_name] = cookie.cookie_value # this line raise the TypeError!

The unicode type is reason for the TypeError!  Don't know why, but it is related to the SimpleCookie's implementation.

>>> import Cookie
>>> c = Cookie.SimpleCookie()
>>> k = u'abd'
>>> v = u'value'
>>> c[k] = v
Traceback (most recent call last):
  File "", line 1, in ?
  File "/usr/local/rd/python-2.4.6/lib/python2.4/", line 581, in __setitem__
    self.__set(key, rval, cval)
  File "/usr/local/rd/python-2.4.6/lib/python2.4/", line 574, in __set
    M.set(key, real_value, coded_value)
  File "/usr/local/rd/python-2.4.6/lib/python2.4/", line 452, in set
    if "" != translate(key, idmap, LegalChars):
  File "/usr/local/rd/python-2.4.6/lib/python2.4/", line 493, in translate
    return s.translate(table, deletions)
TypeError: translate() takes exactly one argument (2 given)
>>> c[str(k)] = v


Tracking History

When Who What Done
2010-05-18 06:43 Sean Chen introduce leocornus.django.ploneproxy.authen.middleware.PloneCookieMiddleware, check in as revision: r409
-- 3.0 Hours, 100.0% Done
Document Actions