mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 13:00:12 +03:00
Work on DocumentationRenderer
This commit is contained in:
parent
9cb3c8ec35
commit
49a294558d
|
@ -803,10 +803,18 @@ class DocumentationRenderer(BaseRenderer):
|
||||||
|
|
||||||
def get_context(self, data):
|
def get_context(self, data):
|
||||||
from pygments.formatters import HtmlFormatter
|
from pygments.formatters import HtmlFormatter
|
||||||
|
from django.utils.html import mark_safe
|
||||||
formatter = HtmlFormatter(style=self.code_style)
|
formatter = HtmlFormatter(style=self.code_style)
|
||||||
code_style = formatter.get_style_defs('.highlight')
|
code_style = formatter.get_style_defs('.highlight')
|
||||||
langs = ['shell', 'javascript', 'python']
|
langs = ['shell', 'javascript', 'python']
|
||||||
return {'document': data, 'langs': langs, 'code_style': code_style}
|
codec = coreapi.codecs.CoreJSONCodec()
|
||||||
|
schema = mark_safe(codec.encode(data))
|
||||||
|
return {
|
||||||
|
'document': data,
|
||||||
|
'langs': langs,
|
||||||
|
'code_style': code_style,
|
||||||
|
'schema': schema
|
||||||
|
}
|
||||||
|
|
||||||
def render(self, data, accepted_media_type=None, renderer_context=None):
|
def render(self, data, accepted_media_type=None, renderer_context=None):
|
||||||
#from coredocs.main import render as render_docs
|
#from coredocs.main import render as render_docs
|
||||||
|
|
|
@ -38,28 +38,28 @@
|
||||||
<script src="{% static 'rest_framework/docs/js/jquery-1.10.2.min.js' %}"></script>
|
<script src="{% static 'rest_framework/docs/js/jquery-1.10.2.min.js' %}"></script>
|
||||||
<script src="{% static 'rest_framework/docs/js/bootstrap-3.0.3.min.js' %}"></script>
|
<script src="{% static 'rest_framework/docs/js/bootstrap-3.0.3.min.js' %}"></script>
|
||||||
<script>
|
<script>
|
||||||
// function getCookie(name) {
|
function getCookie(name) {
|
||||||
// var cookieValue = null;
|
var cookieValue = null;
|
||||||
// if (document.cookie && document.cookie !== '') {
|
if (document.cookie && document.cookie !== '') {
|
||||||
// var cookies = document.cookie.split(';');
|
var cookies = document.cookie.split(';');
|
||||||
// for (var i = 0; i < cookies.length; i++) {
|
for (var i = 0; i < cookies.length; i++) {
|
||||||
// var cookie = jQuery.trim(cookies[i]);
|
var cookie = jQuery.trim(cookies[i]);
|
||||||
// // Does this cookie string begin with the name we want?
|
// Does this cookie string begin with the name we want?
|
||||||
// if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
if (cookie.substring(0, name.length + 1) === (name + '=')) {
|
||||||
// cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
|
||||||
// break;
|
break;
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// }
|
}
|
||||||
// return cookieValue;
|
return cookieValue;
|
||||||
// }
|
}
|
||||||
// const csrf = {'X-CSRFToken': getCookie('csrftoken')}
|
const csrf = {'X-CSRFToken': getCookie('csrftoken')}
|
||||||
|
|
||||||
const coreapi = window.coreapi
|
const coreapi = window.coreapi
|
||||||
const codec = new coreapi.codecs.CoreJSONCodec()
|
const codec = new coreapi.codecs.CoreJSONCodec()
|
||||||
const schema = '{"_type": "document"}' {{ schema }}
|
const schema = {{ schema }}
|
||||||
const doc = codec.decode(schema, {preloaded: true})
|
const doc = codec.decode(schema, {preloaded: true})
|
||||||
const client = new coreapi.Client()
|
const client = new coreapi.Client(null, null, csrf)
|
||||||
|
|
||||||
$('body').scrollspy({ target: '#toc' })
|
$('body').scrollspy({ target: '#toc' })
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
// Initialize a client & load the schema document
|
// Initialize a client & load the schema document
|
||||||
var client = new coreapi.Client()
|
var client = new coreapi.Client()
|
||||||
var document = null
|
var document = null
|
||||||
client.get("{{ schema_url }}").then(function(result) {
|
client.get("{{ document.url }}").then(function(result) {
|
||||||
document = result
|
document = result
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
|
|
||||||
# Initialize a client & load the schema document
|
# Initialize a client & load the schema document
|
||||||
client = coreapi.Client()
|
client = coreapi.Client()
|
||||||
document = client.get("{{ schema_url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %})
|
document = client.get("{{ document.url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %})
|
||||||
|
|
||||||
# Interact with the API endpoint
|
# Interact with the API endpoint
|
||||||
action = [{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
|
action = [{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{% load rest_framework %}
|
{% load rest_framework %}
|
||||||
<pre class="highlight shell" data-language="shell"><code>{% code bash %}# Load the schema document
|
<pre class="highlight shell" data-language="shell"><code>{% code bash %}# Load the schema document
|
||||||
$ coreapi get {{ schema_url }}{% if schema_format %} --format {{ schema_format }}{% endif %}
|
$ coreapi get {{ document.url }}{% if schema_format %} --format {{ schema_format }}{% endif %}
|
||||||
|
|
||||||
# Interact with the API endpoint
|
# Interact with the API endpoint
|
||||||
$ coreapi action {% if section_key %}{{ section_key }} {% endif %}{{ link_key }}{% for field in link.fields %} -p {{ field.name }}=...{% endfor %}{% endcode %}</code></pre>
|
$ coreapi action {% if section_key %}{{ section_key }} {% endif %}{{ link_key }}{% for field in link.fields %} -p {{ field.name }}=...{% endfor %}{% endcode %}</code></pre>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user