<s> """<STR_LIT>""" <EOL> try : <EOL> from urllib . parse import urlparse , urlunparse <EOL> except ImportError : <EOL> from urlparse import urlparse , urlunparse <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import HttpResponseRedirect , QueryDict <EOL> from django . template . response import TemplateResponse <EOL> try : <EOL> import importlib <EOL> except ImportError : <EOL> from django . utils import importlib <EOL> from django . utils . http import is_safe_url <EOL> from django . views . decorators . debug import sensitive_post_parameters <EOL> from django . views . decorators . cache import never_cache <EOL> from django . views . decorators . csrf import csrf_protect <EOL> from sudo . settings import ( REDIRECT_FIELD_NAME , REDIRECT_URL , SUDO_FORM , <EOL> REDIRECT_TO_FIELD_NAME ) <EOL> from sudo . utils import grant_sudo_privileges <EOL> FormPath , FormClass = '<STR_LIT:.>' . join ( SUDO_FORM . split ( '<STR_LIT:.>' ) [ : - <NUM_LIT:1> ] ) , SUDO_FORM . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] <EOL> SudoForm = getattr ( importlib . import_module ( FormPath ) , FormClass ) <EOL> try : <EOL> from django . shortcuts import resolve_url <EOL> except ImportError : <EOL> from django . core import urlresolvers <EOL> def resolve_url ( to , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( to , '<STR_LIT>' ) : <EOL> return to . get_absolute_url ( ) <EOL> try : <EOL> return urlresolvers . reverse ( to , args = args , kwargs = kwargs ) <EOL> except urlresolvers . NoReverseMatch : <EOL> if callable ( to ) : <EOL> raise <EOL> if '<STR_LIT:/>' not in to and '<STR_LIT:.>' not in to : <EOL> raise <EOL> return to <EOL> @ sensitive_post_parameters ( ) <EOL> @ never_cache <EOL> @ csrf_protect <EOL> @ login_required <EOL> def sudo ( request , template_name = '<STR_LIT>' , extra_context = None ) : <EOL> """<STR_LIT>""" <EOL> redirect_to = request . GET . get ( REDIRECT_FIELD_NAME , REDIRECT_URL ) <EOL> if not is_safe_url ( url = redirect_to , host = request . get_host ( ) ) : <EOL> redirect_to = resolve_url ( REDIRECT_URL ) <EOL> if request . is_sudo ( ) : <EOL> return HttpResponseRedirect ( redirect_to ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> request . session [ REDIRECT_TO_FIELD_NAME ] = redirect_to <EOL> form = SudoForm ( request . user , request . POST or None ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if form . is_valid ( ) : <EOL> grant_sudo_privileges ( request ) <EOL> redirect_to = request . session . pop ( REDIRECT_TO_FIELD_NAME , redirect_to ) <EOL> if not is_safe_url ( url = redirect_to , host = request . get_host ( ) ) : <EOL> redirect_to = resolve_url ( REDIRECT_URL ) <EOL> return HttpResponseRedirect ( redirect_to ) <EOL> context = { <EOL> '<STR_LIT>' : request , <EOL> '<STR_LIT>' : form , <EOL> REDIRECT_FIELD_NAME : redirect_to , <EOL> } <EOL> if extra_context is not None : <EOL> context . update ( extra_context ) <EOL> return TemplateResponse ( request , template_name , context ) <EOL> def redirect_to_sudo ( next_url ) : <EOL> """<STR_LIT>""" <EOL> sudo_url_parts = list ( urlparse ( reverse ( '<STR_LIT>' ) ) ) <EOL> querystring = QueryDict ( sudo_url_parts [ <NUM_LIT:4> ] , mutable = True ) <EOL> querystring [ REDIRECT_FIELD_NAME ] = next_url <EOL> sudo_url_parts [ <NUM_LIT:4> ] = querystring . urlencode ( safe = '<STR_LIT:/>' ) <EOL> return HttpResponseRedirect ( urlunparse ( sudo_url_parts ) ) </s>
