<s> from django . contrib . auth import authenticate , login as auth_login <EOL> from django . contrib . auth . decorators import login_required <EOL> from django . core . cache import cache <EOL> from django . http import HttpResponseRedirect , HttpResponse , HttpResponseForbidden <EOL> from urlcrypt . conf import URLCRYPT_LOGIN_URL , URLCRYPT_RATE_LIMIT <EOL> from urlcrypt . lib import decode_login_token <EOL> def rate_limit ( num = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> def decorator ( func ) : <EOL> def wrapper ( request , * args , ** kwargs ) : <EOL> cache_key = '<STR_LIT>' % request . session . _session_key <EOL> added = cache . add ( cache_key , <NUM_LIT:1> , timeout = <NUM_LIT> ) <EOL> if added : <EOL> num_tries = <NUM_LIT:1> <EOL> else : <EOL> num_tries = cache . incr ( cache_key , delta = <NUM_LIT:1> ) <EOL> if num_tries > num : <EOL> raise HttpResponseForbidden ( "<STR_LIT>" ) <EOL> return func ( request , * args , ** kwargs ) <EOL> return wrapper <EOL> return decorator <EOL> @ rate_limit ( num = URLCRYPT_RATE_LIMIT ) <EOL> def login_redirect ( request , token ) : <EOL> try : <EOL> decoded_data = decode_login_token ( token ) <EOL> except Exception , ex : <EOL> return HttpResponseRedirect ( URLCRYPT_LOGIN_URL ) <EOL> if request . user . is_authenticated ( ) and request . user . id == decoded_data [ '<STR_LIT>' ] : <EOL> return HttpResponseRedirect ( decoded_data [ '<STR_LIT:url>' ] ) <EOL> user = authenticate ( decoded_data = decoded_data ) <EOL> if user : <EOL> auth_login ( request , user ) <EOL> return HttpResponseRedirect ( decoded_data [ '<STR_LIT:url>' ] ) <EOL> else : <EOL> return HttpResponseRedirect ( URLCRYPT_LOGIN_URL ) <EOL> @ login_required <EOL> def test_view ( request , username = None ) : <EOL> return HttpResponse ( "<STR_LIT>" ) </s>
