from django.shortcuts import get_object_or_404 from django.http import FileResponse, Http404 from rest_framework.response import Response from rest_framework.decorators import api_view, permission_classes from rest_framework.permissions import AllowAny from .models import Paper, PaperAbstract from .serializers import PaperListSerializer from apps.utils.viewsets import CustomGenericViewSet, CustomListModelMixin import os @api_view(['GET']) @permission_classes([AllowAny]) def paper_pdf_view(request, pk): paper = get_object_or_404(Paper, pk=pk) if not paper.has_fulltext_pdf: raise Http404("PDF not available") pdf_path = paper.init_paper_path("pdf") if not os.path.isfile(pdf_path): raise Http404("PDF file not found on disk") safe_doi = paper.doi.replace("/", "_") response = FileResponse( open(pdf_path, 'rb'), content_type='application/pdf', ) response['Content-Disposition'] = f'inline; filename="{safe_doi}.pdf"' return response # Create your views here. class PaperViewSet(CustomGenericViewSet, CustomListModelMixin): queryset = Paper.objects.all() serializer_class = PaperListSerializer filterset_fields = ["publication_year", "type", "fetch_status", "has_abstract", "has_fulltext", "doi"] search_fields = ['title', 'first_author', 'first_author_institution'] ordering = ["-publication_date", "-create_time"] def get_authenticators(self): if self.request.method == 'GET': return [] return super().get_authenticators() def get_permissions(self): if self.request.method == 'GET': return [AllowAny()] return super().get_permissions()