<s> """<STR_LIT>""" <EOL> import re <EOL> from importlib import import_module <EOL> from functools import wraps <EOL> from django . conf import settings <EOL> def get_api_endpoints ( ) : <EOL> """<STR_LIT>""" <EOL> urlconf = import_module ( settings . ROOT_URLCONF ) <EOL> urllist = urlconf . urlpatterns <EOL> api_endpoints = { } <EOL> for ( url , callback ) in get_all_urls ( urllist ) : <EOL> if getattr ( callback , '<STR_LIT>' , False ) : <EOL> clean_url = clean_api_regex ( url ) <EOL> api_endpoints [ clean_url ] = callback <EOL> return api_endpoints <EOL> def format_api_docstring ( docstring ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( docstring , basestring ) : <EOL> return "<STR_LIT>" <EOL> whitespace_regex = '<STR_LIT>' <EOL> ret = re . sub ( whitespace_regex , '<STR_LIT:U+0020>' , docstring ) <EOL> ret = ret . strip ( ) <EOL> return ret <EOL> def clean_api_regex ( url ) : <EOL> """<STR_LIT>""" <EOL> url = url . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> url = url . replace ( '<STR_LIT:$>' , '<STR_LIT>' ) <EOL> if not url . startswith ( '<STR_LIT:/>' ) : <EOL> url = '<STR_LIT:/>' + url <EOL> return url <EOL> def get_all_urls ( urllist , prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> for entry in urllist : <EOL> if hasattr ( entry , '<STR_LIT>' ) : <EOL> for url in get_all_urls ( entry . url_patterns , <EOL> prefix + entry . regex . pattern ) : <EOL> yield url <EOL> else : <EOL> yield ( prefix + entry . regex . pattern , entry . callback ) <EOL> endpoints = [ ] <EOL> def api_endpoint ( fn ) : <EOL> """<STR_LIT>""" <EOL> fn . is_api_endpoint = True <EOL> global endpoints <EOL> endpoints . append ( fn ) <EOL> @ wraps ( fn ) <EOL> def _wrapped ( request , * args , ** kwargs ) : <EOL> user = get_api_request_user ( request ) <EOL> if user : <EOL> request . is_api_request = True <EOL> request . user = user <EOL> return fn ( request , * args , ** kwargs ) <EOL> return _wrapped <EOL> def drf_api_endpoint ( fn ) : <EOL> """<STR_LIT>""" <EOL> fn . is_api_endpoint = True <EOL> global endpoints <EOL> endpoints . append ( fn ) <EOL> return fn <EOL> def get_api_request_user ( request ) : <EOL> """<STR_LIT>""" <EOL> if request . is_ajax ( ) : <EOL> return False <EOL> auth_header = request . META . get ( '<STR_LIT>' ) <EOL> if not auth_header : <EOL> return False <EOL> try : <EOL> from seed . landing . models import SEEDUser as User <EOL> username , api_key = auth_header . split ( '<STR_LIT::>' ) <EOL> return User . objects . get ( api_key = api_key , username = username ) <EOL> except ( ValueError , User . DoesNotExist ) : <EOL> return False <EOL> class APIBypassCSRFMiddleware ( object ) : <EOL> """<STR_LIT>""" <EOL> def process_view ( self , request , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if get_api_request_user ( request ) : <EOL> request . csrf_processing_done = True <EOL> return None </s>
