Basic support for metadata added. Fixes 404 for actions on invalid document ids.

This commit is contained in:
Jonas Winkler 2020-11-25 17:16:04 +01:00
parent 1987dccf48
commit 97639508cb
2 changed files with 18 additions and 9 deletions

View File

@ -93,14 +93,11 @@ class DocumentSerializer(serializers.ModelSerializer):
"document_type_id",
"title",
"content",
"mime_type",
"tags",
"tags_id",
"checksum",
"created",
"modified",
"added",
"file_name",
"archive_serial_number"
)

View File

@ -149,13 +149,25 @@ class DocumentViewSet(RetrieveModelMixin,
else:
return HttpResponseBadRequest(str(form.errors))
@action(methods=['get'], detail=True)
def metadata(self, request, pk=None):
try:
doc = Document.objects.get(pk=pk)
return Response({
"paperless__checksum": doc.checksum,
"paperless__mime_type": doc.mime_type,
"paperless__filename": doc.filename,
})
except Document.DoesNotExist:
raise Http404()
@action(methods=['get'], detail=True)
def preview(self, request, pk=None):
try:
response = self.file_response(pk, "inline")
return response
except FileNotFoundError:
raise Http404("Document source file does not exist")
except (FileNotFoundError, Document.DoesNotExist):
raise Http404()
@action(methods=['get'], detail=True)
@cache_control(public=False, max_age=315360000)
@ -163,15 +175,15 @@ class DocumentViewSet(RetrieveModelMixin,
try:
return HttpResponse(Document.objects.get(id=pk).thumbnail_file,
content_type='image/png')
except FileNotFoundError:
raise Http404("Document thumbnail does not exist")
except (FileNotFoundError, Document.DoesNotExist):
raise Http404()
@action(methods=['get'], detail=True)
def download(self, request, pk=None):
try:
return self.file_response(pk, "attachment")
except FileNotFoundError:
raise Http404("Document source file does not exist")
except (FileNotFoundError, Document.DoesNotExist):
raise Http404()
class LogViewSet(ReadOnlyModelViewSet):