From cebc44f2c98653a7977d8c675fd791934564a44e Mon Sep 17 00:00:00 2001 From: Daniel Quinn Date: Tue, 16 Feb 2016 09:28:34 +0000 Subject: [PATCH] API is halfway there --- src/documents/admin.py | 5 ++++- src/documents/serialisers.py | 38 ++++++++++++++++++++++++++++++++++++ src/documents/views.py | 20 ++++++++++++++++++- src/paperless/settings.py | 2 ++ src/paperless/urls.py | 16 ++++++++++++--- 5 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 src/documents/serialisers.py diff --git a/src/documents/admin.py b/src/documents/admin.py index 635b9ddf8..d3bdd3ba4 100644 --- a/src/documents/admin.py +++ b/src/documents/admin.py @@ -46,10 +46,13 @@ class DocumentAdmin(admin.ModelAdmin): } search_fields = ("sender__name", "title", "content") - list_display = ("created", "sender", "title", "tags_", "document") + list_display = ("created_", "sender", "title", "tags_", "document") list_filter = ("tags", "sender", MonthListFilter) list_per_page = 25 + def created_(self, obj): + return obj.created.date().strftime("%Y-%m-%d") + def tags_(self, obj): r = "" for tag in obj.tags.all(): diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py new file mode 100644 index 000000000..209c778a1 --- /dev/null +++ b/src/documents/serialisers.py @@ -0,0 +1,38 @@ +from rest_framework import serializers + +from .models import Sender, Tag, Document + + +class SenderSerializer(serializers.ModelSerializer): + + class Meta(object): + model = Sender + fields = ("id", "slug", "name") + + +class TagSerializer(serializers.ModelSerializer): + + class Meta(object): + model = Tag + fields = ("id", "slug", "name", "colour", "match", "matching_algorithm") + + +class DocumentSerializer(serializers.ModelSerializer): + + sender = serializers.HyperlinkedModelSerializer(read_only=True) + tags = serializers.HyperlinkedModelSerializer(read_only=True) + + class Meta(object): + model = Document + fields = ( + "id", + "sender", + "title", + "content", + "file_type", + "tags", + "created", + "modified", + "file_name", + "download_url" + ) diff --git a/src/documents/views.py b/src/documents/views.py index c92b6af09..45caf50e9 100644 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -3,10 +3,13 @@ from django.template.defaultfilters import slugify from django.views.decorators.csrf import csrf_exempt from django.views.generic import FormView, DetailView +from rest_framework.viewsets import ModelViewSet + from paperless.db import GnuPG -from .models import Document from .forms import UploadForm +from .models import Sender, Tag, Document +from .serialisers import SenderSerializer, TagSerializer, DocumentSerializer class PdfView(DetailView): @@ -52,3 +55,18 @@ class PushView(FormView): def form_invalid(self, form): return HttpResponse("0") + + +class SenderViewSet(ModelViewSet): + model = Sender + serializer_class = SenderSerializer + + +class TagViewSet(ModelViewSet): + model = Tag + serializer_class = TagSerializer + + +class DocumentViewSet(ModelViewSet): + model = Document + serializer_class = DocumentSerializer diff --git a/src/paperless/settings.py b/src/paperless/settings.py index 444989990..d31879110 100644 --- a/src/paperless/settings.py +++ b/src/paperless/settings.py @@ -44,6 +44,8 @@ INSTALLED_APPS = [ "documents", "logger", + "rest_framework", + ] MIDDLEWARE_CLASSES = [ diff --git a/src/paperless/urls.py b/src/paperless/urls.py index 060953676..d8a48995d 100644 --- a/src/paperless/urls.py +++ b/src/paperless/urls.py @@ -15,14 +15,24 @@ Including another URLconf 3. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls)) """ from django.conf import settings -from django.conf.urls import url, static +from django.conf.urls import url, static, include from django.contrib import admin -from documents.views import PdfView, PushView +from rest_framework.routers import DefaultRouter + +from documents.views import ( + PdfView, PushView, SenderViewSet, TagViewSet, DocumentViewSet) + +router = DefaultRouter() +router.register(r'senders', SenderViewSet) +router.register(r'tags', TagViewSet) +router.register(r'documents', DocumentViewSet) urlpatterns = [ + url(r"^api/auth/", include('rest_framework.urls', namespace='rest_framework')), + url(r"^api/", include(router.urls)), url(r"^fetch/(?P\d+)$", PdfView.as_view(), name="fetch"), - url(r'', admin.site.urls), + url(r"", admin.site.urls), ] + static.static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) if settings.UPLOAD_SHARED_SECRET: