66
77class ApiEndpoint (object ):
88
9- def __init__ (self , pattern , parent_pattern = None ):
9+ def __init__ (self , pattern , parent_pattern = None , drf_router = None ):
10+ self .drf_router = drf_router
1011 self .pattern = pattern
1112 self .callback = pattern .callback
1213 # self.name = pattern.name
@@ -26,7 +27,39 @@ def __get_path__(self, parent_pattern):
2627 return simplify_regex (self .pattern .regex .pattern )
2728
2829 def __get_allowed_methods__ (self ):
29- return [force_str (m ).upper () for m in self .callback .cls .http_method_names if hasattr (self .callback .cls , m )]
30+
31+ viewset_methods = []
32+ if self .drf_router :
33+ for prefix , viewset , basename in self .drf_router .registry :
34+ if self .callback .cls != viewset :
35+ continue
36+
37+ lookup = self .drf_router .get_lookup_regex (viewset )
38+ routes = self .drf_router .get_routes (viewset )
39+
40+ for route in routes :
41+
42+ # Only actions which actually exist on the viewset will be bound
43+ mapping = self .drf_router .get_method_map (viewset , route .mapping )
44+ if not mapping :
45+ continue
46+
47+ # Build the url pattern
48+ regex = route .url .format (
49+ prefix = prefix ,
50+ lookup = lookup ,
51+ trailing_slash = self .drf_router .trailing_slash
52+ )
53+ if self .pattern .regex .pattern == regex :
54+ funcs , viewset_methods = zip (
55+ * [(mapping [m ], m .upper ()) for m in self .callback .cls .http_method_names if m in mapping ]
56+ )
57+ viewset_methods = list (viewset_methods )
58+ if len (set (funcs )) == 1 :
59+ self .docstring = inspect .getdoc (getattr (self .callback .cls , funcs [0 ]))
60+
61+ view_methods = [force_str (m ).upper () for m in self .callback .cls .http_method_names if hasattr (self .callback .cls , m )]
62+ return viewset_methods + view_methods
3063
3164 def __get_docstring__ (self ):
3265 return inspect .getdoc (self .callback )
0 commit comments