Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New field to exclude APIViews from endpoint list #90

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ djangorestframework==3.3.2
coverage==4.0.3
flake8==2.5.1
mkdocs==0.15.3
django-markdown2==2.3.1
3 changes: 2 additions & 1 deletion rest_framework_docs/api_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def get_all_view_names(self, urlpatterns, parent_pattern=None):
self.get_all_view_names(urlpatterns=pattern.url_patterns, parent_pattern=parent_pattern)
elif isinstance(pattern, RegexURLPattern) and self._is_drf_view(pattern) and not self._is_format_endpoint(pattern):
api_endpoint = ApiEndpoint(pattern, parent_pattern)
self.endpoints.append(api_endpoint)
if not api_endpoint.exclude:
self.endpoints.append(api_endpoint)

def _is_drf_view(self, pattern):
"""
Expand Down
15 changes: 7 additions & 8 deletions rest_framework_docs/api_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class ApiEndpoint(object):

def __init__(self, pattern, parent_pattern=None):
self.pattern = pattern
self.callback = pattern.callback
Expand All @@ -19,6 +18,7 @@ def __init__(self, pattern, parent_pattern=None):
self.fields = self.__get_serializer_fields__()
self.fields_json = self.__get_serializer_fields_json__()
self.permissions = self.__get_permissions_class__()
self.exclude = getattr(self.callback.cls, 'drfdocs_exclude', False)

def __get_path__(self, parent_pattern):
if parent_pattern:
Expand All @@ -42,17 +42,16 @@ def __get_serializer_fields__(self):
serializer = self.callback.cls.serializer_class
if hasattr(serializer, 'get_fields'):
try:
fields = [{
"name": key,
"type": str(field.__class__.__name__),
"required": field.required
} for key, field in serializer().get_fields().items()]
fields = [{"name": key,
"type": str(field.__class__.__name__),
"required": field.required
} for key, field in serializer().get_fields().items()]
except KeyError as e:
self.errors = e
fields = []

# FIXME:
# Show more attibutes of `field`?
# FIXME:
# Show more attibutes of `field`?

return fields

Expand Down
5 changes: 3 additions & 2 deletions rest_framework_docs/templates/rest_framework_docs/home.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{% extends "rest_framework_docs/docs.html" %}
{% load md2 %}

{% block apps_menu %}
{% regroup endpoints by name_parent as endpoints_grouped %}
Expand Down Expand Up @@ -56,7 +57,7 @@ <h4 class="panel-title title">
<div id="{{ endpoint.path|slugify }}" class="panel-collapse collapse" role="tabpanel">
<div class="panel-body">
{% if endpoint.docstring %}
<p class="lead">{{ endpoint.docstring }}</p>
<div class="lead">{{ endpoint.docstring | markdown:"code-friendly, code-color"}}</div>
{% endif %}

{% if endpoint.errors %}
Expand All @@ -71,7 +72,7 @@ <h4 class="panel-title title">
{% endfor %}
</ul>
{% elif not endpoint.errors %}
<p>No fields.</p>
<!--<p>No fields.</p>-->
{% endif %}
</div>
</div>
Expand Down
5 changes: 2 additions & 3 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@


class DRFDocsViewTests(TestCase):

SETTINGS_HIDE_DOCS = {
'HIDE_DOCS': True # Default: False
}
Expand All @@ -13,7 +12,6 @@ def setUp(self):
super(DRFDocsViewTests, self).setUp()

def test_settings_module(self):

settings = DRFSettings()

self.assertEqual(settings.get_setting("HIDE_DOCS"), False)
Expand All @@ -33,7 +31,8 @@ def test_index_view_with_endpoints(self):
self.assertEqual(response.context["endpoints"][0].name_parent, "accounts")
self.assertEqual(response.context["endpoints"][0].allowed_methods, ['POST', 'OPTIONS'])
self.assertEqual(response.context["endpoints"][0].path, "/accounts/login/")
self.assertEqual(response.context["endpoints"][0].docstring, "A view that allows users to login providing their username and password.")
self.assertEqual(response.context["endpoints"][0].docstring,
"A view that allows users to login providing their username and password.")
self.assertEqual(len(response.context["endpoints"][0].fields), 2)
self.assertEqual(response.context["endpoints"][0].fields[0]["type"], "CharField")
self.assertTrue(response.context["endpoints"][0].fields[0]["required"])
Expand Down
1 change: 1 addition & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
url(r'^user/profile/$', views.UserProfileView.as_view(), name="profile"),

url(r'^test/$', views.TestView.as_view(), name="test-view"),
url(r'^exclude/$', views.ExcludedTestView.as_view(), name="test-exclude"),
]

organisations_urls = [
Expand Down
16 changes: 8 additions & 8 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,18 @@ class TestView(TemplateView):
template_name = "a_test.html"


class ExcludedTestView(APIView):
"""
This view should not be included in DRF Docs.
"""
drfdocs_exclude = True


class LoginView(APIView):
"""
A view that allows users to login providing their username and password.
"""

drfdocs_exclude = False
throttle_classes = ()
permission_classes = ()
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
Expand All @@ -39,7 +46,6 @@ def post(self, request):


class UserRegistrationView(generics.CreateAPIView):

permission_classes = (AllowAny,)
serializer_class = serializers.UserRegistrationSerializer

Expand All @@ -56,7 +62,6 @@ def get_object(self):


class PasswordResetView(APIView):

permission_classes = (AllowAny,)
queryset = User.objects.all()

Expand All @@ -72,7 +77,6 @@ def post(self, request, *args, **kwargs):


class PasswordResetConfirmView(APIView):

permission_classes = (AllowAny,)
serializer_class = serializers.ResetPasswordSerializer

Expand All @@ -84,12 +88,10 @@ def post(self, request, *args, **kwargs):


class CreateOrganisationView(generics.CreateAPIView):

serializer_class = serializers.CreateOrganisationSerializer


class OrganisationMembersView(generics.ListAPIView):

serializer_class = serializers.OrganisationMembersSerializer

def get_queryset(self):
Expand All @@ -98,7 +100,6 @@ def get_queryset(self):


class LeaveOrganisationView(generics.DestroyAPIView):

def get_object(self):
return Membership.objects.order_by('?').first()

Expand All @@ -109,5 +110,4 @@ def delete(self, request, *args, **kwargs):


class OrganisationErroredView(generics.ListAPIView):

serializer_class = serializers.OrganisationErroredSerializer