mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-03-20 18:04:16 +03:00
Improved view/description function setting
Now supports each View having its own name and description function and overriding the global default.
This commit is contained in:
parent
a95984e4d4
commit
e6662d434f
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user