From 57eae7b315753f072c8436c250e613a892f36352 Mon Sep 17 00:00:00 2001 From: William Sullivan Date: Sat, 15 Jul 2017 05:45:56 -0400 Subject: [PATCH 1/2] Pass the the "title" attribute from each view when constructing Link instances. This is part of a proposed solution to core-api/python-openapi-codec#28 --- rest_framework/schemas.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index 875f9454b3..042d4bb9c9 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -454,7 +454,10 @@ def get_link(self, path, method, view): if self.url and path.startswith('/'): path = path[1:] + title = self.get_title(view) + return coreapi.Link( + title=title, url=urlparse.urljoin(self.url, path), action=method.lower(), encoding=encoding, @@ -462,6 +465,18 @@ def get_link(self, path, method, view): description=description ) + def get_title(self, view): + """ + Determine a title from a view instance. + + If the 'title' attribute is not set on the view, then an empty title is returned. + """ + title = "" + if hasattr(view, "title"): + title = view.title + + return title + def get_description(self, path, method, view): """ Determine a link description. From a223f4d3d72449921ea49688d2d83f90c23842f1 Mon Sep 17 00:00:00 2001 From: William Sullivan Date: Tue, 18 Jul 2017 04:04:36 -0400 Subject: [PATCH 2/2] Simplifying get_title as per PR comment --- rest_framework/schemas.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/rest_framework/schemas.py b/rest_framework/schemas.py index 042d4bb9c9..7106afc414 100644 --- a/rest_framework/schemas.py +++ b/rest_framework/schemas.py @@ -471,11 +471,7 @@ def get_title(self, view): If the 'title' attribute is not set on the view, then an empty title is returned. """ - title = "" - if hasattr(view, "title"): - title = view.title - - return title + return getattr(view, 'title', '') def get_description(self, path, method, view): """