<s> try : <EOL> from urllib . parse import urlencode <EOL> except ImportError : <EOL> from urllib import urlencode <EOL> from django . conf import settings <EOL> from django . contrib import admin <EOL> from django . contrib . admin import AdminSite <EOL> from django . contrib . auth import REDIRECT_FIELD_NAME <EOL> from django . shortcuts import redirect , resolve_url <EOL> from . utils import monkeypatch_method <EOL> from . models import PhoneDevice <EOL> class AdminSiteOTPRequiredMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def has_permission ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if not super ( AdminSiteOTPRequiredMixin , self ) . has_permission ( request ) : <EOL> return False <EOL> return request . user . is_verified ( ) <EOL> def login ( self , request , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> if REDIRECT_FIELD_NAME in request . GET : <EOL> url = request . GET [ REDIRECT_FIELD_NAME ] <EOL> else : <EOL> url = request . get_full_path ( ) <EOL> return redirect ( '<STR_LIT>' % ( <EOL> resolve_url ( settings . LOGIN_URL ) , <EOL> urlencode ( { REDIRECT_FIELD_NAME : url } ) <EOL> ) ) <EOL> class AdminSiteOTPRequired ( AdminSiteOTPRequiredMixin , AdminSite ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def patch_admin ( ) : <EOL> @ monkeypatch_method ( AdminSite ) <EOL> def login ( self , request , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> if REDIRECT_FIELD_NAME in request . GET : <EOL> url = request . GET [ REDIRECT_FIELD_NAME ] <EOL> else : <EOL> url = request . get_full_path ( ) <EOL> return redirect ( '<STR_LIT>' % ( <EOL> resolve_url ( settings . LOGIN_URL ) , <EOL> urlencode ( { REDIRECT_FIELD_NAME : url } ) <EOL> ) ) <EOL> def unpatch_admin ( ) : <EOL> setattr ( AdminSite , '<STR_LIT>' , original_login ) <EOL> original_login = AdminSite . login <EOL> class PhoneDeviceAdmin ( admin . ModelAdmin ) : <EOL> """<STR_LIT>""" <EOL> raw_id_fields = ( '<STR_LIT:user>' , ) <EOL> admin . site . register ( PhoneDevice , PhoneDeviceAdmin ) </s>
