Created
January 30, 2014 23:16
-
-
Save roadsideseb/8722203 to your computer and use it in GitHub Desktop.
Packing slips for Oscar using PISA in the dashboard
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from xhtml2pdf import pisa | |
from xhtml2pdf.pdf import pisaPDF | |
class OrderListView(ListView, BulkEditMixin): | |
... | |
actions = ( | |
'generate_packing_slips', | |
... | |
) | |
... | |
def generate_packing_slips(self, request, orders): | |
template = loader.get_template(self.packing_slip_template) | |
main_pdf = pisaPDF() | |
for order in orders: | |
voucher_codes = [] | |
for discount in order.discounts.all(): | |
if discount.voucher_code: | |
voucher_codes.append(discount.voucher_code) | |
context = RequestContext(request, { | |
'order': order, | |
'STATIC_ROOT': settings.STATIC_ROOT, | |
'voucher_codes': voucher_codes, | |
}) | |
html = template.render(context) | |
result = StringIO() | |
order_pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")), result) | |
if order_pdf.err: | |
messages.error( | |
self.request, | |
_("An problem occured trying to generate the packing slip for " | |
"order #%s") % order.number, | |
) | |
else: | |
main_pdf.addDocument(order_pdf) | |
response = HttpResponse(main_pdf.getvalue(), mimetype='application/pdf') | |
filename = self.get_packing_slip_filename(orders) | |
response['Content-Disposition'] = 'attachment; filename=%s' % filename | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this work in python 3?
Also how to define packing_slip_template and get_packing_slip_filename?