File tree Expand file tree Collapse file tree 4 files changed +31
-9
lines changed Expand file tree Collapse file tree 4 files changed +31
-9
lines changed Original file line number Diff line number Diff line change 11from django .conf import settings
22from django .core .urlresolvers import RegexURLResolver , RegexURLPattern
3+ from drfdocs .api_endpoint import ApiEndpoint
34
45
56class ApiDocumentation (object ):
67 excluded_apps = ["admin" , "drfdocs" ]
78 excluded_endpoints = ["serve" ]
8- root_urlconf = __import__ (settings .ROOT_URLCONF )
99
1010 def __init__ (self ):
11- self .view_names = []
12- self .get_all_view_names (self .root_urlconf .urls .urlpatterns )
11+ self .endpoints = []
12+ root_urlconf = __import__ (settings .ROOT_URLCONF )
13+ self .get_all_view_names (root_urlconf .urls .urlpatterns )
1314
1415 def get_all_view_names (self , urlpatterns ):
1516 for pattern in urlpatterns :
1617 if isinstance (pattern , RegexURLResolver ) and (pattern .app_name not in self .excluded_apps ):
1718 self .get_all_view_names (pattern .url_patterns )
1819 elif isinstance (pattern , RegexURLPattern ) and (pattern .callback .__name__ not in self .excluded_endpoints ):
19- self .view_names .append (pattern .callback .__name__ )
20+ api_endpoint = ApiEndpoint (pattern )
21+ self .endpoints .append (api_endpoint )
2022
21- def get_views (self ):
22- return self .view_names
23+ def get_endpoints (self ):
24+ return self .endpoints
Original file line number Diff line number Diff line change 1+ from django .contrib .admindocs .views import simplify_regex
2+ from rest_framework .views import APIView
3+
4+
5+ class ApiEndpoint (object ):
6+
7+ def __init__ (self , pattern ):
8+ self .pattern = pattern
9+ self .regex = simplify_regex (pattern ._regex )
10+ self .view_name = pattern .callback .__name__
11+ # self._get_api_callback(pattern)
12+
13+ def _get_api_callback (self , pattern ):
14+ """
15+ Verifies that pattern callback is a subclass of APIView, and returns the class
16+ """
17+ if (hasattr (pattern .callback , 'cls' ) and issubclass (pattern .callback .cls , APIView )):
18+ return pattern .callback .cls
Original file line number Diff line number Diff line change 22
33{% block content %}
44 < ul >
5- {% for view in views %}
6- < li > {{ view }}</ li >
5+ {% for endpoint in endpoints %}
6+ < li >
7+ < pre > {{ endpoint.regex }}</ pre >
8+ < p > {{ endpoint.view_name }}</ p >
79 {% endfor %}
810 </ ul >
911{% endblock %}
Original file line number Diff line number Diff line change @@ -9,5 +9,5 @@ class DRFDocsView(TemplateView):
99 def get_context_data (self , ** kwargs ):
1010 context = super (DRFDocsView , self ).get_context_data (** kwargs )
1111 docs = ApiDocumentation ()
12- context ['views ' ] = docs .get_views ()
12+ context ['endpoints ' ] = docs .get_endpoints ()
1313 return context
You can’t perform that action at this time.
0 commit comments