Add API route for paste

This commit is contained in:
Tanner Collin 2020-02-17 03:27:41 +00:00
parent 8555fbfa0f
commit 613369d9e9
2 changed files with 15 additions and 4 deletions

View File

@ -6,7 +6,7 @@ from django.core.files.base import File
from django.core.cache import cache from django.core.cache import cache
from rest_framework import viewsets, views, mixins, generics, exceptions from rest_framework import viewsets, views, mixins, generics, exceptions
from rest_framework.decorators import action 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_framework.response import Response
from rest_auth.views import PasswordChangeView from rest_auth.views import PasswordChangeView
from rest_auth.registration.views import RegisterView from rest_auth.registration.views import RegisterView
@ -303,7 +303,6 @@ class PingView(views.APIView):
return Response(200) return Response(200)
class DoorViewSet(viewsets.ViewSet, List): class DoorViewSet(viewsets.ViewSet, List):
def list(self, request): def list(self, request):
cards = models.Card.objects.filter(active_status='card_active') cards = models.Card.objects.filter(active_status='card_active')
@ -328,7 +327,6 @@ class DoorViewSet(viewsets.ViewSet, List):
return Response(200) return Response(200)
class IpnView(views.APIView): class IpnView(views.APIView):
def post(self, request): def post(self, request):
try: try:
@ -339,7 +337,6 @@ class IpnView(views.APIView):
return Response(200) return Response(200)
class StatsView(views.APIView): class StatsView(views.APIView):
def get(self, request): def get(self, request):
stats_keys = utils_stats.DEFAULTS.keys() stats_keys = utils_stats.DEFAULTS.keys()
@ -369,6 +366,19 @@ class BackupView(views.APIView):
return Response(dict(url=backup_url)) 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): class RegistrationView(RegisterView):
serializer_class = serializers.MyRegisterSerializer serializer_class = serializers.MyRegisterSerializer

View File

@ -30,6 +30,7 @@ urlpatterns = [
url(r'^user/', views.UserView.as_view(), name='user'), url(r'^user/', views.UserView.as_view(), name='user'),
url(r'^ping/', views.PingView.as_view(), name='ping'), url(r'^ping/', views.PingView.as_view(), name='ping'),
url(r'^stats/', views.StatsView.as_view(), name='stats'), 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(r'^backup/', views.BackupView.as_view(), name='backup'),
url(IPN_ROUTE, views.IpnView.as_view(), name='ipn'), url(IPN_ROUTE, views.IpnView.as_view(), name='ipn'),
] ]