[PATCH 1/2] REST: Fix versioning
Stephen Finucane
stephen at that.guru
Thu Apr 27 09:58:03 AEST 2017
One of the few remaining warts in the API is versioning. There is some
basic versioning there, but it doesn't work properly as-is. Fix this by
correcting the index endpoint ('/') to use Django REST Framework's
'reverse' function [1], which handles versioning for us [2] and
switching from 'NamespaceVersioning' to 'URLPathVersioning', the latter
of which does the same thing but in a different way.
[1] http://www.django-rest-framework.org/api-guide/reverse/#reverse
[2] http://www.django-rest-framework.org/api-guide/versioning/#reversing-urls-for-versioned-apis
[3] http://www.django-rest-framework.org/api-guide/versioning/#urlpathversioning
Signed-off-by: Stephen Finucane <stephen at that.guru>
Cc: Andy Doan <andy.doan at linaro.org>
---
patchwork/api/event.py | 2 +-
patchwork/api/index.py | 21 ++++++++++-----------
patchwork/api/patch.py | 2 +-
patchwork/settings/base.py | 2 +-
patchwork/urls.py | 2 +-
5 files changed, 14 insertions(+), 15 deletions(-)
diff --git a/patchwork/api/event.py b/patchwork/api/event.py
index 32b3bde..a060022 100644
--- a/patchwork/api/event.py
+++ b/patchwork/api/event.py
@@ -19,8 +19,8 @@
from collections import OrderedDict
-from django.core.urlresolvers import reverse
from rest_framework.generics import ListAPIView
+from rest_framework.reverse import reverse
from rest_framework.serializers import HyperlinkedModelSerializer
from rest_framework.serializers import SerializerMethodField
diff --git a/patchwork/api/index.py b/patchwork/api/index.py
index 513e8b6..1cdd379 100644
--- a/patchwork/api/index.py
+++ b/patchwork/api/index.py
@@ -17,22 +17,21 @@
# along with Patchwork; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-from django.core.urlresolvers import reverse
from rest_framework.response import Response
+from rest_framework.reverse import reverse
from rest_framework.views import APIView
class IndexView(APIView):
- def get(self, request, format=None):
+ def get(self, request, version=None):
return Response({
- 'projects': request.build_absolute_uri(
- reverse('api-project-list')),
- 'users': request.build_absolute_uri(reverse('api-user-list')),
- 'people': request.build_absolute_uri(reverse('api-person-list')),
- 'patches': request.build_absolute_uri(reverse('api-patch-list')),
- 'covers': request.build_absolute_uri(reverse('api-cover-list')),
- 'series': request.build_absolute_uri(reverse('api-series-list')),
- 'events': request.build_absolute_uri(reverse('api-event-list')),
- 'bundles': request.build_absolute_uri(reverse('api-bundle-list')),
+ 'projects': reverse('api-project-list', request=request),
+ 'users': reverse('api-user-list', request=request),
+ 'people': reverse('api-person-list', request=request),
+ 'patches': reverse('api-patch-list', request=request),
+ 'covers': reverse('api-cover-list', request=request),
+ 'series': reverse('api-series-list', request=request),
+ 'events': reverse('api-event-list', request=request),
+ 'bundles': reverse('api-bundle-list', request=request),
})
diff --git a/patchwork/api/patch.py b/patchwork/api/patch.py
index ded4910..a231524 100644
--- a/patchwork/api/patch.py
+++ b/patchwork/api/patch.py
@@ -19,11 +19,11 @@
import email.parser
-from django.core.urlresolvers import reverse
from django.utils.translation import ugettext_lazy as _
from rest_framework.generics import ListAPIView
from rest_framework.generics import RetrieveUpdateAPIView
from rest_framework.relations import RelatedField
+from rest_framework.reverse import reverse
from rest_framework.serializers import HyperlinkedModelSerializer
from rest_framework.serializers import SerializerMethodField
diff --git a/patchwork/settings/base.py b/patchwork/settings/base.py
index 180a469..cafb4a6 100644
--- a/patchwork/settings/base.py
+++ b/patchwork/settings/base.py
@@ -144,7 +144,7 @@ except ImportError:
REST_FRAMEWORK = {
'DEFAULT_VERSIONING_CLASS':
- 'rest_framework.versioning.NamespaceVersioning',
+ 'rest_framework.versioning.URLPathVersioning',
'DEFAULT_PAGINATION_CLASS': 'patchwork.api.base.LinkHeaderPagination',
'DEFAULT_FILTER_BACKENDS': (
'django_filters.rest_framework.DjangoFilterBackend',
diff --git a/patchwork/urls.py b/patchwork/urls.py
index e888386..8c8c172 100644
--- a/patchwork/urls.py
+++ b/patchwork/urls.py
@@ -232,7 +232,7 @@ if settings.ENABLE_REST_API:
]
urlpatterns += [
- url(r'^api/1.0/', include(api_patterns)),
+ url(r'^api/(?P<version>(1.0))/', include(api_patterns)),
]
--
2.9.3
More information about the Patchwork
mailing list