diff options
author | Stephen Finucane <stephen@that.guru> | 2016-11-24 12:55:42 +0000 |
---|---|---|
committer | Stephen Finucane <stephen@that.guru> | 2016-12-23 23:41:29 +0000 |
commit | 3e8048fc1aef396301083d1141d82de55e423418 (patch) | |
tree | 736080c85b32d4c9d45b63d144b387bd1a5a335a /patchwork/urls.py | |
parent | dc787e93a771addddf0f6ceff5d853240d217f2b (diff) | |
download | patchwork-3e8048fc1aef396301083d1141d82de55e423418.tar patchwork-3e8048fc1aef396301083d1141d82de55e423418.tar.gz |
REST: Use generic views instead of ViewSets
ViewSet provide an easy way to define an API, but they don't offer much
flexibility. To get them to work as expected, a lot of hacks were
required. Generic views provide a more verbose, but ultimately easier to
understand, version. Using generic views lets us remove the dependency
of drf-nested-routers, bringing us back down to two main dependencies.
It also lets us remove the AuthenticatedReadOnly permission class, as
the DRF provides a similar permission class that can be used with
generic views.
The main user facing change is that invalid methods, such as POST on an
endpoint that doesn't allow object creation, will now return a HTTP 405
(Method Not Allowed) error code rather than the HTTP 403 (Forbidden)
error code previously returned. This is the semantically correct option
and should have been used all along.
Signed-off-by: Stephen Finucane <stephen@that.guru>
Reviewed-by: Daniel Axtens <dja@axtens.net>
Diffstat (limited to 'patchwork/urls.py')
-rw-r--r-- | patchwork/urls.py | 63 |
1 files changed, 44 insertions, 19 deletions
diff --git a/patchwork/urls.py b/patchwork/urls.py index 6671837..56db24c 100644 --- a/patchwork/urls.py +++ b/patchwork/urls.py @@ -152,29 +152,54 @@ if settings.ENABLE_REST_API: raise RuntimeError( 'djangorestframework must be installed to enable the REST API.') - from rest_framework.routers import DefaultRouter - from rest_framework_nested.routers import NestedSimpleRouter - - from patchwork.api.check import CheckViewSet - from patchwork.api.patch import PatchViewSet - from patchwork.api.person import PeopleViewSet - from patchwork.api.project import ProjectViewSet - from patchwork.api.user import UserViewSet - - router = DefaultRouter() - router.register('patches', PatchViewSet, 'patch') - router.register('people', PeopleViewSet, 'person') - router.register('projects', ProjectViewSet, 'project') - router.register('users', UserViewSet, 'user') - - patches_router = NestedSimpleRouter(router, r'patches', lookup='patch') - patches_router.register(r'checks', CheckViewSet, base_name='patch-checks') + from patchwork.api import check as api_check_views + from patchwork.api import index as api_index_views + from patchwork.api import patch as api_patch_views + from patchwork.api import person as api_person_views + from patchwork.api import project as api_project_views + from patchwork.api import user as api_user_views + + api_patterns = [ + url(r'^$', + api_index_views.IndexView.as_view(), + name='api-index'), + url(r'^users/$', + api_user_views.UserList.as_view(), + name='api-user-list'), + url(r'^users/(?P<pk>[^/]+)/$', + api_user_views.UserDetail.as_view(), + name='api-user-detail'), + url(r'^people/$', + api_person_views.PersonList.as_view(), + name='api-person-list'), + url(r'^people/(?P<pk>[^/]+)/$', + api_person_views.PersonDetail.as_view(), + name='api-person-detail'), + url(r'^patches/$', + api_patch_views.PatchList.as_view(), + name='api-patch-list'), + url(r'^patches/(?P<pk>[^/]+)/$', + api_patch_views.PatchDetail.as_view(), + name='api-patch-detail'), + url(r'^patches/(?P<patch_id>[^/]+)/checks/$', + api_check_views.CheckListCreate.as_view(), + name='api-check-list'), + url(r'^patches/(?P<patch_id>[^/]+)/checks/(?P<check_id>[^/]+)/$', + api_check_views.CheckDetail.as_view(), + name='api-check-detail'), + url(r'^projects/$', + api_project_views.ProjectList.as_view(), + name='api-project-list'), + url(r'^projects/(?P<pk>[^/]+)/$', + api_project_views.ProjectDetail.as_view(), + name='api-project-detail'), + ] urlpatterns += [ - url(r'^api/1.0/', include(router.urls, namespace='api_1.0')), - url(r'^api/1.0/', include(patches_router.urls, namespace='api_1.0')), + url(r'^api/1.0/', include(api_patterns)), ] + # redirect from old urls if settings.COMPAT_REDIR: urlpatterns += [ |