From e6662d434f0214d21d38e4388a40fd63e1f9dcc6 Mon Sep 17 00:00:00 2001 From: Christopher Paolini Date: Sat, 17 Aug 2013 17:44:51 -0400 Subject: [PATCH] Improved view/description function setting Now supports each View having its own name and description function and overriding the global default. --- rest_framework/renderers.py | 5 ++--- rest_framework/utils/breadcrumbs.py | 5 ++--- rest_framework/utils/formatting.py | 15 --------------- rest_framework/views.py | 27 ++++++++++++++++++++------- 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 3a03ca332..1006e26cf 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -24,7 +24,6 @@ from rest_framework.settings import api_settings from rest_framework.request import clone_request from rest_framework.utils import encoders from rest_framework.utils.breadcrumbs import get_breadcrumbs -from rest_framework.utils.formatting import get_view_name, get_view_description from rest_framework import exceptions, parsers, status, VERSION @@ -498,10 +497,10 @@ class BrowsableAPIRenderer(BaseRenderer): return GenericContentForm() def get_name(self, view): - return get_view_name(view.__class__, getattr(view, 'suffix', None)) + return view.get_view_name() def get_description(self, view): - return get_view_description(view.__class__, html=True) + return view.get_view_description(html=True) def get_breadcrumbs(self, request): return get_breadcrumbs(request.path) diff --git a/rest_framework/utils/breadcrumbs.py b/rest_framework/utils/breadcrumbs.py index d51374b0a..0384faba3 100644 --- a/rest_framework/utils/breadcrumbs.py +++ b/rest_framework/utils/breadcrumbs.py @@ -1,6 +1,5 @@ from __future__ import unicode_literals from django.core.urlresolvers import resolve, get_script_prefix -from rest_framework.utils.formatting import get_view_name def get_breadcrumbs(url): @@ -29,8 +28,8 @@ def get_breadcrumbs(url): # Don't list the same view twice in a row. # Probably an optional trailing slash. if not seen or seen[-1] != view: - suffix = getattr(view, 'suffix', None) - name = get_view_name(view.cls, suffix) + instance = view.cls() + name = instance.get_view_name() breadcrumbs_list.insert(0, (name, prefix + url)) seen.append(view) diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py index 4f59f6591..5780301af 100644 --- a/rest_framework/utils/formatting.py +++ b/rest_framework/utils/formatting.py @@ -45,21 +45,6 @@ def _camelcase_to_spaces(content): content = re.sub(camelcase_boundry, ' \\1', content).strip() return ' '.join(content.split('_')).title() - -def get_view_name(cls, suffix=None): - """ - Return a formatted name for an `APIView` class or `@api_view` function. - """ - return api_settings.VIEW_NAME_FUNCTION(cls, suffix) - - -def get_view_description(cls, html=False): - """ - Return a description for an `APIView` class or `@api_view` function. - """ - return api_settings.VIEW_DESCRIPTION_FUNCTION(cls) - - def markup_description(description): """ Apply HTML markup to the given description. diff --git a/rest_framework/views.py b/rest_framework/views.py index d51233a93..4553714a9 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -12,7 +12,6 @@ from rest_framework.compat import View, HttpResponseBase from rest_framework.request import Request from rest_framework.response import Response from rest_framework.settings import api_settings -from rest_framework.utils.formatting import get_view_name, get_view_description class APIView(View): @@ -25,6 +24,9 @@ class APIView(View): permission_classes = api_settings.DEFAULT_PERMISSION_CLASSES content_negotiation_class = api_settings.DEFAULT_CONTENT_NEGOTIATION_CLASS + view_name_function = api_settings.VIEW_NAME_FUNCTION + view_description_function = api_settings.VIEW_DESCRIPTION_FUNCTION + @classmethod def as_view(cls, **initkwargs): """ @@ -157,6 +159,21 @@ class APIView(View): self._negotiator = self.content_negotiation_class() return self._negotiator + def get_view_name(self): + """ + Get the view name + """ + # This is used by ViewSets to disambiguate instance vs list views + view_name_suffix = getattr(self, 'suffix', None) + + return self.view_name_function(self.__class__, view_name_suffix) + + def get_view_description(self, html=False): + """ + Get the view description + """ + return self.view_description_function(self.__class__, html) + # API policy implementation methods def perform_content_negotiation(self, request, force=False): @@ -342,16 +359,12 @@ class APIView(View): Return a dictionary of metadata about the view. Used to return responses for OPTIONS requests. """ - - # This is used by ViewSets to disambiguate instance vs list views - view_name_suffix = getattr(self, 'suffix', None) - # By default we can't provide any form-like information, however the # generic views override this implementation and add additional # information for POST and PUT methods, based on the serializer. ret = SortedDict() - ret['name'] = get_view_name(self.__class__, view_name_suffix) - ret['description'] = get_view_description(self.__class__) + ret['name'] = self.get_view_name() + ret['description'] = self.get_view_description() ret['renders'] = [renderer.media_type for renderer in self.renderer_classes] ret['parses'] = [parser.media_type for parser in self.parser_classes] return ret