diff --git a/.gitignore b/.gitignore index bde1b9d..da87042 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ rest_framework_docs/static/rest_framework_docs/js/dist.min.js rest_framework_docs/static/node_modules/ rest_framework_docs/static/rest_framework_docs/js/dist.min.js.map +.idea \ No newline at end of file diff --git a/README.md b/README.md index 00010a6..d740982 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,9 @@ You can find detailed information about the package's settings at [the docs](htt REST_FRAMEWORK_DOCS = { 'HIDE_DOCS': True # Default: False + 'MODULE_ROUTERS': {}, + 'DEFAULT_MODULE_ROUTER': 'router', + 'DEFAULT_ROUTER': None, } @@ -68,14 +71,14 @@ You can find detailed information about the package's settings at [the docs](htt First of all thanks to the [Django](http://www.djangoproject.com/) core team and to all the contributors of [Django REST Framework](http://www.django-rest-framework.org/) for their amazing work. Also I would like to thank [Marc Gibbons](https://github.com/marcgibbons) for his *django-rest-framework-docs* project. Both projects share the same idea, it is just that Marc's is not maintained anymore and does not support DRF 3+ & Python 3. -[travis-image]: https://travis-ci.org/ekonstantinidis/django-rest-framework-docs.svg?branch=master -[travis-url]: https://travis-ci.org/ekonstantinidis/django-rest-framework-docs +[travis-image]: https://travis-ci.org/manosim/django-rest-framework-docs.svg?branch=master +[travis-url]: https://travis-ci.org/manosim/django-rest-framework-docs [pypi-image]: https://badge.fury.io/py/drfdocs.svg [pypi-url]: https://pypi.python.org/pypi/drfdocs/ -[codecov-image]: https://codecov.io/github/ekonstantinidis/django-rest-framework-docs/coverage.svg?branch=master -[codecov-url]:https://codecov.io/github/ekonstantinidis/django-rest-framework-docs?branch=master +[codecov-image]: https://codecov.io/github/manosim/django-rest-framework-docs/coverage.svg?branch=master +[codecov-url]:https://codecov.io/github/manosim/django-rest-framework-docs?branch=master [slack-image]: https://img.shields.io/badge/slack-pythondev/drfdocs-e01563.svg [slack-url]: https://pythondev.slack.com diff --git a/demo/project/accounts/urls.py b/demo/project/accounts/urls.py index 1486675..9fb4c43 100644 --- a/demo/project/accounts/urls.py +++ b/demo/project/accounts/urls.py @@ -1,15 +1,25 @@ -from django.conf.urls import url +from django.conf.urls import url, include +from rest_framework import routers from project.accounts import views +router = routers.DefaultRouter() +router.register( + r'user_viewset', + views.UserModelViewSet, +) + + urlpatterns = [ url(r'^test/$', views.TestView.as_view(), name="test-view"), url(r'^login/$', views.LoginView.as_view(), name="login"), url(r'^register/$', views.UserRegistrationView.as_view(), name="register"), + url(r'^register/$', views.UserRegistrationView.as_view(), name="register"), url(r'^reset-password/$', view=views.PasswordResetView.as_view(), name="reset-password"), url(r'^reset-password/confirm/$', views.PasswordResetConfirmView.as_view(), name="reset-password-confirm"), url(r'^user/profile/$', views.UserProfileView.as_view(), name="profile"), + url(r'^viewset_test/', include(router.urls), name="user_viewset"), ] diff --git a/demo/project/accounts/views.py b/demo/project/accounts/views.py index e1bd9c0..6c1bb48 100644 --- a/demo/project/accounts/views.py +++ b/demo/project/accounts/views.py @@ -6,6 +6,7 @@ from rest_framework.permissions import AllowAny from rest_framework.response import Response from rest_framework.views import APIView +from rest_framework.viewsets import ModelViewSet from project.accounts.models import User from project.accounts.serializers import ( UserRegistrationSerializer, UserProfileSerializer, ResetPasswordSerializer @@ -81,3 +82,8 @@ def post(self, request, *args, **kwargs): if not serializer.is_valid(): return Response({'errors': serializer.errors}, status=status.HTTP_400_BAD_REQUEST) return Response({"msg": "Password updated successfully."}, status=status.HTTP_200_OK) + + +class UserModelViewSet(ModelViewSet): + queryset = User.objects.all() + serializer_class = UserProfileSerializer diff --git a/demo/project/organisations/urls.py b/demo/project/organisations/urls.py index 4d0311e..0f57c91 100644 --- a/demo/project/organisations/urls.py +++ b/demo/project/organisations/urls.py @@ -1,12 +1,21 @@ -from django.conf.urls import url +from django.conf.urls import url, include +from rest_framework import routers from project.organisations import views +router = routers.DefaultRouter() +router.register( + r'organisation_viewset', + views.OrganisationViewSet, +) + + urlpatterns = [ url(r'^create/$', view=views.CreateOrganisationView.as_view(), name="create"), url(r'^(?P[\w-]+)/$', view=views.RetrieveOrganisationView.as_view(), name="organisation"), url(r'^(?P[\w-]+)/members/$', view=views.OrganisationMembersView.as_view(), name="members"), - url(r'^(?P[\w-]+)/leave/$', view=views.LeaveOrganisationView.as_view(), name="leave") + url(r'^(?P[\w-]+)/leave/$', view=views.LeaveOrganisationView.as_view(), name="leave"), + url(r'^', include(router.urls), name="organisation_viewset"), ] diff --git a/demo/project/organisations/views.py b/demo/project/organisations/views.py index 1e2d5fb..c40f630 100644 --- a/demo/project/organisations/views.py +++ b/demo/project/organisations/views.py @@ -1,8 +1,10 @@ from rest_framework import generics, status from rest_framework.response import Response +from rest_framework.viewsets import ModelViewSet from project.organisations.models import Organisation, Membership from project.organisations.serializers import ( - CreateOrganisationSerializer, OrganisationMembersSerializer, RetrieveOrganisationSerializer + CreateOrganisationSerializer, OrganisationMembersSerializer, + RetrieveOrganisationSerializer, OrganisationDetailSerializer ) @@ -34,3 +36,8 @@ def delete(self, request, *args, **kwargs): instance = self.get_object() self.perform_destroy(instance) return Response(status=status.HTTP_204_NO_CONTENT) + + +class OrganisationViewSet(ModelViewSet): + queryset = Organisation.objects.all() + serializer_class = OrganisationDetailSerializer diff --git a/demo/project/settings.py b/demo/project/settings.py index 5e06207..0b34c30 100644 --- a/demo/project/settings.py +++ b/demo/project/settings.py @@ -101,6 +101,14 @@ ) } +REST_FRAMEWORK_DOCS = { + 'HIDE_DOCS': False, + 'MODULE_ROUTERS': { + 'project.accounts.urls': 'router', + }, + 'DEFAULT_MODULE_ROUTER': 'router', +} + # Internationalization # https://docs.djangoproject.com/en/1.8/topics/i18n/ diff --git a/demo/project/urls.py b/demo/project/urls.py index d8e049f..f764dcf 100644 --- a/demo/project/urls.py +++ b/demo/project/urls.py @@ -16,11 +16,15 @@ from django.conf.urls import include, url from django.contrib import admin + urlpatterns = [ url(r'^admin/', include(admin.site.urls)), url(r'^docs/', include('rest_framework_docs.urls')), # API url(r'^accounts/', view=include('project.accounts.urls', namespace='accounts')), + + url(r'^organisations/', view=include('project.organisations.urls', namespace='organisations')), + ] diff --git a/docs/contributing.md b/docs/contributing.md index 13938f8..305e06c 100755 --- a/docs/contributing.md +++ b/docs/contributing.md @@ -5,7 +5,7 @@ source_filename: contributing ### Development If you want to **use the demo** app to work on this package: -In the project [repository](https://github.com/ekonstantinidis/django-rest-framework-docs) you can find the demo app(at /demo). It is a project with Django & Django Rest Framework that will allow you to work with this project. +In the project [repository](https://github.com/manosim/django-rest-framework-docs) you can find the demo app(at /demo). It is a project with Django & Django Rest Framework that will allow you to work with this project. From the root of the repository: diff --git a/docs/settings.md b/docs/settings.md index abd2106..b0c5643 100755 --- a/docs/settings.md +++ b/docs/settings.md @@ -6,7 +6,12 @@ source_filename: settings To set DRF docs' settings just include the dictionary below in Django's `settings.py` file. REST_FRAMEWORK_DOCS = { - 'HIDE_DOCS': True + 'HIDE_DOCS': True, + 'MODULE_ROUTERS': { + 'project.accounts.urls': 'accounts_router', + }, + 'DEFAULT_MODULE_ROUTER': 'router', + 'DEFAULT_ROUTER': 'project.urls.default_router', } @@ -21,9 +26,29 @@ You can use hidden to prevent your docs from showing up in different environment Then set the value of the environment variable `HIDE_DRFDOCS` for each environment (ie. Use `.env` files) +##### MODULE_ROUTERS +Use this setting to manually bind url modules to a router instance. The router must be defined in the module, or imported in the module. +For instance, if the router of an app called 'gifts' is 'gifts_router', and the router of another app called 'scuba_diving' is 'scuba_diving_router', the MODULE_ROUTERS setting should look like: + + 'MODULE_ROUTERS': { + 'gifts.urls': 'gift_router', + 'scuba_diving.urls': 'scuba_diving_router' + } + +If there is no entry for a given module, if this setting is not set, or if it is set to None, the value of the DEFAULT_MODULE_ROUTER setting is used. + +##### DEFAULT_MODULE_ROUTER +When set, the value of this setting will be used to find a router for a urls module. If there is no router having the DEFAULT_MODULE_ROUTER name, the setting is ignored and the value of DEFAULT_ROUTER is used. + +##### DEFAULT_ROUTER +When defined, this setting must describe a python dotted path leading to the router that should be used when MODULE_ROUTERS and DEFAULT_MODULE_ROUTER are not set. +This parameter is useful when there is only one router for your whole API. + ### List of Settings -| Setting | Type | Options | Default | -|---------|---------|-----------------|---------| -|HIDE_DOCS| Boolean | `True`, `False` | `False` | -| | | | | +| Setting | Type | Options | Default | +|---------------------|-----------------------------------------------------------|-----------------|---------| +|HIDE_DOCS | Boolean | `True`, `False` | `False` | +|MODULE_ROUTERS | dict of python dotted paths -> router instance name | | `None` | +|DEFAULT_MODULE_ROUTER| str representing a default router instance name | | `None` | +|DEFAULT_ROUTER | str representing a python dotted path to a router instance| | `None` | diff --git a/docs/template/base.html b/docs/template/base.html index 6ea52fa..0db72a9 100755 --- a/docs/template/base.html +++ b/docs/template/base.html @@ -56,8 +56,8 @@