fixed the api

This commit is contained in:
Jonas Winkler 2018-09-05 15:25:14 +02:00
parent 8a1a736340
commit 131e1c9dd8
4 changed files with 50 additions and 8 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 .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

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

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

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

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

View File

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