Sorry, but I can't accept this patch for a few reasons.
1) It will change the default behavior for all existing users. In your patch, it will always redirect to the HTTP_REFERRER if present (which it usually is)
2) The line for "request.session['HTTP_REFERER'] = request.META['HTTP_REFERER']" should do a conditional check or use request.META.get() instead. You leave potential KeyError exceptions open for the edge case that a HTTP_REFERRER header isn't actually sent in the request.
You can already use the setting IMPERSONATE_REDIRECT_URL to specify the redirect URL.
If you want it customized on the fly then I suggest adding support for something like: