Skip to content

Commit 4d5e846

Browse files
author
Carlton Gibson
authored
Merge pull request #5334 from Woile/woile-nested-docs-fix
Fix docs multiple nested and multiple methods
2 parents 4e08abb + a1546cc commit 4d5e846

File tree

7 files changed

+352
-9
lines changed

7 files changed

+352
-9
lines changed

rest_framework/static/rest_framework/docs/js/api.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ var responseDisplay = 'data'
22
var coreapi = window.coreapi
33
var schema = window.schema
44

5+
function normalizeKeys (arr) {
6+
var _normarr = [];
7+
for (var i = 0; i < arr.length; i++) {
8+
_normarr = _normarr.concat(arr[i].split(' > '));
9+
}
10+
return _normarr;
11+
}
12+
513
function normalizeHTTPHeader (str) {
614
// Capitalize HTTP headers for display.
715
return (str.charAt(0).toUpperCase() + str.substring(1))
@@ -94,7 +102,7 @@ $(function () {
94102
var $requestAwaiting = $form.find('.request-awaiting')
95103
var $responseRaw = $form.find('.response-raw')
96104
var $responseData = $form.find('.response-data')
97-
var key = $form.data('key')
105+
var key = normalizeKeys($form.data('key'))
98106
var params = {}
99107
var entries = formEntries($form.get()[0])
100108

@@ -212,7 +220,6 @@ $(function () {
212220
}
213221

214222
var client = new coreapi.Client(options)
215-
216223
client.action(schema, key, params).then(function (data) {
217224
var response = JSON.stringify(data, null, 2)
218225
$requestAwaiting.addClass('hide')

rest_framework/templates/rest_framework/docs/document.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ <h2 id="{{ section_key }}" class="coredocs-section-title">{{ section_key }} <a h
2020
</a></h2>
2121
{% endif %}
2222

23-
{% for link_key, link in section.links|items %}
23+
{% for link_key, link in section|schema_links|items %}
2424
{% include "rest_framework/docs/link.html" %}
2525
{% endfor %}
2626
{% endfor %}

rest_framework/templates/rest_framework/docs/interact.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% load rest_framework %}
22

33
<!-- Modal -->
4-
<div class="modal fade api-modal" id="{{ section_key }}_{{ link_key }}_modal" tabindex="-1" role="dialog" aria-labelledby="api explorer modal">
4+
<div class="modal fade api-modal" id="{{ section_key }}_{{ link_key|slugify }}_modal" tabindex="-1" role="dialog" aria-labelledby="api explorer modal">
55
<div class="modal-dialog modal-lg" role="document">
66
<div class="modal-content">
77
<div class="modal-header">

rest_framework/templates/rest_framework/docs/link.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
class="btn btn-sm btn-success"
77
style="float: right; margin-top: 20px"
88
data-toggle="modal"
9-
data-target="#{{ section_key }}_{{ link_key }}_modal">
9+
data-target="#{{ section_key }}_{{ link_key|slugify }}_modal">
1010
<i class="fa fa-exchange"></i> Interact
1111
</button>
1212

13-
<h3 id="{{ section_key }}-{{ link_key }}" class="coredocs-link-title">{{ link.title|default:link_key }} <a href="#{{ section_key }}-{{ link_key }}"><i class="fa fa-link" aria-hidden="true"></i>
13+
<h3 id="{{ section_key }}-{{ link_key|slugify }}" class="coredocs-link-title">{{ link.title|default:link_key }} <a href="#{{ section_key }}-{{ link_key|slugify }}"><i class="fa fa-link" aria-hidden="true"></i>
1414
</a></h3>
1515

1616
<div class="meta">

rest_framework/templates/rest_framework/docs/sidebar.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ <h3 class="brand"><a href="#">{{ document.title }}</a></h3>
1010
<li data-toggle="collapse" data-target="#{{ section_key }}-dropdown" class="collapsed">
1111
<a><i class="fa fa-dot-circle-o fa-lg"></i> {% if section_key %}{{ section_key }}{% else %}API Endpoints{% endif %} <span class="arrow"></span></a>
1212
<ul class="sub-menu {% if section_key %}collapse{% endif %}" id="{{ section_key }}-dropdown">
13-
{% for link_key, link in section.links|items %}
14-
<li><a href="#{{ section_key }}-{{ link_key }}">{{ link.title|default:link_key }}</a></li>
13+
{% for link_key, link in section|schema_links|items %}
14+
<li><a href="#{{ section_key }}-{{ link_key|slugify }}">{{ link.title|default:link_key }}</a></li>
1515
{% endfor %}
1616
</ul>
1717
</li>

rest_framework/templatetags/rest_framework.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,29 @@ def items(value):
244244
return value.items()
245245

246246

247+
@register.filter
248+
def schema_links(section, sec_key=None):
249+
"""
250+
Recursively find every link in a schema, even nested.
251+
"""
252+
NESTED_FORMAT = '%s > %s' # this format is used in docs/js/api.js:normalizeKeys
253+
links = section.links
254+
if section.data:
255+
data = section.data.items()
256+
for sub_section_key, sub_section in data:
257+
new_links = schema_links(sub_section, sec_key=sub_section_key)
258+
links.update(new_links)
259+
260+
if sec_key is not None:
261+
new_links = OrderedDict()
262+
for link_key, link in links.items():
263+
new_key = NESTED_FORMAT % (sec_key, link_key)
264+
new_links.update({new_key: link})
265+
return new_links
266+
267+
return links
268+
269+
247270
@register.filter
248271
def add_nested_class(value):
249272
if isinstance(value, dict):

0 commit comments

Comments
 (0)