From 17d3a449525248e77327f22e1b141c76c2adfabc Mon Sep 17 00:00:00 2001 From: Daniel Quinn Date: Sun, 21 Feb 2016 00:55:38 +0000 Subject: [PATCH] A crude API is in place --- src/documents/serialisers.py | 10 ++++++---- src/documents/views.py | 13 +++++++++++++ src/paperless/urls.py | 4 ++-- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index f23a482c6..345fa166d 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -3,14 +3,14 @@ from rest_framework import serializers from .models import Sender, Tag, Document -class SenderSerializer(serializers.ModelSerializer): +class SenderSerializer(serializers.HyperlinkedModelSerializer): class Meta(object): model = Sender fields = ("id", "slug", "name") -class TagSerializer(serializers.ModelSerializer): +class TagSerializer(serializers.HyperlinkedModelSerializer): class Meta(object): model = Tag @@ -20,8 +20,10 @@ class TagSerializer(serializers.ModelSerializer): class DocumentSerializer(serializers.ModelSerializer): - sender = serializers.HyperlinkedModelSerializer(read_only=True) - tags = serializers.HyperlinkedModelSerializer(read_only=True) + sender = serializers.HyperlinkedRelatedField( + read_only=True, view_name="drf:sender-detail", allow_null=True) + tags = serializers.HyperlinkedRelatedField( + read_only=True, view_name="drf:tag-detail", many=True) class Meta(object): model = Document diff --git a/src/documents/views.py b/src/documents/views.py index 45caf50e9..bcce6f677 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -3,6 +3,7 @@ from django.template.defaultfilters import slugify from django.views.decorators.csrf import csrf_exempt from django.views.generic import FormView, DetailView +from rest_framework.pagination import PageNumberPagination from rest_framework.viewsets import ModelViewSet from paperless.db import GnuPG @@ -57,16 +58,28 @@ class PushView(FormView): return HttpResponse("0") +class StandardPagination(PageNumberPagination): + page_size = 25 + page_size_query_param = "page-size" + max_page_size = 100000 + + class SenderViewSet(ModelViewSet): model = Sender + queryset = Sender.objects.all() serializer_class = SenderSerializer + pagination_class = StandardPagination class TagViewSet(ModelViewSet): model = Tag + queryset = Tag.objects.all() serializer_class = TagSerializer + pagination_class = StandardPagination class DocumentViewSet(ModelViewSet): model = Document + queryset = Document.objects.all() serializer_class = DocumentSerializer + pagination_class = StandardPagination diff --git a/src/paperless/urls.py b/src/paperless/urls.py index 5803a6685..b7ffe17dc 100644 --- a/src/paperless/urls.py +++ b/src/paperless/urls.py @@ -33,9 +33,9 @@ urlpatterns = [ # API url( r"^api/auth/", - include('rest_framework.urls', namespace='rest_framework') + include('rest_framework.urls', namespace="rest_framework") ), - url(r"^api/", include(router.urls)), + url(r"^api/", include(router.urls, namespace="drf")), # File downloads url(r"^fetch/(?P\d+)$", PdfView.as_view(), name="fetch"),