<s> from operator import itemgetter <EOL> from django . core . urlresolvers import NoReverseMatch <EOL> from django . utils . datastructures import SortedDict <EOL> from rest_framework import views <EOL> from rest_framework . response import Response <EOL> from rest_framework . reverse import reverse <EOL> from rest_framework . routers import DefaultRouter <EOL> class SortedDefaultRouter ( DefaultRouter ) : <EOL> def get_api_root_view ( self ) : <EOL> """<STR_LIT>""" <EOL> api_root_dict = { } <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 = SortedDict ( ) <EOL> for key , url_name in sorted ( api_root_dict . items ( ) , key = itemgetter ( <NUM_LIT:0> ) ) : <EOL> try : <EOL> ret [ key ] = reverse ( <EOL> url_name , <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_default_base_name ( self , viewset ) : <EOL> """<STR_LIT>""" <EOL> queryset = getattr ( viewset , '<STR_LIT>' , None ) <EOL> if queryset is not None : <EOL> get_url_name = getattr ( queryset . model , '<STR_LIT>' , None ) <EOL> if get_url_name is not None : <EOL> return get_url_name ( ) <EOL> return super ( SortedDefaultRouter , self ) . get_default_base_name ( viewset ) </s>
