Basic formatting for admin cells

This commit is contained in:
Tom Christie 2015-05-12 15:21:49 +01:00
parent 995aa47570
commit 9a504efd26
5 changed files with 29 additions and 8 deletions

View File

@ -687,7 +687,7 @@ class AdminRenderer(BrowsableAPIRenderer):
results = data
if isinstance(results, list):
header = results[0]
header = results[0] if results else {}
style = 'list'
else:
header = results
@ -695,7 +695,7 @@ class AdminRenderer(BrowsableAPIRenderer):
columns = [key for key in header.keys() if key != 'url']
details = [key for key in header.keys() if key != 'url']
linked = [columns[0]]
linked = [columns[0]] if columns else []
context['style'] = style
context['columns'] = columns

View File

@ -73,11 +73,9 @@
<form id="get-form" class="pull-right">
<fieldset>
<div class="btn-group format-selection">
<a class="btn btn-primary js-tooltip" href='{{ request.get_full_path }}'
rel="nofollow" title="Make a GET request on the {{ name }} resource">Format</a>
<a class="btn btn-primary" href='{{ request.get_full_path }}'>Format</a>
<button class="btn btn-primary dropdown-toggle js-tooltip" data-toggle="dropdown"
title="Specify a format for the GET request">
<button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
<span class="caret"></span>
</button>
<ul class="dropdown-menu">

View File

@ -1,8 +1,9 @@
{% load rest_framework %}
<table class="table table-striped">
<tbody>
{% for key, value in results.items %}
{% if key in details %}
<tr><th>{{ key|capfirst }}</th><td>{{ value }}</td></tr>
<tr><th>{{ key|capfirst }}</th><td>{{ value|format_value }}</td></tr>
{% endif %}
{% endfor %}
</tbody>

View File

@ -1,3 +1,4 @@
{% load rest_framework %}
<table class="table table-striped">
<thead>
<tr>{% for column in columns%}<th>{{ column|capfirst }}</th>{% endfor %}</tr>
@ -9,7 +10,7 @@
{% if key in columns %}
<td>
{% if key in linked %}<a href="{{ row.url }}">{% endif %}
{{ value }}
{{ value|format_value }}
{% if key in linked %}</a>{% endif %}
</td>
{% endif %}

View File

@ -8,6 +8,7 @@ from django.utils.safestring import SafeData, mark_safe
from django.utils.html import smart_urlquote
from rest_framework.renderers import HTMLFormRenderer
from rest_framework.utils.urls import replace_query_param
import decimal
import re
register = template.Library()
@ -104,6 +105,26 @@ def add_class(value, css_class):
return value
@register.filter
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):
return ''
elif isinstance(value, dict):
return ''
elif isinstance(value, six.string_types):
if (
(value.startswith('http:') or value.startswith('https:')) and not
re.search(r'\s', value)
):
return mark_safe('<a href="{value}">{value}</a>'.format(value=escape(value)))
elif '@' in value and not re.search(r'\s', value):
return mark_safe('<a href="mailto:{value}">{value}</a>'.format(value=escape(value)))
return six.text_type(value)
# Bunch of stuff cloned from urlize
TRAILING_PUNCTUATION = ['.', ',', ':', ';', '.)', '"', "']", "'}", "'"]
WRAPPING_PUNCTUATION = [('(', ')'), ('<', '>'), ('[', ']'), ('&lt;', '&gt;'),