mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-06-08 07:33:23 +03:00
Basic formatting for admin cells
This commit is contained in:
parent
995aa47570
commit
9a504efd26
|
@ -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):
|
||||||
|
|
||||||
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 @@
|
||||||
<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">
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
|
{% 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>
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
{% 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 @@
|
||||||
{% 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
|
||||||
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):
|
||||||
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 = [('(', ')'), ('<', '>'), ('[', ']'), ('<', '>'),
|
WRAPPING_PUNCTUATION = [('(', ')'), ('<', '>'), ('[', ']'), ('<', '>'),
|
||||||
|
|
Loading…
Reference in New Issue
Block a user