django-orchestra/orchestra/contrib/bills/api.py

30 lines
935 B
Python
Raw Normal View History

2015-02-24 09:34:26 +00:00
from django.http import HttpResponse
2014-07-23 16:24:56 +00:00
from rest_framework import viewsets
from rest_framework.decorators import action
2014-07-23 16:24:56 +00:00
2015-04-05 18:02:36 +00:00
from orchestra.api import router, LogApiMixin
2015-04-05 10:46:24 +00:00
from orchestra.contrib.accounts.api import AccountApiMixin
2015-02-24 09:34:26 +00:00
from orchestra.utils.html import html_to_pdf
2014-07-23 16:24:56 +00:00
from .models import Bill
from .serializers import BillSerializer
2015-02-24 09:34:26 +00:00
2015-04-05 18:02:36 +00:00
class BillViewSet(LogApiMixin, AccountApiMixin, viewsets.ModelViewSet):
2015-04-23 14:34:04 +00:00
queryset = Bill.objects.all()
2014-07-23 16:24:56 +00:00
serializer_class = BillSerializer
@action(detail=True, methods=['get'])
2015-02-24 09:34:26 +00:00
def document(self, request, pk):
bill = self.get_object()
content_type = request.headers.get('accept')
2015-02-24 09:34:26 +00:00
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)