|
21 | 21 | from rest_framework.utils import formatting
|
22 | 22 |
|
23 | 23 |
|
24 |
| -def get_view_name(view_cls, suffix=None): |
| 24 | +def get_view_name(view): |
25 | 25 | """
|
26 | 26 | Given a view class, return a textual name to represent the view.
|
27 | 27 | This name is used in the browsable API, and in OPTIONS responses.
|
28 | 28 |
|
29 | 29 | This function is the default for the `VIEW_NAME_FUNCTION` setting.
|
30 | 30 | """
|
31 |
| - name = view_cls.__name__ |
| 31 | + if view.name is not None: |
| 32 | + return view.name |
| 33 | + |
| 34 | + name = view.__class__.__name__ |
32 | 35 | name = formatting.remove_trailing_string(name, 'View')
|
33 | 36 | name = formatting.remove_trailing_string(name, 'ViewSet')
|
34 | 37 | name = formatting.camelcase_to_spaces(name)
|
| 38 | + |
| 39 | + # Suffix may be set by some Views, such as a ViewSet. |
| 40 | + suffix = getattr(view, 'suffix', None) |
35 | 41 | if suffix:
|
36 | 42 | name += ' ' + suffix
|
37 | 43 |
|
38 | 44 | return name
|
39 | 45 |
|
40 | 46 |
|
41 |
| -def get_view_description(view_cls, html=False): |
| 47 | +def get_view_description(view, html=False): |
42 | 48 | """
|
43 | 49 | Given a view class, return a textual description to represent the view.
|
44 | 50 | This name is used in the browsable API, and in OPTIONS responses.
|
45 | 51 |
|
46 | 52 | This function is the default for the `VIEW_DESCRIPTION_FUNCTION` setting.
|
47 | 53 | """
|
48 |
| - description = view_cls.__doc__ or '' |
| 54 | + if view.description is not None: |
| 55 | + description = view.description |
| 56 | + else: |
| 57 | + description = view.__class__.__doc__ or '' |
| 58 | + |
49 | 59 | description = formatting.dedent(smart_text(description))
|
50 | 60 | if html:
|
51 | 61 | return formatting.markup_description(description)
|
@@ -93,6 +103,10 @@ def exception_handler(exc, context):
|
93 | 103 |
|
94 | 104 | class APIView(View):
|
95 | 105 |
|
| 106 | + # Allow the view name and description to be set by the initkwargs. |
| 107 | + name = None |
| 108 | + description = None |
| 109 | + |
96 | 110 | # The following policies may be set at either globally, or per-view.
|
97 | 111 | renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
|
98 | 112 | parser_classes = api_settings.DEFAULT_PARSER_CLASSES
|
@@ -224,15 +238,15 @@ def get_view_name(self):
|
224 | 238 | browsable API.
|
225 | 239 | """
|
226 | 240 | func = self.settings.VIEW_NAME_FUNCTION
|
227 |
| - return func(self.__class__, getattr(self, 'suffix', None)) |
| 241 | + return func(self) |
228 | 242 |
|
229 | 243 | def get_view_description(self, html=False):
|
230 | 244 | """
|
231 | 245 | Return some descriptive text for the view, as used in OPTIONS responses
|
232 | 246 | and in the browsable API.
|
233 | 247 | """
|
234 | 248 | func = self.settings.VIEW_DESCRIPTION_FUNCTION
|
235 |
| - return func(self.__class__, html) |
| 249 | + return func(self, html) |
236 | 250 |
|
237 | 251 | # API policy instantiation methods
|
238 | 252 |
|
|
0 commit comments