diff options
Diffstat (limited to 'tools/pharos-dashboard/src/api')
-rw-r--r-- | tools/pharos-dashboard/src/api/urls.py | 1 | ||||
-rw-r--r-- | tools/pharos-dashboard/src/api/views.py | 18 |
2 files changed, 18 insertions, 1 deletions
diff --git a/tools/pharos-dashboard/src/api/urls.py b/tools/pharos-dashboard/src/api/urls.py index 5206ac7f..dfbe1ac9 100644 --- a/tools/pharos-dashboard/src/api/urls.py +++ b/tools/pharos-dashboard/src/api/urls.py @@ -35,4 +35,5 @@ router.register(r'bookings', BookingViewSet) urlpatterns = [ url(r'^', include(router.urls)), + url(r'^token$', GenerateTokenView.as_view(), name='generate_token'), ]
\ No newline at end of file diff --git a/tools/pharos-dashboard/src/api/views.py b/tools/pharos-dashboard/src/api/views.py index 761ce6e7..55de5c6a 100644 --- a/tools/pharos-dashboard/src/api/views.py +++ b/tools/pharos-dashboard/src/api/views.py @@ -10,9 +10,14 @@ from rest_framework import viewsets +from django.contrib.auth.decorators import login_required +from django.utils.decorators import method_decorator from api.serializers import ResourceSerializer, ServerSerializer, BookingSerializer from booking.models import Booking from dashboard.models import Resource, Server +from django.views import View +from rest_framework.authtoken.models import Token +from django.shortcuts import redirect class BookingViewSet(viewsets.ModelViewSet): @@ -30,4 +35,15 @@ class ServerViewSet(viewsets.ModelViewSet): class ResourceViewSet(viewsets.ModelViewSet): queryset = Resource.objects.all() serializer_class = ResourceSerializer - filter_fields = ('name',)
\ No newline at end of file + filter_fields = ('name',) + + +@method_decorator(login_required, name='dispatch') +class GenerateTokenView(View): + def get(self, request, *args, **kwargs): + user = self.request.user + token, created = Token.objects.get_or_create(user=user) + if not created: + token.delete() + Token.objects.create(user=user) + return redirect('account:settings') |