django-orchestra/orchestra/contrib/bills/api.py
Santiago L f7627926cb Replace detail_route with action decorator
DRF 3.10.0 deprecates the detail_route decorator in favor of action
2021-05-06 13:08:21 +02:00

30 lines
937 B
Python

from django.http import HttpResponse
from rest_framework import viewsets
from rest_framework.decorators import action
from orchestra.api import router, LogApiMixin
from orchestra.contrib.accounts.api import AccountApiMixin
from orchestra.utils.html import html_to_pdf
from .models import Bill
from .serializers import BillSerializer
class BillViewSet(LogApiMixin, AccountApiMixin, viewsets.ModelViewSet):
queryset = Bill.objects.all()
serializer_class = BillSerializer
@action(detail=True, methods=['get'])
def document(self, request, pk):
bill = self.get_object()
content_type = request.META.get('HTTP_ACCEPT')
if content_type == 'application/pdf':
pdf = html_to_pdf(bill.html or bill.render())
return HttpResponse(pdf, content_type='application/pdf')
else:
return HttpResponse(bill.html or bill.render())
router.register('bills', BillViewSet)