69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
from django.contrib.auth.models import User, Group
|
|
from django.shortcuts import render
|
|
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.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):
|
|
# """
|
|
# API endpoint that allows users to be viewed or edited.
|
|
# """
|
|
# queryset = User.objects.all().order_by('-date_joined')
|
|
# 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)
|
|
|