Received: from mail.netlandish.com (mail.netlandish.com [174.136.98.166]) by code.netlandish.com (Postfix) with ESMTP id DC7E98123F for <~petersanchez/public-inbox@lists.code.netlandish.com>; Mon, 21 Dec 2020 16:32:07 +0000 (UTC) Received-SPF: None (mailfrom) identity=mailfrom; client-ip=209.85.167.53; helo=mail-lf1-f53.google.com; envelope-from=mortas.11@gmail.com; receiver= Authentication-Results: mail.netlandish.com; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=uzA3A0iB Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) by mail.netlandish.com (Postfix) with ESMTP id 5D886152FB1 for <~petersanchez/public-inbox@lists.code.netlandish.com>; Mon, 21 Dec 2020 16:32:05 +0000 (UTC) Received: by mail-lf1-f53.google.com with SMTP id a12so25117446lfl.6 for <~petersanchez/public-inbox@lists.code.netlandish.com>; Mon, 21 Dec 2020 08:32:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=8quLI7g4TiVlK8cbE2RPxkD70N9X+wZ1v1YTcIBW6ag=; b=uzA3A0iB/yHaKp6zvKgIy3giVgA7DkAJHX6f7CbJypPukUwnsxm7UWgGTzPFp6t9TD fJTPxMR60DfQpSK8BZHURZzZk7eLJp3QHjSN8QkVVvqPmKeG6HF0Ql6zTPyPkkK06mTc 3Znb2mwZlRRlCH8uERRlmBJm0/Mo1SFopZdyK3YzQxRi/FfGkxHuXVgMapwgh2dXRL5u tCj93eJxp2+uiiRcU1/5e5SmRtmCa2Btgwf3yPW9qjNswMAhh5X4QhnGqulBvjLO1P3W 02XScYxhMzvv8zAqRLK/TWKvjF/ba98Rfu2Kqb5iKtW1OiI6BnqDVTXlbWJ3xxbB1OcD f49A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=8quLI7g4TiVlK8cbE2RPxkD70N9X+wZ1v1YTcIBW6ag=; b=VaK0yEixgpXFqfKF8mFPNm9YMGnb+h1sJJDRcg1Pyg1KK1SFAfNSO0n3HB/OgHKm3O eCMf6eLmzYZlwbHAvP3uFXyANFebbPCkeXX7HUyl3iTZ1PWFb7mZ5Qhf0M4kJBzfm4Al 948yfxJElh1QzlvlpyXFUeSHpZ+4kf6JOYZaiJ5uxIhHCfLyUrImNe0EB1118Wo2GRrD fhYBt5EgF0wj1hxFaG4gcuK7J2q2XMIP+8BEbiLO7pKE5piMkqJvsgSlkH7gQIddGZTL PjMAXBH0CeneWNRAaIMXByIEVzRSJTdcdynuvkiJsTRxA8unb/A3GRT1PBh4SIhiqsSR 8xdg== X-Gm-Message-State: AOAM531E//sbscPjrL5wrQ6eMEYBbviwRNJqRVRXwwaVIvgfFkBXnaCB mh5x7rzclDcnqGo0Zb0RV1hpmG5Y6mkrsw/USzmYlJpSCqI= X-Google-Smtp-Source: ABdhPJyoZScNN0iDpW5oo5ZlG9RqF1L4cH7NmkSo3afZuOvaFpfzN0dfRTKjuWOsn1ErpaSEZQm5051t6KLHwDE5W5w= X-Received: by 2002:a2e:780d:: with SMTP id t13mr7478965ljc.144.1608568324809; Mon, 21 Dec 2020 08:32:04 -0800 (PST) MIME-Version: 1.0 From: =?UTF-8?B?0JjQu9GM0Y8=?= Date: Mon, 21 Dec 2020 19:31:54 +0300 Message-ID: Subject: [PATCH django-impersonate] optimization | preserve request.user laziness To: ~petersanchez/public-inbox@lists.code.netlandish.com Content-Type: text/plain; charset="UTF-8" Hello. By default django request.user is lazy object so when we don't access it - it doesn't hit database. But in ImpersonateMiddleware we have 2 places that "disable" this behaviour: class ImpersonateMiddleware(MiddlewareMixin): def process_request(self, request): request.user.is_impersonate = False # <----- this one request.impersonator = None if is_authenticated(request.user) and \ # <----- and this one '_impersonate' in request.session: new_user_id = request.session['_impersonate'] ..... As a quick-and-dirty fix we can just remove the first line - but in client code we would access this param as `getattr(request.user, 'is_impersonate', False)`. The second one is solved by placing `'_impersonate' in request.session` as a first condition. I think for many people impersonated requests are a very little amount of the whole load so it could help to improve performance. Thanks.