From 43f1c0719e4f657bc3eaec0fa8550bbde7f22f41 Mon Sep 17 00:00:00 2001 From: Micah Denbraver Date: Sat, 1 Nov 2014 19:16:42 -0700 Subject: [PATCH] Add swagger --- freemusicninja/settings.py | 1 + freemusicninja/urls.py | 1 + requirements.txt | 1 + users/tests/test_api.py | 1 - users/views.py | 17 ++++++++--------- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/freemusicninja/settings.py b/freemusicninja/settings.py index 649950b..4566d33 100644 --- a/freemusicninja/settings.py +++ b/freemusicninja/settings.py @@ -50,6 +50,7 @@ 'raven.contrib.django.raven_compat', 'rest_framework', 'rest_framework.authtoken', + 'rest_framework_swagger', 'artists', 'echonest', diff --git a/freemusicninja/urls.py b/freemusicninja/urls.py index 058c865..60de222 100644 --- a/freemusicninja/urls.py +++ b/freemusicninja/urls.py @@ -16,5 +16,6 @@ namespace='rest_framework')), url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token'), + url(r'^docs/', include('rest_framework_swagger.urls')), url(r'^admin/', include(admin.site.urls)), ] diff --git a/requirements.txt b/requirements.txt index 42b75e1..086a50b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ django-cors-headers==0.13 django-filter==0.8 django-jsonfield==0.9.13 django-model-utils==2.2.0 +django-rest-swagger==0.2.0 djangorestframework==2.4.3 psycopg2==2.5.4 purl==1.0 diff --git a/users/tests/test_api.py b/users/tests/test_api.py index 6509f6a..45d185b 100644 --- a/users/tests/test_api.py +++ b/users/tests/test_api.py @@ -12,7 +12,6 @@ class UserTest(APITestCase): def test_list_users_logged_out(self): url = reverse('user-list') response = self.client.get(url, format='json') - print(response.data) assert response.status_code == status.HTTP_200_OK assert (response.data == [{ 'id': 1, diff --git a/users/views.py b/users/views.py index 96719ec..8e05a5c 100644 --- a/users/views.py +++ b/users/views.py @@ -12,17 +12,16 @@ class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() permission_classes = (IsUserOrReadOnly,) + serializer_class = UserSerializer - def get_serializer_class(self): - try: - obj = self.get_object() - except ImproperlyConfigured: # assume list view, use non-authenticated - pass + def get_serializer(self, instance=None, *args, **kwargs): + context = self.get_serializer_context() + if (self.request.user.is_authenticated() + and instance == self.request.user): + serializer_class = AuthenticatedUserSerializer else: - if (self.request.user.is_authenticated() - and obj == self.request.user): - return AuthenticatedUserSerializer - return UserSerializer + serializer_class = self.serializer_class + return serializer_class(instance, context=context, *args, **kwargs) def initial(self, request, *args, **kwargs): """Retrieve given user or current user if ``pk`` is "me"."""