Subject: [PATCH django-impersonate] optimization | preserve request.user laziness

From: Илья <[email-masked]>
Date: Tue, 22 Dec 2020 12:53:41 +0300

(I've used "reply in thread" link but it created separate topic, sorry)

In our case that means 100k+ requests every day =)

What do you think about this workaround? I've tested it locally.

class ImpersonateMiddleware(MiddlewareMixin):
    def process_request(self, request):
        _usr = request.user  # save as local var to prevent infinite recursion
        def _get_usr():
            _usr.is_impersonate = False
            return _usr

        request.user = SimpleLazyObject(_get_usr)
        request.impersonator = None

        if '_impersonate' in request.session and request.user.is_authenticated:
            ....

---

From: Peter Sanchez <[email-masked]>
Date: Tue, 22 Dec 2020 22:26:42 -0800

>        if '_impersonate' in request.session and request.user.is_authenticated:

This has been applied in changeset eed039f473c6

Thanks!