A crude API is in place

This commit is contained in:
Daniel Quinn 2016-02-21 00:55:38 +00:00
parent 809fb8fa1f
commit 17d3a44952
3 changed files with 21 additions and 6 deletions

View File

@ -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

View File

@ -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

View File

@ -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<pk>\d+)$", PdfView.as_view(), name="fetch"),