Commit 9a504efd by Tom Christie

Basic formatting for admin cells

parent 995aa475
...@@ -687,7 +687,7 @@ class AdminRenderer(BrowsableAPIRenderer): ...@@ -687,7 +687,7 @@ class AdminRenderer(BrowsableAPIRenderer):
results = data results = data
if isinstance(results, list): if isinstance(results, list):
header = results[0] header = results[0] if results else {}
style = 'list' style = 'list'
else: else:
header = results header = results
...@@ -695,7 +695,7 @@ class AdminRenderer(BrowsableAPIRenderer): ...@@ -695,7 +695,7 @@ class AdminRenderer(BrowsableAPIRenderer):
columns = [key for key in header.keys() if key != 'url'] columns = [key for key in header.keys() if key != 'url']
details = [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['style'] = style
context['columns'] = columns context['columns'] = columns
......
...@@ -73,11 +73,9 @@ ...@@ -73,11 +73,9 @@
<form id="get-form" class="pull-right"> <form id="get-form" class="pull-right">
<fieldset> <fieldset>
<div class="btn-group format-selection"> <div class="btn-group format-selection">
<a class="btn btn-primary js-tooltip" href='{{ request.get_full_path }}' <a class="btn btn-primary" href='{{ request.get_full_path }}'>Format</a>
rel="nofollow" title="Make a GET request on the {{ name }} resource">Format</a>
<button class="btn btn-primary dropdown-toggle js-tooltip" data-toggle="dropdown" <button class="btn btn-primary dropdown-toggle" data-toggle="dropdown">
title="Specify a format for the GET request">
<span class="caret"></span> <span class="caret"></span>
</button> </button>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
......
{% load rest_framework %}
<table class="table table-striped"> <table class="table table-striped">
<tbody> <tbody>
{% for key, value in results.items %} {% for key, value in results.items %}
{% if key in details %} {% 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 %} {% endif %}
{% endfor %} {% endfor %}
</tbody> </tbody>
......
{% load rest_framework %}
<table class="table table-striped"> <table class="table table-striped">
<thead> <thead>
<tr>{% for column in columns%}<th>{{ column|capfirst }}</th>{% endfor %}</tr> <tr>{% for column in columns%}<th>{{ column|capfirst }}</th>{% endfor %}</tr>
...@@ -9,7 +10,7 @@ ...@@ -9,7 +10,7 @@
{% if key in columns %} {% if key in columns %}
<td> <td>
{% if key in linked %}<a href="{{ row.url }}">{% endif %} {% if key in linked %}<a href="{{ row.url }}">{% endif %}
{{ value }} {{ value|format_value }}
{% if key in linked %}</a>{% endif %} {% if key in linked %}</a>{% endif %}
</td> </td>
{% endif %} {% endif %}
......
...@@ -8,6 +8,7 @@ from django.utils.safestring import SafeData, mark_safe ...@@ -8,6 +8,7 @@ from django.utils.safestring import SafeData, mark_safe
from django.utils.html import smart_urlquote from django.utils.html import smart_urlquote
from rest_framework.renderers import HTMLFormRenderer from rest_framework.renderers import HTMLFormRenderer
from rest_framework.utils.urls import replace_query_param from rest_framework.utils.urls import replace_query_param
import decimal
import re import re
register = template.Library() register = template.Library()
...@@ -104,6 +105,26 @@ def add_class(value, css_class): ...@@ -104,6 +105,26 @@ def add_class(value, css_class):
return value 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 # Bunch of stuff cloned from urlize
TRAILING_PUNCTUATION = ['.', ',', ':', ';', '.)', '"', "']", "'}", "'"] TRAILING_PUNCTUATION = ['.', ',', ':', ';', '.)', '"', "']", "'}", "'"]
WRAPPING_PUNCTUATION = [('(', ')'), ('<', '>'), ('[', ']'), ('&lt;', '&gt;'), WRAPPING_PUNCTUATION = [('(', ')'), ('<', '>'), ('[', ']'), ('&lt;', '&gt;'),
......
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