Set up DRF

Fixes #5
This commit is contained in:
Matthew Ryan Dillon 2016-06-15 13:27:56 -07:00
parent 07ff19d5b3
commit ab01518bdc
7 changed files with 74 additions and 3 deletions

0
ccdb/api/__init__.py Normal file
View file

18
ccdb/api/urls.py Normal file
View file

@ -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')),
]

49
ccdb/api/views.py Normal file
View file

@ -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)

View file

View file

@ -16,7 +16,6 @@ DJANGO_APPS = (
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'grappelli', 'grappelli',
@ -28,6 +27,10 @@ THIRD_PARTY_APPS = (
'allauth.account', 'allauth.account',
'bootstrap3', 'bootstrap3',
'django_tables2', 'django_tables2',
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
'djoser',
) )
# Apps specific for this project go here. # Apps specific for this project go here.
@ -236,6 +239,6 @@ REST_FRAMEWORK = {
'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.SessionAuthentication',
], ],
'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning', 'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning',
'DEFAULT_PAGINATION_CLASS': 'hibernators.api.pagination.CustomPageNumberPagination', 'DEFAULT_PAGINATION_CLASS': 'drf_ember_pagination.EmberPageNumberPagination',
'PAGE_SIZE': 100, 'PAGE_SIZE': 100,
} }

View file

@ -10,7 +10,7 @@ urlpatterns = [
url(r'^grappelli/', include('grappelli.urls')), url(r'^grappelli/', include('grappelli.urls')),
url(settings.ADMIN_URL, include(admin.site.urls)), url(settings.ADMIN_URL, include(admin.site.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')), 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)), url(r'^$', RedirectView.as_view(url=reverse_lazy('admin:index'), permanent=True)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

View file

@ -45,3 +45,4 @@ djangorestframework==3.3.3
django-cors-headers==1.1.0 django-cors-headers==1.1.0
django-filter==0.13.0 django-filter==0.13.0
djoser==0.4.3 djoser==0.4.3
git+git://github.com/thermokarst/drf_ember_pagination.git