django-orchestra/orchestra/contrib/bills/api.py
Santiago L f8dd7c8775 Update code using django-upgrade (target 3.2)
git ls-files -z -- '*.py' | xargs -0 django-upgrade --target-version 3.2
2024-01-26 14:05:02 +01:00

30 lines
935 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.headers.get('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)