aboutsummaryrefslogtreecommitdiffstats
path: root/djangorestframework/description.py
diff options
context:
space:
mode:
authortom christie tom@tomchristie.com2011-02-19 10:26:27 +0000
committertom christie tom@tomchristie.com2011-02-19 10:26:27 +0000
commit805aa03ec1871f6a766d9052b348ddce9e9843c3 (patch)
tree8ab5b6a7396236aa45bbc61e8404cc77fc75a9c5 /djangorestframework/description.py
parentb749b950a1b4bede76b7e3900a6385779904902d (diff)
downloaddjango-rest-framework-805aa03ec1871f6a766d9052b348ddce9e9843c3.tar.bz2
Yowzers. Final big bunch of refactoring for 0.1 release. Now support Django 1.3's views, admin style api is all polished off, loads of tests, new test project for running the test. All sorts of goodness. Getting ready to push this out now.
Diffstat (limited to 'djangorestframework/description.py')
-rw-r--r--djangorestframework/description.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/djangorestframework/description.py b/djangorestframework/description.py
new file mode 100644
index 00000000..f7145c0f
--- /dev/null
+++ b/djangorestframework/description.py
@@ -0,0 +1,37 @@
+"""Get a descriptive name and description for a view,
+based on class name and docstring, and override-able by 'name' and 'description' attributes"""
+import re
+
+def get_name(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 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
+ name = view.__class__.__name__
+ 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.
+
+ By default this is the view's docstring with nice unindention applied."""
+ if getattr(view, 'description', None) is not None:
+ return getattr(view, 'description')
+
+ 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()]
+
+ if whitespace_counts:
+ whitespace_pattern = '^' + (' ' * min(whitespace_counts))
+ return re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', view.__doc__)
+
+ return view.__doc__
+
+ return '' \ No newline at end of file