API is halfway there

This commit is contained in:
Daniel Quinn 2016-02-16 09:28:34 +00:00
parent bbe7a02b4d
commit cebc44f2c9
5 changed files with 76 additions and 5 deletions

View File

@ -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():

View File

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

View File

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

View File

@ -44,6 +44,8 @@ INSTALLED_APPS = [
"documents",
"logger",
"rest_framework",
]
MIDDLEWARE_CLASSES = [

View File

@ -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<pk>\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: