From ab01518bdc6c1f6641aefd41734c00630587c564 Mon Sep 17 00:00:00 2001 From: Matthew Dillon Date: Wed, 15 Jun 2016 13:27:56 -0700 Subject: [PATCH] Set up DRF Fixes #5 --- ccdb/api/__init__.py | 0 ccdb/api/urls.py | 18 ++++++++++++ ccdb/api/views.py | 49 +++++++++++++++++++++++++++++++ ccdb/utils/migrations/__init__.py | 0 config/settings/base.py | 7 +++-- config/urls.py | 2 +- requirements/base.txt | 1 + 7 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 ccdb/api/__init__.py create mode 100644 ccdb/api/urls.py create mode 100644 ccdb/api/views.py create mode 100644 ccdb/utils/migrations/__init__.py diff --git a/ccdb/api/__init__.py b/ccdb/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ccdb/api/urls.py b/ccdb/api/urls.py new file mode 100644 index 0000000..17243f9 --- /dev/null +++ b/ccdb/api/urls.py @@ -0,0 +1,18 @@ +from django.conf.urls import url, include + +from rest_framework import routers + +from . import views as api_v + + +router = routers.DefaultRouter() + +router.register(r'administrative-urls', api_v.AdminURLs, base_name='adminurls') + +urlpatterns = [ + url(r'^auth/login/', api_v.Login.as_view()), + url(r'^auth/password/reset/confirm/', api_v.PasswordResetConfirm.as_view()), + url(r'^auth/password/reset/', api_v.PasswordReset.as_view()), + url(r'^auth/', include('djoser.urls.authtoken')), + url(r'^v1/', include(router.urls, namespace='v1')), +] diff --git a/ccdb/api/views.py b/ccdb/api/views.py new file mode 100644 index 0000000..e51d501 --- /dev/null +++ b/ccdb/api/views.py @@ -0,0 +1,49 @@ +from django.contrib.auth import user_logged_in + +from rest_framework import status +from rest_framework.authtoken.models import Token +from rest_framework.response import Response +from rest_framework.reverse import reverse +from rest_framework.viewsets import ViewSet + +from djoser.views import LoginView, PasswordResetView, PasswordResetConfirmView + + +class Login(LoginView): + def action(self, serializer): + user = serializer.user + token, _ = Token.objects.get_or_create(user=user) + user_logged_in.send(sender=user.__class__, request=self.request, user=user) + return Response( + data={'token': token.key, 'id': token.user_id}, + status=status.HTTP_200_OK, + ) + + +class PasswordReset(PasswordResetView): + """Overriding to return empty object, for ember-ajax""" + def action(self, serializer): + response = super(PasswordReset, self).action(serializer) + response.data = {} + return response + + +class PasswordResetConfirm(PasswordResetConfirmView): + """Overriding to return empty object, for ember-ajax""" + def action(self, serializer): + response = super(PasswordResetConfirm, self).action(serializer) + response.data = {} + return response + + +class AdminURLs(ViewSet): + def get_view_name(self): + return 'Admin URLs List' + + def list(self, request, *args, **kwargs): + urls = [ + ['collection-type', 'collections_ccdb', 'collectiontype'], + ] + + data = [{'id': x[0], 'url': reverse('admin:{}_{}_changelist'.format(x[1], x[2]) , request=request)} for x in urls] + return Response(data) diff --git a/ccdb/utils/migrations/__init__.py b/ccdb/utils/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/config/settings/base.py b/config/settings/base.py index d9d6a67..a11a2f6 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -16,7 +16,6 @@ DJANGO_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', - 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'grappelli', @@ -28,6 +27,10 @@ THIRD_PARTY_APPS = ( 'allauth.account', 'bootstrap3', 'django_tables2', + 'rest_framework', + 'rest_framework.authtoken', + 'corsheaders', + 'djoser', ) # Apps specific for this project go here. @@ -236,6 +239,6 @@ REST_FRAMEWORK = { 'rest_framework.authentication.SessionAuthentication', ], 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning', - 'DEFAULT_PAGINATION_CLASS': 'hibernators.api.pagination.CustomPageNumberPagination', + 'DEFAULT_PAGINATION_CLASS': 'drf_ember_pagination.EmberPageNumberPagination', 'PAGE_SIZE': 100, } diff --git a/config/urls.py b/config/urls.py index 13ebb3f..6388f9f 100644 --- a/config/urls.py +++ b/config/urls.py @@ -10,7 +10,7 @@ urlpatterns = [ url(r'^grappelli/', include('grappelli.urls')), url(settings.ADMIN_URL, include(admin.site.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), - # url(r'^api/', include('ccdb.api.urls', namespace='api')), + url(r'^api/', include('ccdb.api.urls', namespace='api')), url(r'^$', RedirectView.as_view(url=reverse_lazy('admin:index'), permanent=True)), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) diff --git a/requirements/base.txt b/requirements/base.txt index e334345..067b10a 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -45,3 +45,4 @@ djangorestframework==3.3.3 django-cors-headers==1.1.0 django-filter==0.13.0 djoser==0.4.3 +git+git://github.com/thermokarst/drf_ember_pagination.git