[A] add token authentication
[U] update OfficeHoursAPI, use CreateAPIView
This commit is contained in:
parent
25572c23c2
commit
97054f38fb
@ -1 +1,8 @@
|
|||||||
from .settings_default import *
|
from .settings_default import *
|
||||||
|
REST_FRAMEWORK = {
|
||||||
|
'DEFAULT_AUTHENTICATION_CLASSES': (
|
||||||
|
'rest_framework.authentication.BasicAuthentication',
|
||||||
|
'rest_framework.authentication.SessionAuthentication',
|
||||||
|
),
|
||||||
|
'PAGE_SIZE': 10
|
||||||
|
}
|
||||||
|
|||||||
@ -38,8 +38,10 @@ INSTALLED_APPS = [
|
|||||||
'django.contrib.messages',
|
'django.contrib.messages',
|
||||||
'django.contrib.staticfiles',
|
'django.contrib.staticfiles',
|
||||||
|
|
||||||
|
|
||||||
# third party
|
# third party
|
||||||
'rest_framework',
|
'rest_framework',
|
||||||
|
'rest_framework.authtoken',
|
||||||
|
|
||||||
# user apps
|
# user apps
|
||||||
'timer',
|
'timer',
|
||||||
|
|||||||
@ -19,12 +19,15 @@ from django.urls import include, path
|
|||||||
from django.conf.urls import url
|
from django.conf.urls import url
|
||||||
|
|
||||||
from rest_framework import routers
|
from rest_framework import routers
|
||||||
|
from rest_framework.authtoken import views
|
||||||
|
|
||||||
router = routers.DefaultRouter()
|
router = routers.DefaultRouter()
|
||||||
# Wire up our API using automatic URL routing.
|
# Wire up our API using automatic URL routing.
|
||||||
# Additionally, we include login URLs for the browsable API.
|
# Additionally, we include login URLs for the browsable API.
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', include(router.urls)),
|
path('', include(router.urls)),
|
||||||
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
path('api-auth/', include('rest_framework.urls', namespace='rest_framework')),
|
||||||
|
path('api-auth-token/', views.obtain_auth_token, name='api-auth-token'),
|
||||||
url('timer/', include('timer.urls')),
|
url('timer/', include('timer.urls')),
|
||||||
]
|
]
|
||||||
|
|||||||
@ -7,5 +7,5 @@ from timer import views
|
|||||||
# Wire up our API using automatic URL routing.
|
# Wire up our API using automatic URL routing.
|
||||||
# Additionally, we include login URLs for the browsable API.
|
# Additionally, we include login URLs for the browsable API.
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url('office-hours', views.OfficeHoursViewSet.as_view({'get': 'list'}))
|
url('office-hours', views.OfficeHoursAPI.as_view())
|
||||||
]
|
]
|
||||||
|
|||||||
@ -1,31 +1,68 @@
|
|||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth.models import User, Group
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from rest_framework import viewsets
|
from rest_framework import authentication, permissions, status, viewsets
|
||||||
|
from rest_framework.generics import CreateAPIView
|
||||||
|
from rest_framework.response import Response
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
import timer.serializers
|
import timer.serializers
|
||||||
import timer.models
|
import timer.models
|
||||||
|
|
||||||
|
import datetime
|
||||||
class OfficeHoursViewSet(viewsets.ModelViewSet):
|
|
||||||
"""
|
|
||||||
API endpoint that allows office hours to be viewed or edited.
|
|
||||||
"""
|
|
||||||
|
|
||||||
queryset = timer.models.OfficeHours.objects.order_by('-id')
|
|
||||||
serializer_class = timer.serializers.OfficeHoursSerializer
|
|
||||||
|
|
||||||
|
|
||||||
class UserViewSet(viewsets.ModelViewSet):
|
# class OfficeHoursViewSet(viewsets.ModelViewSet):
|
||||||
"""
|
# """
|
||||||
API endpoint that allows users to be viewed or edited.
|
# API endpoint that allows office hours to be viewed or edited.
|
||||||
"""
|
# """
|
||||||
queryset = User.objects.all().order_by('-date_joined')
|
|
||||||
serializer_class = timer.serializers.UserSerializer
|
# queryset = timer.models.OfficeHours.objects.order_by('-id')
|
||||||
|
# serializer_class = timer.serializers.OfficeHoursSerializer
|
||||||
|
|
||||||
|
|
||||||
class GroupViewSet(viewsets.ModelViewSet):
|
# class UserViewSet(viewsets.ModelViewSet):
|
||||||
"""
|
# """
|
||||||
API endpoint that allows groups to be viewed or edited.
|
# API endpoint that allows users to be viewed or edited.
|
||||||
"""
|
# """
|
||||||
queryset = Group.objects.all()
|
# queryset = User.objects.all().order_by('-date_joined')
|
||||||
serializer_class = timer.serializers.GroupSerializer
|
# serializer_class = timer.serializers.UserSerializer
|
||||||
|
|
||||||
|
|
||||||
|
# class GroupViewSet(viewsets.ModelViewSet):
|
||||||
|
# """
|
||||||
|
# API endpoint that allows groups to be viewed or edited.
|
||||||
|
# """
|
||||||
|
# queryset = Group.objects.all()
|
||||||
|
# serializer_class = timer.serializers.GroupSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class OfficeHoursAPI(CreateAPIView):
|
||||||
|
|
||||||
|
authentication_classes = (authentication.TokenAuthentication,
|
||||||
|
authentication.SessionAuthentication,
|
||||||
|
authentication.BasicAuthentication)
|
||||||
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
|
|
||||||
|
queryset = timer.models.OfficeHours.objects.all()
|
||||||
|
|
||||||
|
def create(self, request, *args, **kwargs):
|
||||||
|
begins_at = request.data.get('begins_at')
|
||||||
|
if not begins_at:
|
||||||
|
raise
|
||||||
|
try:
|
||||||
|
begins_at = datetime.datetime.strptime(begins_at, '%Y-%m-%d %H:%M')
|
||||||
|
ends_at = begins_at + datetime.timedelta(hours=9.5)
|
||||||
|
oh, __ = timer.models.OfficeHours.objects.get_or_create(
|
||||||
|
begins_at=begins_at,
|
||||||
|
ends_at=ends_at,
|
||||||
|
user=request.user)
|
||||||
|
except ValueError:
|
||||||
|
raise
|
||||||
|
|
||||||
|
resp_data = {
|
||||||
|
'begins_at': begins_at.strftime('%Y-%m-%d %H:%M'),
|
||||||
|
'ends_at': ends_at.strftime('%Y-%m-%d %H:%M'),
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response(resp_data, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user