mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-10 07:14:48 +03:00
Merge branch 'should_include_link' into version-3-5
This commit is contained in:
commit
9a4ed1bf12
|
@ -72,31 +72,10 @@ class SchemaGenerator(object):
|
||||||
|
|
||||||
links = []
|
links = []
|
||||||
for path, method, category, action, callback in self.endpoints:
|
for path, method, category, action, callback in self.endpoints:
|
||||||
view = callback.cls()
|
view = self.setup_view(callback, method, request)
|
||||||
for attr, val in getattr(callback, 'initkwargs', {}).items():
|
if self.should_include_link(path, method, callback, view):
|
||||||
setattr(view, attr, val)
|
link = self.get_link(path, method, callback, view)
|
||||||
view.args = ()
|
links.append((category, action, link))
|
||||||
view.kwargs = {}
|
|
||||||
view.format_kwarg = None
|
|
||||||
|
|
||||||
actions = getattr(callback, 'actions', None)
|
|
||||||
if actions is not None:
|
|
||||||
if method == 'OPTIONS':
|
|
||||||
view.action = 'metadata'
|
|
||||||
else:
|
|
||||||
view.action = actions.get(method.lower())
|
|
||||||
|
|
||||||
if request is not None:
|
|
||||||
view.request = clone_request(request, method)
|
|
||||||
try:
|
|
||||||
view.check_permissions(view.request)
|
|
||||||
except exceptions.APIException:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
view.request = None
|
|
||||||
|
|
||||||
link = self.get_link(path, method, callback, view)
|
|
||||||
links.append((category, action, link))
|
|
||||||
|
|
||||||
if not links:
|
if not links:
|
||||||
return None
|
return None
|
||||||
|
@ -215,8 +194,44 @@ class SchemaGenerator(object):
|
||||||
except IndexError:
|
except IndexError:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def setup_view(self, callback, method, request):
|
||||||
|
"""
|
||||||
|
Setup a view instance.
|
||||||
|
"""
|
||||||
|
view = callback.cls()
|
||||||
|
for attr, val in getattr(callback, 'initkwargs', {}).items():
|
||||||
|
setattr(view, attr, val)
|
||||||
|
view.args = ()
|
||||||
|
view.kwargs = {}
|
||||||
|
view.format_kwarg = None
|
||||||
|
|
||||||
|
actions = getattr(callback, 'actions', None)
|
||||||
|
if actions is not None:
|
||||||
|
if method == 'OPTIONS':
|
||||||
|
view.action = 'metadata'
|
||||||
|
else:
|
||||||
|
view.action = actions.get(method.lower())
|
||||||
|
|
||||||
|
if request is not None:
|
||||||
|
view.request = clone_request(request, method)
|
||||||
|
else:
|
||||||
|
view.request = None
|
||||||
|
|
||||||
|
return view
|
||||||
|
|
||||||
# Methods for generating each individual `Link` instance...
|
# Methods for generating each individual `Link` instance...
|
||||||
|
|
||||||
|
def should_include_link(self, path, method, callback, view):
|
||||||
|
if view.request is None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
try:
|
||||||
|
view.check_permissions(view.request)
|
||||||
|
except exceptions.APIException:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
def get_link(self, path, method, callback, view):
|
def get_link(self, path, method, callback, view):
|
||||||
"""
|
"""
|
||||||
Return a `coreapi.Link` instance for the given endpoint.
|
Return a `coreapi.Link` instance for the given endpoint.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user