Docs: always consider section sub-documents

If the section has both links and sub-documents (nested routes),
then both should be displayed.
This commit is contained in:
Aleksey Zhukov 2017-05-23 16:15:45 +03:00
parent ba091db98c
commit dca59af6f0
No known key found for this signature in database
GPG Key ID: E1C3A2857EFC7C66
4 changed files with 27 additions and 16 deletions

View File

@ -1,6 +1,7 @@
{% load rest_framework %}
{% for section_key, section in items %}
{% if section_key %}
<h{{level}} id="{{prefix}}{{ section_key }}" class="coredocs-section-title">{{ section_key }} <a href="#{{ section_key }}"><i class="fa fa-link" aria-hidden="true"></i>
<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>
</a></h{{level}}>
{% endif %}
@ -8,8 +9,7 @@
{% for link_key, link in section.links|items %}
{% include "rest_framework/docs/link.html" with prefix=prefix level=level|add:1%}
{% endfor %}
{% else %}
{% include 'rest_framework/docs/_recursive_document.html' with items=section|items prefix=prefix|add:section_key|add:'/' level=level|add:1 %}
{% endif %}
{% include 'rest_framework/docs/_recursive_document.html' with items=section.data|items prefix=prefix|add:section_key|add:'/' level=level|add:1 %}
{% endfor %}

View File

@ -1,3 +1,5 @@
{% load rest_framework %}
{% if items %}
<ul id="{% if prefix %}{{prefix}}-dropdown{% else %}menu-content{% endif %}" class="menu-content collapse out">
{% for section_key, section in items %}
<li data-toggle="collapse" data-target="#{{prefix}}{{ section_key }}-dropdown" class="collapsed">
@ -9,13 +11,12 @@
<li><a href="#{% if prefix %}{{prefix}}/{% endif %}{{ section_key }}-{{ link_key }}">{{ link.title|default:link_key }}</a></li>
{% endfor %}
</ul>
{% endif %}
{% if prefix %}
{% include 'rest_framework/docs/_recursive_menu.html' with items=section.data|items prefix=prefix|add:'/'|add:section_key %}
{% else %}
{% if prefix %}
{% include 'rest_framework/docs/_recursive_menu.html' with items=section|items prefix=prefix|add:'/'|add:section_key %}
{% else %}
{% include 'rest_framework/docs/_recursive_menu.html' with items=section|items prefix=section_key %}
{% endif %}
{% include 'rest_framework/docs/_recursive_menu.html' with items=section.data|items prefix=section_key %}
{% endif %}
{% endfor %}
</ul>
{% endif %}

View File

@ -1,5 +1,6 @@
from django.db import models
from rest_framework import serializers, viewsets
from rest_framework.decorators import detail_route
class DummyModel(models.Model):
@ -16,3 +17,7 @@ class DummySerializer(serializers.ModelSerializer):
class DummyViewSet(viewsets.ModelViewSet):
serializer_class = DummySerializer
queryset = DummyModel.objects.all()
@detail_route(methods=['get', 'post'])
def retrieve_alt(self, request, *args, **kwargs):
return self.retrieve(request, *args, **kwargs)

View File

@ -27,12 +27,17 @@ class TestRecursiveUrlViewSets(TestCase):
)
def test_documentation(self):
self.assertTrue(
re.search('h2.*>not_dummies <a', self.content),
'unable to find documentation section for not_dummies'
)
for model_type in ['aaaa', 'bbbb']:
header_re = 'h{level}\s+id="{path}".*>{title} <a href="#{path}"'
for route in (('not_dummies',), ('dummy', 'aaaas'), ('dummy', 'bbbbs')):
path = "/".join(route)
self.assertTrue(
re.search('h3.*>{}s <a'.format(model_type), self.content),
'unable to find documentation section for dummy/{}'.format(model_type)
re.search(header_re.format(level=1+len(route), path=path, title=route[-1]), self.content),
'unable to find documentation section for {}'.format(path)
)
for method in ('read', 'create'):
subpath = "{}/retrieve_alt-{}".format(path, method)
self.assertTrue(
re.search(header_re.format(level=3, path=subpath, title=method), self.content),
'unable to find documentation section for {}'.format(subpath)
)