diff options
| author | Tom Christie | 2011-05-23 17:07:31 +0100 |
|---|---|---|
| committer | Tom Christie | 2011-05-23 17:07:31 +0100 |
| commit | c53175914752502629141556f3e001e9d2e9f7fa (patch) | |
| tree | d68bc4f0b2baeed2a90d6d20295423583603ba2b /djangorestframework/utils/description.py | |
| parent | e7f8c06dbbbc9e4ae91327ee02cd8147777b17e2 (diff) | |
| download | django-rest-framework-c53175914752502629141556f3e001e9d2e9f7fa.tar.bz2 | |
name and description
Diffstat (limited to 'djangorestframework/utils/description.py')
| -rw-r--r-- | djangorestframework/utils/description.py | 92 |
1 files changed, 73 insertions, 19 deletions
diff --git a/djangorestframework/utils/description.py b/djangorestframework/utils/description.py index f7145c0f..25bef80b 100644 --- a/djangorestframework/utils/description.py +++ b/djangorestframework/utils/description.py @@ -1,37 +1,91 @@ -"""Get a descriptive name and description for a view, -based on class name and docstring, and override-able by 'name' and 'description' attributes""" +""" +Get a descriptive name and description for a view. +""" import re +from djangorestframework.resources import Resource, FormResource, ModelResource + + +# These a a bit Grungy, but they do the job. def get_name(view): - """Return a name for the view. + """ + Return a name for the view. - If view has a name attribute, use that, otherwise use the view's class name, with 'CamelCaseNames' converted to 'Camel Case Names'.""" - if getattr(view, 'name', None) is not None: - return view.name + If view has a name attribute, use that, otherwise use the view's class name, with 'CamelCaseNames' converted to 'Camel Case Names'. + """ + + # If we're looking up the name of a view callable, as found by reverse, + # grok the class instance that we stored when as_view was called. + if getattr(view, 'cls_instance', None): + view = view.cls_instance - if getattr(view, '__name__', None) is not None: + # If this view has a resource that's been overridden, then use that resource for the name + if getattr(view, 'resource', None) not in (None, Resource, FormResource, ModelResource): + name = view.resource.__name__ + + # Chomp of any non-descriptive trailing part of the resource class name + if name.endswith('Resource') and name != 'Resource': + name = name[:-len('Resource')] + + # If the view has a descriptive suffix, eg '*** List', '*** Instance' + if getattr(view, '_suffix', None): + name += view._suffix + + # Otherwise if it's a function view use the function's name + elif getattr(view, '__name__', None) is not None: name = view.__name__ - elif getattr(view, '__class__', None) is not None: # TODO: should be able to get rid of this case once refactoring to 1.3 class views is complete + + # If it's a view class with no resource then grok the name from the class name + elif getattr(view, '__class__', None) is not None: name = view.__class__.__name__ + + # Chomp of any non-descriptive trailing part of the view class name + if name.endswith('View') and name != 'View': + name = name[:-len('View')] + + # I ain't got nuthin fo' ya else: return '' return re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', name).strip() + + def get_description(view): - """Provide a description for the view. + """ + Provide a description for the view. - By default this is the view's docstring with nice unindention applied.""" - if getattr(view, 'description', None) is not None: - return getattr(view, 'description') + By default this is the view's docstring with nice unindention applied. + """ + + # If we're looking up the name of a view callable, as found by reverse, + # grok the class instance that we stored when as_view was called. + if getattr(view, 'cls_instance', None): + view = view.cls_instance + + + # If this view has a resource that's been overridden, then use the resource's doctring + if getattr(view, 'resource', None) not in (None, Resource, FormResource, ModelResource): + doc = view.resource.__doc__ + + # Otherwise use the view doctring + elif getattr(view, '__doc__', None): + doc = view.__doc__ + + # I ain't got nuthin fo' ya + else: + return '' + + if not doc: + return '' - if getattr(view, '__doc__', None) is not None: - whitespace_counts = [len(line) - len(line.lstrip(' ')) for line in view.__doc__.splitlines()[1:] if line.lstrip()] + whitespace_counts = [len(line) - len(line.lstrip(' ')) for line in doc.splitlines()[1:] if line.lstrip()] - if whitespace_counts: - whitespace_pattern = '^' + (' ' * min(whitespace_counts)) - return re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', view.__doc__) + # unindent the docstring if needed + if whitespace_counts: + whitespace_pattern = '^' + (' ' * min(whitespace_counts)) + return re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', doc) - return view.__doc__ + # otherwise return it as-is + return doc - return ''
\ No newline at end of file |
