mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-05 04:50:12 +03:00
Nested docs: use lists for paths instead of strings
This way we don't need to do any string manipulation (split_keys is gone), and snippet code generation should be more flexible.
This commit is contained in:
parent
3960328a29
commit
f0e65d2637
|
@ -1,15 +1,16 @@
|
||||||
{% load rest_framework %}
|
{% load rest_framework %}
|
||||||
{% for section_key, section in items %}
|
{% for section_key, section in items %}
|
||||||
{% if section_key %}
|
{% if section_key %}
|
||||||
<h{{level}} id="{{prefix}}{{ section_key }}" class="coredocs-section-title">{{ section_key }} <a href="#{{ prefix }}{{ section_key }}"><i class="fa fa-link" aria-hidden="true"></i>
|
<h{{level}} id="{{ path|list_add:section_key|join:'-' }}" class="coredocs-section-title">
|
||||||
</a></h{{level}}>
|
{{ section_key }}
|
||||||
|
<a href="#{{ path|list_add:section_key|join:'-' }}"><i class="fa fa-link" aria-hidden="true"></i></a>
|
||||||
|
</h{{level}}>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if section.links|items %}
|
{% if section.links|items %}
|
||||||
{% for link_key, link in section.links|items %}
|
{% for link_key, link in section.links|items %}
|
||||||
{% include "rest_framework/docs/link.html" with prefix=prefix level=level|add:1%}
|
{% include "rest_framework/docs/link.html" with path=path %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% include 'rest_framework/docs/_recursive_document.html' with items=section.data|items prefix=prefix|add:section_key|add:'/' level=level|add:1 %}
|
{% include 'rest_framework/docs/_recursive_document.html' with items=section.data|items path=path|list_add:section_key level=level|add:1 %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
|
|
@ -1,22 +1,18 @@
|
||||||
{% load rest_framework %}
|
{% load rest_framework %}
|
||||||
{% if items %}
|
{% if items %}
|
||||||
<ul id="{% if prefix %}{{prefix}}-dropdown{% else %}menu-content{% endif %}" class="menu-content collapse out">
|
<ul id="{% if path %}{{ path|join:"-" }}-dropdown{% else %}menu-content{% endif %}" class="menu-content collapse out">
|
||||||
{% for section_key, section in items %}
|
{% for section_key, section in items %}
|
||||||
<li data-toggle="collapse" data-target="#{{prefix}}{{ section_key }}-dropdown" class="collapsed">
|
<li data-toggle="collapse" data-target="#{{ path|list_add:section_key|join:"-" }}-dropdown" class="collapsed">
|
||||||
<a><i class="fa fa-dot-circle-o fa-lg"></i> {% if prefix %}{{prefix}}/{% endif %}{% if section_key %}{{ section_key }}{% else %}API Endpoints{% endif %} <span class="arrow"></span></a>
|
<a><i class="fa fa-dot-circle-o fa-lg"></i> {% for p in path %}{{ p }}/{% endfor %}{% if section_key %}{{ section_key }}{% else %}API Endpoints{% endif %} <span class="arrow"></span></a>
|
||||||
</li>
|
</li>
|
||||||
{% if section.links|items %}
|
{% if section.links|items %}
|
||||||
<ul class="sub-menu {% if section_key %}collapse{% endif %}" id="{{prefix}}{{ section_key }}-dropdown">
|
<ul class="sub-menu {% if section_key %}collapse{% endif %}" id="{{ path|list_add:section_key|join:'-' }}-dropdown">
|
||||||
{% for link_key, link in section.links|items %}
|
{% for link_key, link in section.links|items %}
|
||||||
<li><a href="#{% if prefix %}{{prefix}}/{% endif %}{{ section_key }}-{{ link_key }}">{{ link.title|default:link_key }}</a></li>
|
<li><a href="#{{ path|list_add:section_key|list_add:link_key|join:"-" }}">{{ link.title|default:link_key }}</a></li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% if prefix %}
|
{% include "rest_framework/docs/_recursive_menu.html" with items=section.data|items path=path|list_add:section_key %}
|
||||||
{% include 'rest_framework/docs/_recursive_menu.html' with items=section.data|items prefix=prefix|add:'/'|add:section_key %}
|
|
||||||
{% else %}
|
|
||||||
{% include 'rest_framework/docs/_recursive_menu.html' with items=section.data|items prefix=section_key %}
|
|
||||||
{% endif %}
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
@ -14,8 +14,8 @@
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{% include 'rest_framework/docs/_recursive_document.html' with items=document.data|items level=2 %}
|
{% include "rest_framework/docs/_recursive_document.html" with items=document.data|items path=""|make_list level=2 %}
|
||||||
|
|
||||||
{% for link_key, link in document.links|items %}
|
{% for link_key, link in document.links|items %}
|
||||||
{% include "rest_framework/docs/link.html" %}
|
{% include "rest_framework/docs/link.html" with path=""|make_list %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<form data-key='[{{ prefix|split_keys }} "{{ section_key }}", "{{ link_key }}"]' class="api-interaction">
|
<form data-key='[{% for p in path %}"{{ p }}", {% endfor %}"{{ section_key }}", "{{ link_key }}"]' class="api-interaction">
|
||||||
<div class="modal-body">
|
<div class="modal-body">
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="col-lg-6 request">
|
<div class="col-lg-6 request">
|
||||||
|
|
|
@ -6,7 +6,7 @@ var schema = window.schema // Loaded by `schema.js`
|
||||||
var client = new coreapi.Client()
|
var client = new coreapi.Client()
|
||||||
|
|
||||||
// Interact with the API endpoint
|
// Interact with the API endpoint
|
||||||
var action = [{{ prefix|split_keys }}{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
|
var action = [{% for p in path %}"{{ p }}", {% endfor %}{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
|
||||||
{% if link.fields %}var params = {
|
{% if link.fields %}var params = {
|
||||||
{% for field in link.fields %} {{ field.name }}: ...{% if not loop.last %},{% endif %}
|
{% for field in link.fields %} {{ field.name }}: ...{% if not loop.last %},{% endif %}
|
||||||
{% endfor %}}
|
{% endfor %}}
|
||||||
|
|
|
@ -6,7 +6,7 @@ client = coreapi.Client()
|
||||||
schema = client.get("{{ document.url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %})
|
schema = client.get("{{ document.url }}"{% if schema_format %}, format="{{ schema_format }}"{% endif %})
|
||||||
|
|
||||||
# Interact with the API endpoint
|
# Interact with the API endpoint
|
||||||
action = [{{ prefix|split_keys }}{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
|
action = [{% for p in path %}"{{ p }}", {% endfor %}{% if section_key %}"{{ section_key }}", {% endif %}"{{ link_key }}"]
|
||||||
{% if link.fields %}params = {
|
{% if link.fields %}params = {
|
||||||
{% for field in link.fields %} "{{ field.name }}": ...{% if not loop.last %},{% endif %}
|
{% for field in link.fields %} "{{ field.name }}": ...{% if not loop.last %},{% endif %}
|
||||||
{% endfor %}}
|
{% endfor %}}
|
||||||
|
|
|
@ -3,4 +3,4 @@
|
||||||
$ coreapi get {{ document.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 {{ prefix|split_keys:'cmd' }}{% if section_key %}{{ section_key }} {% endif %}{{ link_key }}{% for field in link.fields %} -p {{ field.name }}=...{% endfor %}{% endcode %}</code></pre>
|
$ coreapi action {% for p in path %}{{ p }} {% endfor %}{% if section_key %}{{ section_key }} {% endif %}{{ link_key }}{% for field in link.fields %} -p {{ field.name }}=...{% endfor %}{% endcode %}</code></pre>
|
||||||
|
|
|
@ -10,8 +10,10 @@
|
||||||
<i class="fa fa-exchange"></i> Interact
|
<i class="fa fa-exchange"></i> Interact
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<h3 id="{{prefix}}{{ section_key }}-{{ link_key }}" class="coredocs-link-title">{{ link.title|default:link_key }} <a href="#{{prefix}}{{ section_key }}-{{ link_key }}"><i class="fa fa-link" aria-hidden="true"></i>
|
<h3 id="{{ path|list_add:section_key|list_add:link_key|join:"-" }}" class="coredocs-link-title">
|
||||||
</a></h3>
|
{{ link.title|default:link_key }}
|
||||||
|
<a href="#{{ path|list_add:section_key|list_add:link_key|join:"-" }}"><i class="fa fa-link" aria-hidden="true"></i></a>
|
||||||
|
</h3>
|
||||||
|
|
||||||
<div class="meta">
|
<div class="meta">
|
||||||
<span class="label label-primary">{{ link.action|upper }}</span>
|
<span class="label label-primary">{{ link.action|upper }}</span>
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
<i class="fa fa-bars fa-2x toggle-btn" data-toggle="collapse" data-target="#menu-content"></i>
|
<i class="fa fa-bars fa-2x toggle-btn" data-toggle="collapse" data-target="#menu-content"></i>
|
||||||
<div class="menu-list">
|
<div class="menu-list">
|
||||||
{% include 'rest_framework/docs/_recursive_menu.html' with items=document.data|items %}
|
{% include "rest_framework/docs/_recursive_menu.html" with items=document.data|items path=""|make_list %}
|
||||||
|
|
||||||
<ul class="menu-list menu-list-bottom">
|
<ul class="menu-list menu-list-bottom">
|
||||||
<li data-toggle="collapse" data-target="#auth-control" class="collapsed">
|
<li data-toggle="collapse" data-target="#auth-control" class="collapsed">
|
||||||
|
|
|
@ -365,19 +365,14 @@ def break_long_headers(header):
|
||||||
|
|
||||||
|
|
||||||
@register.filter
|
@register.filter
|
||||||
def split_keys(keys, cmd_type='script'):
|
def list_add(lst, item):
|
||||||
if not keys:
|
"""
|
||||||
return ''
|
Appends a item to a list or a tuple. Original value is not modified.
|
||||||
|
|
||||||
if cmd_type == 'script':
|
Can be only applied to lists or tuples, raises TypeError otherwise.
|
||||||
joiner = ', '
|
"""
|
||||||
wrapper = '"'
|
if isinstance(lst, list):
|
||||||
else:
|
return lst + [item]
|
||||||
joiner = ' '
|
elif isinstance(lst, tuple):
|
||||||
wrapper = ''
|
return lst + (item,)
|
||||||
|
raise TypeError("list_add only accepts lists or tuples")
|
||||||
return joiner.join([
|
|
||||||
'{wrapper}{k}{wrapper}'.format(k=k, wrapper=wrapper)
|
|
||||||
if k != '' else ''
|
|
||||||
for k in keys.split('/')
|
|
||||||
])
|
|
||||||
|
|
|
@ -27,16 +27,16 @@ class TestRecursiveUrlViewSets(TestCase):
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_documentation(self):
|
def test_documentation(self):
|
||||||
header_re = 'h{level}\s+id="{path}".*>{title} <a href="#{path}"'
|
header_re = 'h{level}\s+id="{path}".*>\s*{title}\s*<a href="#{path}"'
|
||||||
|
|
||||||
for route in (('not_dummies',), ('dummy', 'aaaas'), ('dummy', 'bbbbs')):
|
for route in (('not_dummies',), ('dummy', 'aaaas'), ('dummy', 'bbbbs')):
|
||||||
path = "/".join(route)
|
path = "-".join(route)
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
re.search(header_re.format(level=1+len(route), path=path, title=route[-1]), self.content),
|
re.search(header_re.format(level=1+len(route), path=path, title=route[-1]), self.content),
|
||||||
'unable to find documentation section for {}'.format(path)
|
'unable to find documentation section for {}'.format(path)
|
||||||
)
|
)
|
||||||
for method in ('read', 'create'):
|
for method in ('read', 'create'):
|
||||||
subpath = "{}/retrieve_alt-{}".format(path, method)
|
subpath = "{}-retrieve_alt-{}".format(path, method)
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
re.search(header_re.format(level=3, path=subpath, title=method), self.content),
|
re.search(header_re.format(level=3, path=subpath, title=method), self.content),
|
||||||
'unable to find documentation section for {}'.format(subpath)
|
'unable to find documentation section for {}'.format(subpath)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user