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
90f2e266
Commit
90f2e266
authored
Jun 03, 2015
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Hyperlink and rendering in admin style renderer
parent
c916ad63
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
2 deletions
+31
-2
rest_framework/relations.py
+24
-1
rest_framework/templates/rest_framework/admin/list_value.html
+3
-1
rest_framework/templates/rest_framework/admin/simple_list_value.html
+1
-0
rest_framework/templatetags/rest_framework.py
+3
-0
No files found.
rest_framework/relations.py
View file @
90f2e266
...
...
@@ -13,6 +13,20 @@ from rest_framework.reverse import reverse
from
rest_framework.utils
import
html
class
Hyperlink
(
six
.
text_type
):
"""
A string like object that additionally has an associated name.
We use this for hyperlinked URLs that may render as a named link
in some contexts, or render as a plain URL in others.
"""
def
__new__
(
self
,
url
,
name
):
ret
=
six
.
text_type
.
__new__
(
self
,
url
)
ret
.
name
=
name
return
ret
is_hyperlink
=
True
class
PKOnlyObject
(
object
):
"""
This is a mock object, used for when we only need the pk of the object
...
...
@@ -203,6 +217,9 @@ class HyperlinkedRelatedField(RelatedField):
kwargs
=
{
self
.
lookup_url_kwarg
:
lookup_value
}
return
self
.
reverse
(
view_name
,
kwargs
=
kwargs
,
request
=
request
,
format
=
format
)
def
get_name
(
self
,
obj
):
return
str
(
obj
)
def
to_internal_value
(
self
,
data
):
request
=
self
.
context
.
get
(
'request'
,
None
)
try
:
...
...
@@ -261,7 +278,7 @@ class HyperlinkedRelatedField(RelatedField):
# Return the hyperlink, or error if incorrectly configured.
try
:
return
self
.
get_url
(
value
,
self
.
view_name
,
request
,
format
)
url
=
self
.
get_url
(
value
,
self
.
view_name
,
request
,
format
)
except
NoReverseMatch
:
msg
=
(
'Could not resolve URL for hyperlinked relationship using '
...
...
@@ -271,6 +288,12 @@ class HyperlinkedRelatedField(RelatedField):
)
raise
ImproperlyConfigured
(
msg
%
self
.
view_name
)
if
url
is
None
:
return
None
name
=
self
.
get_name
(
value
)
return
Hyperlink
(
url
,
name
)
class
HyperlinkedIdentityField
(
HyperlinkedRelatedField
):
"""
...
...
rest_framework/templates/rest_framework/admin/list_value.html
View file @
90f2e266
{% load rest_framework %}
<table
class=
"table table-striped"
>
<
!-- <
table class="table table-striped">
<tbody>
{% for item in value %}
<tr>
...
...
@@ -9,3 +9,5 @@
{% endfor %}
</tbody>
</table>
-->
{% for item in value %}{% if not forloop.first%}, {% endif %}
<a
href=
"{{ item }}"
>
{{item.name}}
</a>
{% endfor %}
rest_framework/templates/rest_framework/admin/simple_list_value.html
0 → 100644
View file @
90f2e266
{% for item in value %}{% if not forloop.first%},{% endif %} {% if item.is_hyperlink %}
<a
href=
"{{ item }}"
>
{{item.name}}
</a>
{% else %}{{ item }}{% endif %}{% endfor %}
rest_framework/templatetags/rest_framework.py
View file @
90f2e266
...
...
@@ -111,7 +111,10 @@ def format_value(value):
if
isinstance
(
value
,
(
int
,
float
,
decimal
.
Decimal
,
bool
,
type
(
None
))):
return
mark_safe
(
'<code>
%
s</code>'
%
value
)
elif
isinstance
(
value
,
list
):
if
any
([
isinstance
(
item
,
(
list
,
dict
))
for
item
in
value
]):
template
=
loader
.
get_template
(
'rest_framework/admin/list_value.html'
)
else
:
template
=
loader
.
get_template
(
'rest_framework/admin/simple_list_value.html'
)
context
=
Context
({
'value'
:
value
})
return
template
.
render
(
context
)
elif
isinstance
(
value
,
dict
):
...
...
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