django-orchestra/orchestra/apps/bills/api.py
2015-02-24 09:34:26 +00:00

30 lines
894 B
Python

from django.http import HttpResponse
from rest_framework import viewsets
from rest_framework.decorators import detail_route
from orchestra.api import router
from orchestra.apps.accounts.api import AccountApiMixin
from orchestra.utils.html import html_to_pdf
from .models import Bill
from .serializers import BillSerializer
class BillViewSet(AccountApiMixin, viewsets.ModelViewSet):
model = Bill
serializer_class = BillSerializer
@detail_route(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)