From d07dae6e79d53fdcfc7d88e085fe7e6da97c9c74 Mon Sep 17 00:00:00 2001
From: Christopher Paolini
Date: Thu, 15 Aug 2013 12:41:52 -0400
Subject: Ability to override name/description of view
Added settings and additions to formatting.py
---
rest_framework/utils/formatting.py | 15 +++++++++++++++
1 file changed, 15 insertions(+)
(limited to 'rest_framework/utils/formatting.py')
diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py
index 4bec8387..c908ce66 100644
--- a/rest_framework/utils/formatting.py
+++ b/rest_framework/utils/formatting.py
@@ -7,6 +7,7 @@ from django.utils.html import escape
from django.utils.safestring import mark_safe
from rest_framework.compat import apply_markdown, smart_text
import re
+from rest_framework.settings import api_settings
def _remove_trailing_string(content, trailing):
@@ -48,7 +49,14 @@ def _camelcase_to_spaces(content):
def get_view_name(cls, suffix=None):
"""
Return a formatted name for an `APIView` class or `@api_view` function.
+ If a VIEW_NAME_FUNCTION is set in settings the value of that will be used
+ if that value is "falsy" then the normal formatting will be used.
"""
+ if api_settings.VIEW_NAME_FUNCTION:
+ name = api_settings.VIEW_NAME_FUNCTION(cls, suffix)
+ if name:
+ return name
+
name = cls.__name__
name = _remove_trailing_string(name, 'View')
name = _remove_trailing_string(name, 'ViewSet')
@@ -61,7 +69,14 @@ def get_view_name(cls, suffix=None):
def get_view_description(cls, html=False):
"""
Return a description for an `APIView` class or `@api_view` function.
+ If a VIEW_DESCRIPTION_FUNCTION is set in settings the value of that will be used
+ if that value is "falsy" then the normal formatting will be used.
"""
+ if api_settings.VIEW_DESCRIPTION_FUNCTION:
+ description = api_settings.VIEW_DESCRIPTION_FUNCTION(cls)
+ if description:
+ return markup_description(description)
+
description = cls.__doc__ or ''
description = _remove_leading_indent(smart_text(description))
if html:
--
cgit v1.2.3
From a95984e4d4b034c196d40f74fbdc6345e6d4124c Mon Sep 17 00:00:00 2001
From: Christopher Paolini
Date: Fri, 16 Aug 2013 13:23:04 -0400
Subject: Settings now have default functions
Updated the setting to have a default function.
---
rest_framework/utils/formatting.py | 46 +++++++++++++++++---------------------
1 file changed, 20 insertions(+), 26 deletions(-)
(limited to 'rest_framework/utils/formatting.py')
diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py
index c908ce66..4f59f659 100644
--- a/rest_framework/utils/formatting.py
+++ b/rest_framework/utils/formatting.py
@@ -49,39 +49,15 @@ def _camelcase_to_spaces(content):
def get_view_name(cls, suffix=None):
"""
Return a formatted name for an `APIView` class or `@api_view` function.
- If a VIEW_NAME_FUNCTION is set in settings the value of that will be used
- if that value is "falsy" then the normal formatting will be used.
"""
- if api_settings.VIEW_NAME_FUNCTION:
- name = api_settings.VIEW_NAME_FUNCTION(cls, suffix)
- if name:
- return name
-
- name = cls.__name__
- name = _remove_trailing_string(name, 'View')
- name = _remove_trailing_string(name, 'ViewSet')
- name = _camelcase_to_spaces(name)
- if suffix:
- name += ' ' + suffix
- return name
+ 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.
- If a VIEW_DESCRIPTION_FUNCTION is set in settings the value of that will be used
- if that value is "falsy" then the normal formatting will be used.
"""
- if api_settings.VIEW_DESCRIPTION_FUNCTION:
- description = api_settings.VIEW_DESCRIPTION_FUNCTION(cls)
- if description:
- return markup_description(description)
-
- description = cls.__doc__ or ''
- description = _remove_leading_indent(smart_text(description))
- if html:
- return markup_description(description)
- return description
+ return api_settings.VIEW_DESCRIPTION_FUNCTION(cls)
def markup_description(description):
@@ -93,3 +69,21 @@ def markup_description(description):
else:
description = escape(description).replace('\n', '
')
return mark_safe(description)
+
+
+def view_name(cls, suffix=None):
+ name = cls.__name__
+ name = _remove_trailing_string(name, 'View')
+ name = _remove_trailing_string(name, 'ViewSet')
+ name = _camelcase_to_spaces(name)
+ if suffix:
+ name += ' ' + suffix
+
+ return name
+
+def view_description(cls, html=False):
+ description = cls.__doc__ or ''
+ description = _remove_leading_indent(smart_text(description))
+ if html:
+ return markup_description(description)
+ return description
\ No newline at end of file
--
cgit v1.2.3
From e6662d434f0214d21d38e4388a40fd63e1f9dcc6 Mon Sep 17 00:00:00 2001
From: Christopher Paolini
Date: Sat, 17 Aug 2013 17:44:51 -0400
Subject: Improved view/description function setting
Now supports each View having its own name and description function and
overriding the global default.
---
rest_framework/utils/formatting.py | 15 ---------------
1 file changed, 15 deletions(-)
(limited to 'rest_framework/utils/formatting.py')
diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py
index 4f59f659..5780301a 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.
--
cgit v1.2.3
From 11d7c1838a1146728528d762cdf6bf329321c1d1 Mon Sep 17 00:00:00 2001
From: Christopher Paolini
Date: Sat, 17 Aug 2013 17:52:08 -0400
Subject: Updated default view name/description functions
Forgot to update the default view name/description functions to the new
setup.
---
rest_framework/utils/formatting.py | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
(limited to 'rest_framework/utils/formatting.py')
diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py
index 5780301a..89a89252 100644
--- a/rest_framework/utils/formatting.py
+++ b/rest_framework/utils/formatting.py
@@ -56,8 +56,8 @@ def markup_description(description):
return mark_safe(description)
-def view_name(cls, suffix=None):
- name = cls.__name__
+def view_name(instance, view, suffix=None):
+ name = view.__name__
name = _remove_trailing_string(name, 'View')
name = _remove_trailing_string(name, 'ViewSet')
name = _camelcase_to_spaces(name)
@@ -66,8 +66,8 @@ def view_name(cls, suffix=None):
return name
-def view_description(cls, html=False):
- description = cls.__doc__ or ''
+def view_description(instance, view, html=False):
+ description = view.__doc__ or ''
description = _remove_leading_indent(smart_text(description))
if html:
return markup_description(description)
--
cgit v1.2.3
From 89b0a539c389477cfd7df7df461868b85f618d95 Mon Sep 17 00:00:00 2001
From: Tom Christie
Date: Mon, 19 Aug 2013 08:24:27 +0100
Subject: Move view name/description functions into public space
---
rest_framework/utils/formatting.py | 36 +++++++++++-------------------------
1 file changed, 11 insertions(+), 25 deletions(-)
(limited to 'rest_framework/utils/formatting.py')
diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py
index 89a89252..4b59ba84 100644
--- a/rest_framework/utils/formatting.py
+++ b/rest_framework/utils/formatting.py
@@ -5,12 +5,13 @@ from __future__ import unicode_literals
from django.utils.html import escape
from django.utils.safestring import mark_safe
-from rest_framework.compat import apply_markdown, smart_text
-import re
+from rest_framework.compat import apply_markdown
from rest_framework.settings import api_settings
+from textwrap import dedent
+import re
-def _remove_trailing_string(content, trailing):
+def remove_trailing_string(content, trailing):
"""
Strip trailing component `trailing` from `content` if it exists.
Used when generating names from view classes.
@@ -20,10 +21,14 @@ def _remove_trailing_string(content, trailing):
return content
-def _remove_leading_indent(content):
+def dedent(content):
"""
Remove leading indent from a block of text.
Used when generating descriptions from docstrings.
+
+ Note that python's `textwrap.dedent` doesn't quite cut it,
+ as it fails to dedent multiline docstrings that include
+ unindented text on the initial line.
"""
whitespace_counts = [len(line) - len(line.lstrip(' '))
for line in content.splitlines()[1:] if line.lstrip()]
@@ -32,11 +37,10 @@ def _remove_leading_indent(content):
if whitespace_counts:
whitespace_pattern = '^' + (' ' * min(whitespace_counts))
content = re.sub(re.compile(whitespace_pattern, re.MULTILINE), '', content)
- content = content.strip('\n')
- return content
+ return content.strip()
-def _camelcase_to_spaces(content):
+def camelcase_to_spaces(content):
"""
Translate 'CamelCaseNames' to 'Camel Case Names'.
Used when generating names from view classes.
@@ -54,21 +58,3 @@ def markup_description(description):
else:
description = escape(description).replace('\n', '
')
return mark_safe(description)
-
-
-def view_name(instance, view, suffix=None):
- name = view.__name__
- name = _remove_trailing_string(name, 'View')
- name = _remove_trailing_string(name, 'ViewSet')
- name = _camelcase_to_spaces(name)
- if suffix:
- name += ' ' + suffix
-
- return name
-
-def view_description(instance, view, html=False):
- description = view.__doc__ or ''
- description = _remove_leading_indent(smart_text(description))
- if html:
- return markup_description(description)
- return description
\ No newline at end of file
--
cgit v1.2.3