Skip to content

Commit

Permalink
Merge pull request #19 from ekonstantinidis/search-endpoints
Browse files Browse the repository at this point in the history
Search for endpoints
  • Loading branch information
ekonstantinidis committed Dec 18, 2015
2 parents 66a940b + b7d4bc3 commit fdecdfb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions rest_framework_docs/templates/rest_framework_docs/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="drfdoc-navbar">
<form class="navbar-form navbar-right" role="search">
<form method="get" action="." class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="text" class="form-control" placeholder="Search">
<input type="text" class="form-control" name="search" value="{{ query }}" placeholder="Search">
</div>
</form>
<ul class="nav navbar-nav navbar-right">
Expand Down
4 changes: 3 additions & 1 deletion rest_framework_docs/templates/rest_framework_docs/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ <h4 class="panel-title title">
</div>

{% endfor %}
{% else %}
{% elif not query %}
<h2 class="text-center">There are currently no api endpoints to document.</h2>
{% else %}
<h2 class="text-center">No endpoints found for {{ query }}.</h2>
{% endif %}

{% endblock %}
9 changes: 8 additions & 1 deletion rest_framework_docs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,12 @@ def get_context_data(self, **kwargs):

context = super(DRFDocsView, self).get_context_data(**kwargs)
docs = ApiDocumentation()
context['endpoints'] = docs.get_endpoints()
endpoints = docs.get_endpoints()

query = self.request.GET.get("search", "")
if query and endpoints:
endpoints = [endpoint for endpoint in endpoints if query in endpoint.path]

context['query'] = query
context['endpoints'] = endpoints
return context
10 changes: 10 additions & 0 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ def test_index_view_with_endpoints(self):
# The view "OrganisationErroredView" (organisations/(?P<slug>[\w-]+)/errored/) should contain an error.
self.assertEqual(str(response.context["endpoints"][8].errors), "'test_value'")

def test_index_search_with_endpoints(self):
url = "%s?search=reset-password" % reverse("drfdocs")
print(url)
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.context["endpoints"]), 2)
self.assertEqual(response.context["endpoints"][1].path, "/accounts/reset-password/confirm/")
self.assertEqual(len(response.context["endpoints"][1].fields), 3)

@override_settings(REST_FRAMEWORK_DOCS=SETTINGS_HIDE_DOCS)
def test_index_view_docs_hidden(self):
"""
Expand Down

0 comments on commit fdecdfb

Please sign in to comment.