Commit a1421cd4 by Tom Christie

Basic formatting for dict and list values

parent 9a504efd
...@@ -33,6 +33,14 @@ h2, h3 { ...@@ -33,6 +33,14 @@ h2, h3 {
margin-right: 1em; margin-right: 1em;
} }
td.nested {
padding: 0 !important;
}
td.nested > table {
margin: 0;
}
form select, form input, form textarea { form select, form input, form textarea {
width: 90%; width: 90%;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
<tr> <tr>
{% for key, value in row.items %} {% for key, value in row.items %}
{% if key in columns %} {% if key in columns %}
<td> <td {{ value|add_nested_class }} >
{% if key in linked %}<a href="{{ row.url }}">{% endif %} {% if key in linked %}<a href="{{ row.url }}">{% endif %}
{{ value|format_value }} {{ value|format_value }}
{% if key in linked %}</a>{% endif %} {% if key in linked %}</a>{% endif %}
......
{% load rest_framework %}
<table class="table table-striped">
<tbody>
{% for item in value %}
<tr>
<th>{{ forloop.counter0 }}</th>
<td>{{ item|format_value }}</td>
</tr>
{% endfor %}
</tbody>
</table>
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
from django import template from django import template
from django.core.urlresolvers import reverse, NoReverseMatch from django.core.urlresolvers import reverse, NoReverseMatch
from django.template import loader, Context
from django.utils import six from django.utils import six
from django.utils.encoding import iri_to_uri, force_text from django.utils.encoding import iri_to_uri, force_text
from django.utils.html import escape from django.utils.html import escape
...@@ -110,9 +111,13 @@ def format_value(value): ...@@ -110,9 +111,13 @@ def format_value(value):
if isinstance(value, (int, float, decimal.Decimal, bool, type(None))): if isinstance(value, (int, float, decimal.Decimal, bool, type(None))):
return mark_safe('<code>%s</code>' % value) return mark_safe('<code>%s</code>' % value)
elif isinstance(value, list): elif isinstance(value, list):
return '' template = loader.get_template('rest_framework/admin/list_value.html')
context = Context({'value': value})
return template.render(context)
elif isinstance(value, dict): elif isinstance(value, dict):
return '' template = loader.get_template('rest_framework/admin/dict_value.html')
context = Context({'value': value})
return template.render(context)
elif isinstance(value, six.string_types): elif isinstance(value, six.string_types):
if ( if (
(value.startswith('http:') or value.startswith('https:')) and not (value.startswith('http:') or value.startswith('https:')) and not
...@@ -124,6 +129,12 @@ def format_value(value): ...@@ -124,6 +129,12 @@ def format_value(value):
return six.text_type(value) return six.text_type(value)
@register.filter
def add_nested_class(value):
if isinstance(value, (list, dict)):
return 'class=nested'
return ''
# Bunch of stuff cloned from urlize # Bunch of stuff cloned from urlize
TRAILING_PUNCTUATION = ['.', ',', ':', ';', '.)', '"', "']", "'}", "'"] TRAILING_PUNCTUATION = ['.', ',', ':', ';', '.)', '"', "']", "'}", "'"]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment