Skip to content

Commit 225c47b

Browse files
MaxenceEmmanouil Konstantinidis
Maxence
authored and
Emmanouil Konstantinidis
committed
Support Model View Sets
1 parent d0fc0e1 commit 225c47b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

rest_framework_docs/api_endpoint.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
import inspect
33
from django.contrib.admindocs.views import simplify_regex
44
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+
}
511

612

713
class 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

Comments
 (0)