From 613369d9e9125d2ec8e61a32d742926950128aff Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Mon, 17 Feb 2020 03:27:41 +0000 Subject: [PATCH] Add API route for paste --- apiserver/apiserver/api/views.py | 18 ++++++++++++++---- apiserver/apiserver/urls.py | 1 + 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index 602bae0..80f0e63 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -6,7 +6,7 @@ from django.core.files.base import File from django.core.cache import cache from rest_framework import viewsets, views, mixins, generics, exceptions from rest_framework.decorators import action -from rest_framework.permissions import BasePermission, IsAuthenticated, SAFE_METHODS +from rest_framework.permissions import BasePermission, IsAuthenticated, SAFE_METHODS, IsAuthenticatedOrReadOnly from rest_framework.response import Response from rest_auth.views import PasswordChangeView from rest_auth.registration.views import RegisterView @@ -303,7 +303,6 @@ class PingView(views.APIView): return Response(200) - class DoorViewSet(viewsets.ViewSet, List): def list(self, request): cards = models.Card.objects.filter(active_status='card_active') @@ -328,7 +327,6 @@ class DoorViewSet(viewsets.ViewSet, List): return Response(200) - class IpnView(views.APIView): def post(self, request): try: @@ -339,7 +337,6 @@ class IpnView(views.APIView): return Response(200) - class StatsView(views.APIView): def get(self, request): stats_keys = utils_stats.DEFAULTS.keys() @@ -369,6 +366,19 @@ class BackupView(views.APIView): return Response(dict(url=backup_url)) +class PasteView(views.APIView): + permission_classes = [IsAuthenticatedOrReadOnly] + + def get(self, request): + return Response(dict(paste=cache.get('paste', ''))) + + def post(self, request): + if 'paste' in request.data: + cache.set('paste', request.data['paste'][:20000]) + return Response(dict(paste=cache.get('paste', ''))) + else: + raise exceptions.ValidationError(dict(paste='This field is required.')) + class RegistrationView(RegisterView): serializer_class = serializers.MyRegisterSerializer diff --git a/apiserver/apiserver/urls.py b/apiserver/apiserver/urls.py index 12d50b3..8794888 100644 --- a/apiserver/apiserver/urls.py +++ b/apiserver/apiserver/urls.py @@ -30,6 +30,7 @@ urlpatterns = [ url(r'^user/', views.UserView.as_view(), name='user'), url(r'^ping/', views.PingView.as_view(), name='ping'), url(r'^stats/', views.StatsView.as_view(), name='stats'), + url(r'^paste/', views.PasteView.as_view(), name='paste'), url(r'^backup/', views.BackupView.as_view(), name='backup'), url(IPN_ROUTE, views.IpnView.as_view(), name='ipn'), ]