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