<s> import time <EOL> from django . http import HttpResponseNotAllowed , HttpResponseForbidden , HttpResponse , HttpResponseBadRequest <EOL> from django . core . urlresolvers import reverse <EOL> from django . core . cache import cache <EOL> from django import get_version as django_version <EOL> from django . core . mail import send_mail , mail_admins <EOL> from django . conf import settings <EOL> from django . utils . translation import ugettext as _ <EOL> from django . template import loader , TemplateDoesNotExist <EOL> from django . contrib . sites . models import Site <EOL> from decorator import decorator <EOL> from datetime import datetime , timedelta <EOL> __version__ = '<STR_LIT>' <EOL> def get_version ( ) : <EOL> return __version__ <EOL> def format_error ( error ) : <EOL> return u"<STR_LIT>" % ( get_version ( ) , django_version ( ) , error ) <EOL> class rc_factory ( object ) : <EOL> """<STR_LIT>""" <EOL> CODES = dict ( ALL_OK = ( '<STR_LIT:OK>' , <NUM_LIT:200> ) , <EOL> CREATED = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> DELETED = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> BAD_REQUEST = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> FORBIDDEN = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> NOT_FOUND = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> DUPLICATE_ENTRY = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> NOT_HERE = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> INTERNAL_ERROR = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> NOT_IMPLEMENTED = ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> THROTTLED = ( '<STR_LIT>' , <NUM_LIT> ) ) <EOL> def __getattr__ ( self , attr ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> ( r , c ) = self . CODES . get ( attr ) <EOL> except TypeError : <EOL> raise AttributeError ( attr ) <EOL> class HttpResponseWrapper ( HttpResponse ) : <EOL> """<STR_LIT>""" <EOL> def _set_content ( self , content ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( content , basestring ) and hasattr ( content , '<STR_LIT>' ) : <EOL> self . _container = content <EOL> self . _is_string = False <EOL> else : <EOL> self . _container = [ content ] <EOL> self . _is_string = True <EOL> content = property ( HttpResponse . _get_content , _set_content ) <EOL> return HttpResponseWrapper ( r , content_type = '<STR_LIT>' , status = c ) <EOL> rc = rc_factory ( ) <EOL> class FormValidationError ( Exception ) : <EOL> def __init__ ( self , form ) : <EOL> self . form = form <EOL> class HttpStatusCode ( Exception ) : <EOL> def __init__ ( self , response ) : <EOL> self . response = response <EOL> def validate ( v_form , operation = '<STR_LIT:POST>' ) : <EOL> @ decorator <EOL> def wrap ( f , self , request , * a , ** kwa ) : <EOL> form = v_form ( getattr ( request , operation ) ) <EOL> if form . is_valid ( ) : <EOL> setattr ( request , '<STR_LIT>' , form ) <EOL> return f ( self , request , * a , ** kwa ) <EOL> else : <EOL> raise FormValidationError ( form ) <EOL> return wrap <EOL> def throttle ( max_requests , timeout = <NUM_LIT> * <NUM_LIT> , extra = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> @ decorator <EOL> def wrap ( f , self , request , * args , ** kwargs ) : <EOL> if request . user . is_authenticated ( ) : <EOL> ident = request . user . username <EOL> else : <EOL> ident = request . META . get ( '<STR_LIT>' , None ) <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> ident += '<STR_LIT>' % str ( request . throttle_extra ) <EOL> if ident : <EOL> """<STR_LIT>""" <EOL> ident += '<STR_LIT>' % extra <EOL> now = time . time ( ) <EOL> count , expiration = cache . get ( ident , ( <NUM_LIT:1> , None ) ) <EOL> if expiration is None : <EOL> expiration = now + timeout <EOL> if count >= max_requests and expiration > now : <EOL> t = rc . THROTTLED <EOL> wait = int ( expiration - now ) <EOL> t . content = '<STR_LIT>' % wait <EOL> t [ '<STR_LIT>' ] = wait <EOL> return t <EOL> cache . set ( ident , ( count + <NUM_LIT:1> , expiration ) , ( expiration - now ) ) <EOL> return f ( self , request , * args , ** kwargs ) <EOL> return wrap <EOL> def coerce_put_post ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . method == "<STR_LIT>" : <EOL> if hasattr ( request , '<STR_LIT>' ) : <EOL> del request . _post <EOL> del request . _files <EOL> try : <EOL> request . method = "<STR_LIT:POST>" <EOL> request . _load_post_and_files ( ) <EOL> request . method = "<STR_LIT>" <EOL> except AttributeError : <EOL> request . META [ '<STR_LIT>' ] = '<STR_LIT:POST>' <EOL> request . _load_post_and_files ( ) <EOL> request . META [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> request . PUT = request . POST <EOL> class MimerDataException ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class Mimer ( object ) : <EOL> TYPES = dict ( ) <EOL> def __init__ ( self , request ) : <EOL> self . request = request <EOL> def is_multipart ( self ) : <EOL> content_type = self . content_type ( ) <EOL> if content_type is not None : <EOL> return content_type . lstrip ( ) . startswith ( '<STR_LIT>' ) <EOL> return False <EOL> def loader_for_type ( self , ctype ) : <EOL> """<STR_LIT>""" <EOL> for loadee , mimes in Mimer . TYPES . iteritems ( ) : <EOL> for mime in mimes : <EOL> if ctype . startswith ( mime ) : <EOL> return loadee <EOL> def content_type ( self ) : <EOL> """<STR_LIT>""" <EOL> type_formencoded = "<STR_LIT>" <EOL> ctype = self . request . META . get ( '<STR_LIT>' , type_formencoded ) <EOL> if type_formencoded in ctype : <EOL> return None <EOL> return ctype <EOL> def translate ( self ) : <EOL> """<STR_LIT>""" <EOL> ctype = self . content_type ( ) <EOL> self . request . content_type = ctype <EOL> if not self . is_multipart ( ) and ctype : <EOL> loadee = self . loader_for_type ( ctype ) <EOL> if loadee : <EOL> try : <EOL> self . request . data = loadee ( self . request . raw_post_data ) <EOL> self . request . POST = self . request . PUT = dict ( ) <EOL> except ( TypeError , ValueError ) : <EOL> raise MimerDataException <EOL> else : <EOL> self . request . data = None <EOL> return self . request <EOL> @ classmethod <EOL> def register ( cls , loadee , types ) : <EOL> cls . TYPES [ loadee ] = types <EOL> @ classmethod <EOL> def unregister ( cls , loadee ) : <EOL> return cls . TYPES . pop ( loadee ) <EOL> def translate_mime ( request ) : <EOL> request = Mimer ( request ) . translate ( ) <EOL> def require_mime ( * mimes ) : <EOL> """<STR_LIT>""" <EOL> @ decorator <EOL> def wrap ( f , self , request , * args , ** kwargs ) : <EOL> m = Mimer ( request ) <EOL> realmimes = set ( ) <EOL> rewrite = { '<STR_LIT>' : '<STR_LIT:application/json>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> for idx , mime in enumerate ( mimes ) : <EOL> realmimes . add ( rewrite . get ( mime , mime ) ) <EOL> if not m . content_type ( ) in realmimes : <EOL> return rc . BAD_REQUEST <EOL> return f ( self , request , * args , ** kwargs ) <EOL> return wrap <EOL> require_extended = require_mime ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def send_consumer_mail ( consumer ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> subject = settings . PISTON_OAUTH_EMAIL_SUBJECTS [ consumer . status ] <EOL> except AttributeError : <EOL> subject = "<STR_LIT>" % Site . objects . get_current ( ) . name <EOL> if consumer . status == "<STR_LIT>" : <EOL> subject += "<STR_LIT>" <EOL> elif consumer . status == "<STR_LIT>" : <EOL> subject += "<STR_LIT>" <EOL> elif consumer . status == "<STR_LIT>" : <EOL> subject += "<STR_LIT>" <EOL> else : <EOL> subject += "<STR_LIT>" <EOL> template = "<STR_LIT>" % consumer . status <EOL> try : <EOL> body = loader . render_to_string ( template , <EOL> { '<STR_LIT>' : consumer , '<STR_LIT:user>' : consumer . user } ) <EOL> except TemplateDoesNotExist : <EOL> """<STR_LIT>""" <EOL> return <EOL> try : <EOL> sender = settings . PISTON_FROM_EMAIL <EOL> except AttributeError : <EOL> sender = settings . DEFAULT_FROM_EMAIL <EOL> if consumer . user : <EOL> send_mail ( _ ( subject ) , body , sender , [ consumer . user . email ] , fail_silently = True ) <EOL> if consumer . status == '<STR_LIT>' and len ( settings . ADMINS ) : <EOL> mail_admins ( _ ( subject ) , body , fail_silently = True ) <EOL> if settings . DEBUG and consumer . user : <EOL> print "<STR_LIT>" % consumer . user . email <EOL> print "<STR_LIT>" % _ ( subject ) <EOL> print body </s>
