<s> import base64 <EOL> from django . http import HttpResponse <EOL> from django . contrib . auth import authenticate , login <EOL> def view_or_basicauth ( view , request , test_func , realm = "<STR_LIT>" , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if test_func ( request . user ) : <EOL> return view ( request , * args , ** kwargs ) <EOL> if '<STR_LIT>' in request . META : <EOL> auth = request . META [ '<STR_LIT>' ] . split ( ) <EOL> if len ( auth ) == <NUM_LIT:2> : <EOL> if auth [ <NUM_LIT:0> ] . lower ( ) == "<STR_LIT>" : <EOL> uname , passwd = base64 . b64decode ( auth [ <NUM_LIT:1> ] ) . split ( '<STR_LIT::>' ) <EOL> user = authenticate ( username = uname , password = passwd ) <EOL> if user is not None : <EOL> if user . is_active : <EOL> login ( request , user ) <EOL> request . user = user <EOL> return view ( request , * args , ** kwargs ) <EOL> response = HttpResponse ( ) <EOL> response . status_code = <NUM_LIT> <EOL> response [ '<STR_LIT>' ] = '<STR_LIT>' % realm <EOL> return response <EOL> def logged_in_or_basicauth ( realm = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> def view_decorator ( func ) : <EOL> def wrapper ( request , * args , ** kwargs ) : <EOL> return view_or_basicauth ( func , request , <EOL> lambda u : u . is_authenticated ( ) , <EOL> realm , * args , ** kwargs ) <EOL> return wrapper <EOL> return view_decorator <EOL> def has_perm_or_basicauth ( perm , realm = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> def view_decorator ( func ) : <EOL> def wrapper ( request , * args , ** kwargs ) : <EOL> return view_or_basicauth ( func , request , <EOL> lambda u : u . has_perm ( perm ) , <EOL> realm , * args , ** kwargs ) <EOL> return wrapper <EOL> return view_decorator </s>
