Commit e420ca26 authored by Bengfort's avatar Bengfort
Browse files

use djangorestframework for API view

parent 5d4f8ceb
......@@ -22,21 +22,18 @@ from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib import messages
from django.db import models
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.utils.decorators import method_decorator
from django.utils.translation import gettext_lazy as _
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import CreateView
from django.views.generic import DeleteView
from django.views.generic import ListView
from django.views.generic import UpdateView
from django.views.generic import View
import requests
from rest_framework.permissions import BasePermission
from rest_framework.response import Response as APIResponse
from rest_framework.views import APIView
from .forms import ScheduleForm
from .models import Invitation
......@@ -139,16 +136,17 @@ class InvitationUpdateView(UpdateView):
return response
@method_decorator(csrf_exempt, 'dispatch')
class InvitationApiView(View):
def dispatch(self, request, *args, **kwargs):
if request.headers.get('Authorization') != 'token ' + settings.API_TOKEN:
raise PermissionDenied
return super().dispatch(request, *args, **kwargs)
class TokenPermission(BasePermission):
def has_permission(self, request, view):
return request.headers.get('Authorization') == 'token ' + settings.API_TOKEN
class InvitationApiView(APIView):
permission_classes = [TokenPermission]
def get(self, request, *args, **kwargs):
invitation = get_object_or_404(Invitation, **kwargs)
return JsonResponse({
return APIResponse({
'datetime': invitation.timeslot.datetime if invitation.timeslot else None,
......@@ -157,9 +155,9 @@ class InvitationApiView(View):
schedule = get_object_or_404(Schedule, uuid=kwargs.pop('schedule__uuid'))
kwargs['schedule_id'] =
invitation, _ = Invitation.objects.get_or_create(**kwargs)
return HttpResponse(status=204)
return APIResponse(status=204)
def delete(self, request, *args, **kwargs):
invitation = get_object_or_404(Invitation, **kwargs)
return HttpResponse(status=204)
return APIResponse(status=204)
......@@ -15,6 +15,7 @@ INSTALLED_APPS = [
......@@ -9,6 +9,7 @@ include_package_data = True
packages = find:
install_requires =
Django == 3.2.6
django-bootstrap4 == 3.0.1
django-npm == 1.0.0
requests == 2.26.0
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment