diff --git a/src/documents/filters.py b/src/documents/filters.py old mode 100644 new mode 100755 index c3c60ccc7..77f14928b --- a/src/documents/filters.py +++ b/src/documents/filters.py @@ -1,6 +1,6 @@ from django_filters.rest_framework import CharFilter, FilterSet -from .models import Correspondent, Document, Tag +from .models import Correspondent, Document, Tag, DocumentType 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): CHAR_KWARGS = { @@ -46,6 +59,8 @@ class DocumentFilterSet(FilterSet): correspondent__slug = CharFilter(name="correspondent__slug", **CHAR_KWARGS) tags__name = CharFilter(name="tags__name", **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): model = Document diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py old mode 100644 new mode 100755 index 504999276..99e71917f --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -1,6 +1,6 @@ from rest_framework import serializers -from .models import Correspondent, Tag, Document, Log +from .models import Correspondent, Tag, Document, Log, DocumentType class CorrespondentSerializer(serializers.HyperlinkedModelSerializer): @@ -10,6 +10,13 @@ class CorrespondentSerializer(serializers.HyperlinkedModelSerializer): fields = ("id", "slug", "name") +class DocumentTypeSerializer(serializers.HyperlinkedModelSerializer): + + class Meta(object): + model = DocumentType + fields = ("id", "slug", "name") + + class TagSerializer(serializers.HyperlinkedModelSerializer): class Meta(object): @@ -28,17 +35,25 @@ class TagsField(serializers.HyperlinkedRelatedField): return Tag.objects.all() +class DocumentTypeField(serializers.HyperlinkedRelatedField): + def get_queryset(self): + return DocumentType.objects.all() + + class DocumentSerializer(serializers.ModelSerializer): correspondent = CorrespondentField( view_name="drf:correspondent-detail", allow_null=True) tags = TagsField(view_name="drf:tag-detail", many=True) + document_type = DocumentTypeField( + view_name="drf:documenttype-detail", allow_null=True) class Meta(object): model = Document fields = ( "id", "correspondent", + "document_type", "title", "content", "file_type", diff --git a/src/documents/views.py b/src/documents/views.py old mode 100644 new mode 100755 index e297e0984..be1b4a425 --- a/src/documents/views.py +++ b/src/documents/views.py @@ -18,15 +18,15 @@ from rest_framework.viewsets import ( ReadOnlyModelViewSet ) -from .filters import CorrespondentFilterSet, DocumentFilterSet, TagFilterSet +from .filters import CorrespondentFilterSet, DocumentFilterSet, TagFilterSet, DocumentTypeFilterSet from .forms import UploadForm -from .models import Correspondent, Document, Log, Tag +from .models import Correspondent, Document, Log, Tag, DocumentType from .serialisers import ( CorrespondentSerializer, DocumentSerializer, LogSerializer, - TagSerializer -) + TagSerializer, + DocumentTypeSerializer) class IndexView(TemplateView): @@ -108,6 +108,17 @@ class TagViewSet(ModelViewSet): 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, UpdateModelMixin, DestroyModelMixin, diff --git a/src/paperless/urls.py b/src/paperless/urls.py index 6f50ac644..51e2f75e6 100755 --- a/src/paperless/urls.py +++ b/src/paperless/urls.py @@ -12,12 +12,13 @@ from documents.views import ( FetchView, LogViewSet, PushView, - TagViewSet -) + TagViewSet, + DocumentTypeViewSet) from reminders.views import ReminderViewSet router = DefaultRouter() router.register(r"correspondents", CorrespondentViewSet) +router.register(r"document_types", DocumentTypeViewSet) router.register(r"documents", DocumentViewSet) router.register(r"logs", LogViewSet) router.register(r"reminders", ReminderViewSet)