6
6
7
7
class ApiEndpoint (object ):
8
8
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
10
11
self .pattern = pattern
11
12
self .callback = pattern .callback
12
13
# self.name = pattern.name
@@ -26,7 +27,39 @@ def __get_path__(self, parent_pattern):
26
27
return simplify_regex (self .pattern .regex .pattern )
27
28
28
29
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
30
63
31
64
def __get_docstring__ (self ):
32
65
return inspect .getdoc (self .callback )
0 commit comments