etianen/django-reversion — pull request #256 by pricem (2013-10-09 00:39:24+00:00)
Title: Fix bug in RevisionMiddleware exception handling

Greetings Dave and others,

I was debugging an issue in another project that occurred when using the latest Git version of reversion but not any of the releases. It appears that a commit 99a2871 (which allows proxy caching to be used with reversion) may be introducing a new exception after an exception has been thrown elsewhere, which can confuse debugging. It's possible that this is related to issue #225.

I'm not very experienced with reversion, but I wanted to show a tweak that appeared to fix the problem on our end and see if it made sense. Commit message copied below:

Recently the RevisionMiddleware was modified to avoid accessing
request.user unncessarily for caching purposes. This works well
except in some cases it can obscure errors generated elsewhere in a
project.

The RevisionContextManager has "active" and "inactive" states.
If there is an exception handler elsewhere in the middleware stack
that generates a new HTTP response, the
RevisionMiddleware.process_exception() method will inactivate the
RevisionContextManager via _close_revision(). Then the HTTP response will
be provided to process_response(). If a user is logged in the middleware
will call RevisionContextManager.set_user(), which expects to be in the
active state, and throws an error ("no active revision for this thread").

To fix this, check if the RevisionContextManager is in the active state
before calling set_user.