2
2
import inspect
3
3
from django .contrib .admindocs .views import simplify_regex
4
4
from django .utils .encoding import force_str
5
+ from rest_framework .viewsets import ModelViewSet
6
+
7
+ VIEWSET_METHODS = {
8
+ 'List' : ['get' , 'post' ],
9
+ 'Instance' : ['get' , 'put' , 'patch' , 'delete' ],
10
+ }
5
11
6
12
7
13
class ApiEndpoint (object ):
@@ -25,8 +31,14 @@ def __get_path__(self, parent_pattern):
25
31
return "/{0}{1}" .format (self .name_parent , simplify_regex (self .pattern .regex .pattern ))
26
32
return simplify_regex (self .pattern .regex .pattern )
27
33
34
+ def is_method_allowed (self , method_name , callback_cls ):
35
+ return hasattr (callback_cls , method_name ) or (
36
+ issubclass (callback_cls , ModelViewSet ) and method_name in VIEWSET_METHODS .get (self .callback .suffix , [])
37
+ )
38
+
28
39
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 )]
40
+ callback_cls = self .callback .cls
41
+ return sorted ([force_str (name ).upper () for name in callback_cls .http_method_names if self .is_method_allowed (name , callback_cls )])
30
42
31
43
def __get_docstring__ (self ):
32
44
return inspect .getdoc (self .callback )
0 commit comments