22import inspect
33from django .contrib .admindocs .views import simplify_regex
44from 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+ }
511
612
713class ApiEndpoint (object ):
@@ -25,8 +31,14 @@ def __get_path__(self, parent_pattern):
2531 return "/{0}{1}" .format (self .name_parent , simplify_regex (self .pattern .regex .pattern ))
2632 return simplify_regex (self .pattern .regex .pattern )
2733
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+
2839 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 )])
3042
3143 def __get_docstring__ (self ):
3244 return inspect .getdoc (self .callback )
0 commit comments