<s> """<STR_LIT>""" <EOL> from __future__ import unicode_literals <EOL> import itertools <EOL> from collections import OrderedDict , namedtuple <EOL> from django . conf . urls import url <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . urlresolvers import NoReverseMatch <EOL> from rest_framework import views <EOL> from rest_framework . response import Response <EOL> from rest_framework . reverse import reverse <EOL> from rest_framework . urlpatterns import format_suffix_patterns <EOL> Route = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:url>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> DynamicDetailRoute = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:url>' , '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> DynamicListRoute = namedtuple ( '<STR_LIT>' , [ '<STR_LIT:url>' , '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> def replace_methodname ( format_string , methodname ) : <EOL> """<STR_LIT>""" <EOL> methodnamehyphen = methodname . replace ( '<STR_LIT:_>' , '<STR_LIT:->' ) <EOL> ret = format_string <EOL> ret = ret . replace ( '<STR_LIT>' , methodname ) <EOL> ret = ret . replace ( '<STR_LIT>' , methodnamehyphen ) <EOL> return ret <EOL> def flatten ( list_of_lists ) : <EOL> """<STR_LIT>""" <EOL> return itertools . chain ( * list_of_lists ) <EOL> class BaseRouter ( object ) : <EOL> def __init__ ( self ) : <EOL> self . registry = [ ] <EOL> def register ( self , prefix , viewset , base_name = None ) : <EOL> if base_name is None : <EOL> base_name = self . get_default_base_name ( viewset ) <EOL> self . registry . append ( ( prefix , viewset , base_name ) ) <EOL> def get_default_base_name ( self , viewset ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def get_urls ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> @ property <EOL> def urls ( self ) : <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> self . _urls = self . get_urls ( ) <EOL> return self . _urls <EOL> class SimpleRouter ( BaseRouter ) : <EOL> routes = [ <EOL> Route ( <EOL> url = r'<STR_LIT>' , <EOL> mapping = { <EOL> '<STR_LIT>' : '<STR_LIT:list>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> name = '<STR_LIT>' , <EOL> initkwargs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) , <EOL> DynamicListRoute ( <EOL> url = r'<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> initkwargs = { } <EOL> ) , <EOL> Route ( <EOL> url = r'<STR_LIT>' , <EOL> mapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } , <EOL> name = '<STR_LIT>' , <EOL> initkwargs = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) , <EOL> DynamicDetailRoute ( <EOL> url = r'<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> initkwargs = { } <EOL> ) , <EOL> ] <EOL> def __init__ ( self , trailing_slash = True ) : <EOL> self . trailing_slash = trailing_slash and '<STR_LIT:/>' or '<STR_LIT>' <EOL> super ( SimpleRouter , self ) . __init__ ( ) <EOL> def get_default_base_name ( self , viewset ) : <EOL> """<STR_LIT>""" <EOL> queryset = getattr ( viewset , '<STR_LIT>' , None ) <EOL> assert queryset is not None , '<STR_LIT>' '<STR_LIT>' '<STR_LIT>' <EOL> return queryset . model . _meta . object_name . lower ( ) <EOL> def get_routes ( self , viewset ) : <EOL> """<STR_LIT>""" <EOL> known_actions = flatten ( [ route . mapping . values ( ) for route in self . routes if isinstance ( route , Route ) ] ) <EOL> detail_routes = [ ] <EOL> list_routes = [ ] <EOL> for methodname in dir ( viewset ) : <EOL> attr = getattr ( viewset , methodname ) <EOL> httpmethods = getattr ( attr , '<STR_LIT>' , None ) <EOL> detail = getattr ( attr , '<STR_LIT>' , True ) <EOL> if httpmethods : <EOL> if methodname in known_actions : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % methodname ) <EOL> httpmethods = [ method . lower ( ) for method in httpmethods ] <EOL> if detail : <EOL> detail_routes . append ( ( httpmethods , methodname ) ) <EOL> else : <EOL> list_routes . append ( ( httpmethods , methodname ) ) <EOL> def _get_dynamic_routes ( route , dynamic_routes ) : <EOL> ret = [ ] <EOL> for httpmethods , methodname in dynamic_routes : <EOL> method_kwargs = getattr ( viewset , methodname ) . kwargs <EOL> initkwargs = route . initkwargs . copy ( ) <EOL> initkwargs . update ( method_kwargs ) <EOL> url_path = initkwargs . pop ( "<STR_LIT>" , None ) or methodname <EOL> ret . append ( Route ( <EOL> url = replace_methodname ( route . url , url_path ) , <EOL> mapping = { httpmethod : methodname for httpmethod in httpmethods } , <EOL> name = replace_methodname ( route . name , url_path ) , <EOL> initkwargs = initkwargs , <EOL> ) ) <EOL> return ret <EOL> ret = [ ] <EOL> for route in self . routes : <EOL> if isinstance ( route , DynamicDetailRoute ) : <EOL> ret += _get_dynamic_routes ( route , detail_routes ) <EOL> elif isinstance ( route , DynamicListRoute ) : <EOL> ret += _get_dynamic_routes ( route , list_routes ) <EOL> else : <EOL> ret . append ( route ) <EOL> return ret <EOL> def get_method_map ( self , viewset , method_map ) : <EOL> """<STR_LIT>""" <EOL> bound_methods = { } <EOL> for method , action in method_map . items ( ) : <EOL> if hasattr ( viewset , action ) : <EOL> bound_methods [ method ] = action <EOL> return bound_methods <EOL> def get_lookup_regex ( self , viewset , lookup_prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> base_regex = '<STR_LIT>' <EOL> lookup_field = getattr ( viewset , '<STR_LIT>' , '<STR_LIT>' ) <EOL> lookup_url_kwarg = getattr ( viewset , '<STR_LIT>' , None ) or lookup_field <EOL> lookup_value = getattr ( viewset , '<STR_LIT>' , '<STR_LIT>' ) <EOL> return base_regex . format ( <EOL> lookup_prefix = lookup_prefix , <EOL> lookup_url_kwarg = lookup_url_kwarg , <EOL> lookup_value = lookup_value <EOL> ) <EOL> def get_urls ( self ) : <EOL> """<STR_LIT>""" <EOL> ret = [ ] <EOL> for prefix , viewset , basename in self . registry : <EOL> lookup = self . get_lookup_regex ( viewset ) <EOL> routes = self . get_routes ( viewset ) <EOL> for route in routes : <EOL> mapping = self . get_method_map ( viewset , route . mapping ) <EOL> if not mapping : <EOL> continue <EOL> regex = route . url . format ( <EOL> prefix = prefix , <EOL> lookup = lookup , <EOL> trailing_slash = self . trailing_slash <EOL> ) <EOL> view = viewset . as_view ( mapping , ** route . initkwargs ) <EOL> name = route . name . format ( basename = basename ) <EOL> ret . append ( url ( regex , view , name = name ) ) <EOL> return ret <EOL> class DefaultRouter ( SimpleRouter ) : <EOL> """<STR_LIT>""" <EOL> include_root_view = True <EOL> include_format_suffixes = True <EOL> root_view_name = '<STR_LIT>' <EOL> def get_api_root_view ( self ) : <EOL> """<STR_LIT>""" <EOL> api_root_dict = OrderedDict ( ) <EOL> list_name = self . routes [ <NUM_LIT:0> ] . name <EOL> for prefix , viewset , basename in self . registry : <EOL> api_root_dict [ prefix ] = list_name . format ( basename = basename ) <EOL> class APIRoot ( views . APIView ) : <EOL> _ignore_model_permissions = True <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> ret = OrderedDict ( ) <EOL> namespace = request . resolver_match . namespace <EOL> for key , url_name in api_root_dict . items ( ) : <EOL> if namespace : <EOL> url_name = namespace + '<STR_LIT::>' + url_name <EOL> try : <EOL> ret [ key ] = reverse ( <EOL> url_name , <EOL> args = args , <EOL> kwargs = kwargs , <EOL> request = request , <EOL> format = kwargs . get ( '<STR_LIT>' , None ) <EOL> ) <EOL> except NoReverseMatch : <EOL> continue <EOL> return Response ( ret ) <EOL> return APIRoot . as_view ( ) <EOL> def get_urls ( self ) : <EOL> """<STR_LIT>""" <EOL> urls = [ ] <EOL> if self . include_root_view : <EOL> root_url = url ( r'<STR_LIT>' , self . get_api_root_view ( ) , name = self . root_view_name ) <EOL> urls . append ( root_url ) <EOL> default_urls = super ( DefaultRouter , self ) . get_urls ( ) <EOL> urls . extend ( default_urls ) <EOL> if self . include_format_suffixes : <EOL> urls = format_suffix_patterns ( urls ) <EOL> return urls </s>
