Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
89b0a539
Commit
89b0a539
authored
Aug 19, 2013
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move view name/description functions into public space
parent
a8aabe23
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
33 additions
and
29 deletions
+33
-29
rest_framework/settings.py
+2
-2
rest_framework/utils/formatting.py
+11
-26
rest_framework/views.py
+20
-1
No files found.
rest_framework/settings.py
View file @
89b0a539
...
@@ -70,8 +70,8 @@ DEFAULTS = {
...
@@ -70,8 +70,8 @@ DEFAULTS = {
'PAGINATE_BY_PARAM'
:
None
,
'PAGINATE_BY_PARAM'
:
None
,
# View configuration
# View configuration
'VIEW_NAME_FUNCTION'
:
'rest_framework.
utils.formatting.
view_name'
,
'VIEW_NAME_FUNCTION'
:
'rest_framework.
views.get_
view_name'
,
'VIEW_DESCRIPTION_FUNCTION'
:
'rest_framework.
utils.formatting.
view_description'
,
'VIEW_DESCRIPTION_FUNCTION'
:
'rest_framework.
views.get_
view_description'
,
# Authentication
# Authentication
'UNAUTHENTICATED_USER'
:
'django.contrib.auth.models.AnonymousUser'
,
'UNAUTHENTICATED_USER'
:
'django.contrib.auth.models.AnonymousUser'
,
...
...
rest_framework/utils/formatting.py
View file @
89b0a539
...
@@ -5,12 +5,13 @@ from __future__ import unicode_literals
...
@@ -5,12 +5,13 @@ from __future__ import unicode_literals
from
django.utils.html
import
escape
from
django.utils.html
import
escape
from
django.utils.safestring
import
mark_safe
from
django.utils.safestring
import
mark_safe
from
rest_framework.compat
import
apply_markdown
,
smart_text
from
rest_framework.compat
import
apply_markdown
import
re
from
rest_framework.settings
import
api_settings
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.
Strip trailing component `trailing` from `content` if it exists.
Used when generating names from view classes.
Used when generating names from view classes.
...
@@ -20,10 +21,14 @@ def _remove_trailing_string(content, trailing):
...
@@ -20,10 +21,14 @@ def _remove_trailing_string(content, trailing):
return
content
return
content
def
_remove_leading_in
dent
(
content
):
def
de
dent
(
content
):
"""
"""
Remove leading indent from a block of text.
Remove leading indent from a block of text.
Used when generating descriptions from docstrings.
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
(
' '
))
whitespace_counts
=
[
len
(
line
)
-
len
(
line
.
lstrip
(
' '
))
for
line
in
content
.
splitlines
()[
1
:]
if
line
.
lstrip
()]
for
line
in
content
.
splitlines
()[
1
:]
if
line
.
lstrip
()]
...
@@ -32,11 +37,10 @@ def _remove_leading_indent(content):
...
@@ -32,11 +37,10 @@ def _remove_leading_indent(content):
if
whitespace_counts
:
if
whitespace_counts
:
whitespace_pattern
=
'^'
+
(
' '
*
min
(
whitespace_counts
))
whitespace_pattern
=
'^'
+
(
' '
*
min
(
whitespace_counts
))
content
=
re
.
sub
(
re
.
compile
(
whitespace_pattern
,
re
.
MULTILINE
),
''
,
content
)
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'.
Translate 'CamelCaseNames' to 'Camel Case Names'.
Used when generating names from view classes.
Used when generating names from view classes.
...
@@ -54,21 +58,3 @@ def markup_description(description):
...
@@ -54,21 +58,3 @@ def markup_description(description):
else
:
else
:
description
=
escape
(
description
)
.
replace
(
'
\n
'
,
'<br />'
)
description
=
escape
(
description
)
.
replace
(
'
\n
'
,
'<br />'
)
return
mark_safe
(
description
)
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
rest_framework/views.py
View file @
89b0a539
...
@@ -8,10 +8,29 @@ from django.http import Http404
...
@@ -8,10 +8,29 @@ from django.http import Http404
from
django.utils.datastructures
import
SortedDict
from
django.utils.datastructures
import
SortedDict
from
django.views.decorators.csrf
import
csrf_exempt
from
django.views.decorators.csrf
import
csrf_exempt
from
rest_framework
import
status
,
exceptions
from
rest_framework
import
status
,
exceptions
from
rest_framework.compat
import
View
,
HttpResponseBase
from
rest_framework.compat
import
smart_text
,
HttpResponseBase
,
View
from
rest_framework.request
import
Request
from
rest_framework.request
import
Request
from
rest_framework.response
import
Response
from
rest_framework.response
import
Response
from
rest_framework.settings
import
api_settings
from
rest_framework.settings
import
api_settings
from
rest_framework.utils
import
formatting
def
get_view_name
(
instance
,
view
,
suffix
=
None
):
name
=
view
.
__name__
name
=
formatting
.
remove_trailing_string
(
name
,
'View'
)
name
=
formatting
.
remove_trailing_string
(
name
,
'ViewSet'
)
name
=
formatting
.
camelcase_to_spaces
(
name
)
if
suffix
:
name
+=
' '
+
suffix
return
name
def
get_view_description
(
instance
,
view
,
html
=
False
):
description
=
view
.
__doc__
or
''
description
=
formatting
.
dedent
(
smart_text
(
description
))
if
html
:
return
formatting
.
markup_description
(
description
)
return
description
class
APIView
(
View
):
class
APIView
(
View
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment