1fromplain.auth.viewsimportAuthView 2fromplain.httpimportResponse,ResponseForbidden,ResponseRedirect 3fromplain.sessions.viewsimportSessionView 4 5from.constantsimportIMPERSONATE_SESSION_KEY 6from.permissionsimportcan_be_impersonator 7from.requestsimportget_request_impersonator 8 910classImpersonateStartView(AuthView):11defget(self)->Response:12# We *could* already be impersonating, so need to consider that13impersonator=get_request_impersonator(self.request)orself.user14ifimpersonatorandcan_be_impersonator(impersonator):15self.session[IMPERSONATE_SESSION_KEY]=self.url_kwargs["id"]16returnResponseRedirect(self.request.query_params.get("next","/"))1718returnResponseForbidden()192021classImpersonateStopView(SessionView):22defget(self)->Response:23self.session.pop(IMPERSONATE_SESSION_KEY)24returnResponseRedirect(self.request.query_params.get("next","/"))