Merge branch 'dev' into machine-learning

This commit is contained in:
Jonas Winkler 2018-09-05 15:26:39 +02:00
commit 8eeded95c4
5 changed files with 54 additions and 9 deletions

17
src/documents/filters.py Normal file → Executable file
View File

@ -1,6 +1,6 @@
from django_filters.rest_framework import CharFilter, FilterSet from django_filters.rest_framework import CharFilter, FilterSet
from .models import Correspondent, Document, Tag from .models import Correspondent, Document, Tag, DocumentType
class CorrespondentFilterSet(FilterSet): class CorrespondentFilterSet(FilterSet):
@ -29,6 +29,19 @@ class TagFilterSet(FilterSet):
} }
class DocumentTypeFilterSet(FilterSet):
class Meta(object):
model = DocumentType
fields = {
"name": [
"startswith", "endswith", "contains",
"istartswith", "iendswith", "icontains"
],
"slug": ["istartswith", "iendswith", "icontains"]
}
class DocumentFilterSet(FilterSet): class DocumentFilterSet(FilterSet):
CHAR_KWARGS = { CHAR_KWARGS = {
@ -46,6 +59,8 @@ class DocumentFilterSet(FilterSet):
correspondent__slug = CharFilter(name="correspondent__slug", **CHAR_KWARGS) correspondent__slug = CharFilter(name="correspondent__slug", **CHAR_KWARGS)
tags__name = CharFilter(name="tags__name", **CHAR_KWARGS) tags__name = CharFilter(name="tags__name", **CHAR_KWARGS)
tags__slug = CharFilter(name="tags__slug", **CHAR_KWARGS) tags__slug = CharFilter(name="tags__slug", **CHAR_KWARGS)
document_type__name = CharFilter(name="document_type__name", **CHAR_KWARGS)
document_type__slug = CharFilter(name="document_type__slug", **CHAR_KWARGS)
class Meta(object): class Meta(object):
model = Document model = Document

5
src/documents/management/commands/document_exporter.py Normal file → Executable file
View File

@ -6,7 +6,7 @@ import shutil
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django.core import serializers from django.core import serializers
from documents.models import Document, Correspondent, Tag from documents.models import Document, Correspondent, Tag, DocumentType
from paperless.db import GnuPG from paperless.db import GnuPG
from ...mixins import Renderable from ...mixins import Renderable
@ -91,6 +91,9 @@ class Command(Renderable, BaseCommand):
manifest += json.loads(serializers.serialize( manifest += json.loads(serializers.serialize(
"json", Tag.objects.all())) "json", Tag.objects.all()))
manifest += json.loads(serializers.serialize(
"json", DocumentType.objects.all()))
with open(os.path.join(self.target, "manifest.json"), "w") as f: with open(os.path.join(self.target, "manifest.json"), "w") as f:
json.dump(manifest, f, indent=2) json.dump(manifest, f, indent=2)

View File

@ -1,6 +1,6 @@
from rest_framework import serializers from rest_framework import serializers
from .models import Correspondent, Tag, Document, Log from .models import Correspondent, Tag, Document, Log, DocumentType
class CorrespondentSerializer(serializers.HyperlinkedModelSerializer): class CorrespondentSerializer(serializers.HyperlinkedModelSerializer):
@ -10,6 +10,13 @@ class CorrespondentSerializer(serializers.HyperlinkedModelSerializer):
fields = ("id", "slug", "name", "automatic_classification") fields = ("id", "slug", "name", "automatic_classification")
class DocumentTypeSerializer(serializers.HyperlinkedModelSerializer):
class Meta(object):
model = DocumentType
fields = ("id", "slug", "name")
class TagSerializer(serializers.HyperlinkedModelSerializer): class TagSerializer(serializers.HyperlinkedModelSerializer):
class Meta(object): class Meta(object):
@ -28,17 +35,25 @@ class TagsField(serializers.HyperlinkedRelatedField):
return Tag.objects.all() return Tag.objects.all()
class DocumentTypeField(serializers.HyperlinkedRelatedField):
def get_queryset(self):
return DocumentType.objects.all()
class DocumentSerializer(serializers.ModelSerializer): class DocumentSerializer(serializers.ModelSerializer):
correspondent = CorrespondentField( correspondent = CorrespondentField(
view_name="drf:correspondent-detail", allow_null=True) view_name="drf:correspondent-detail", allow_null=True)
tags = TagsField(view_name="drf:tag-detail", many=True) tags = TagsField(view_name="drf:tag-detail", many=True)
document_type = DocumentTypeField(
view_name="drf:documenttype-detail", allow_null=True)
class Meta(object): class Meta(object):
model = Document model = Document
fields = ( fields = (
"id", "id",
"correspondent", "correspondent",
"document_type",
"title", "title",
"content", "content",
"file_type", "file_type",

19
src/documents/views.py Normal file → Executable file
View File

@ -18,15 +18,15 @@ from rest_framework.viewsets import (
ReadOnlyModelViewSet ReadOnlyModelViewSet
) )
from .filters import CorrespondentFilterSet, DocumentFilterSet, TagFilterSet from .filters import CorrespondentFilterSet, DocumentFilterSet, TagFilterSet, DocumentTypeFilterSet
from .forms import UploadForm from .forms import UploadForm
from .models import Correspondent, Document, Log, Tag from .models import Correspondent, Document, Log, Tag, DocumentType
from .serialisers import ( from .serialisers import (
CorrespondentSerializer, CorrespondentSerializer,
DocumentSerializer, DocumentSerializer,
LogSerializer, LogSerializer,
TagSerializer TagSerializer,
) DocumentTypeSerializer)
class IndexView(TemplateView): class IndexView(TemplateView):
@ -108,6 +108,17 @@ class TagViewSet(ModelViewSet):
ordering_fields = ("name", "slug") ordering_fields = ("name", "slug")
class DocumentTypeViewSet(ModelViewSet):
model = DocumentType
queryset = DocumentType.objects.all()
serializer_class = DocumentTypeSerializer
pagination_class = StandardPagination
permission_classes = (IsAuthenticated,)
filter_backends = (DjangoFilterBackend, OrderingFilter)
filter_class = DocumentTypeFilterSet
ordering_fields = ("name", "slug")
class DocumentViewSet(RetrieveModelMixin, class DocumentViewSet(RetrieveModelMixin,
UpdateModelMixin, UpdateModelMixin,
DestroyModelMixin, DestroyModelMixin,

View File

@ -12,12 +12,13 @@ from documents.views import (
FetchView, FetchView,
LogViewSet, LogViewSet,
PushView, PushView,
TagViewSet TagViewSet,
) DocumentTypeViewSet)
from reminders.views import ReminderViewSet from reminders.views import ReminderViewSet
router = DefaultRouter() router = DefaultRouter()
router.register(r"correspondents", CorrespondentViewSet) router.register(r"correspondents", CorrespondentViewSet)
router.register(r"document_types", DocumentTypeViewSet)
router.register(r"documents", DocumentViewSet) router.register(r"documents", DocumentViewSet)
router.register(r"logs", LogViewSet) router.register(r"logs", LogViewSet)
router.register(r"reminders", ReminderViewSet) router.register(r"reminders", ReminderViewSet)