Add ability to download receipts in PDF form
This commit is contained in:
parent
f8ae8ed6cd
commit
4c6012f756
4 changed files with 47 additions and 6 deletions
|
@ -1,15 +1,16 @@
|
|||
import logging
|
||||
import requests
|
||||
import stripe
|
||||
|
||||
from flask import (abort, redirect, request, url_for, render_template,
|
||||
make_response)
|
||||
make_response, Response)
|
||||
from flask.ext.login import login_user, UserMixin, login_required
|
||||
from flask.ext.principal import identity_changed, Identity, AnonymousIdentity
|
||||
|
||||
from data import model
|
||||
from app import app, login_manager, mixpanel
|
||||
from auth.permissions import QuayDeferredPermissionUser
|
||||
|
||||
from auth.permissions import QuayDeferredPermissionUser, AdministerOrganizationPermission
|
||||
from util.invoice import renderInvoiceToPdf
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
@ -102,6 +103,23 @@ def privacy():
|
|||
return render_template('privacy.html')
|
||||
|
||||
|
||||
@app.route('/receipt', methods=['GET'])
|
||||
def receipt():
|
||||
id = request.args.get('id')
|
||||
if id:
|
||||
invoice = stripe.Invoice.retrieve(id)
|
||||
if invoice:
|
||||
org = model.get_user_or_org_by_customer_id(invoice.customer)
|
||||
if org and org.organization:
|
||||
admin_org = AdministerOrganizationPermission(org.username)
|
||||
if admin_org.can():
|
||||
file_data = renderInvoiceToPdf(invoice, org)
|
||||
return Response(file_data,
|
||||
mimetype="application/pdf",
|
||||
headers={"Content-Disposition":
|
||||
"attachment;filename=receipt.pdf"})
|
||||
abort(404)
|
||||
|
||||
def common_login(db_user):
|
||||
if login_user(_LoginWrappedDBUser(db_user.username, db_user)):
|
||||
logger.debug('Successfully signed in as: %s' % db_user.username)
|
||||
|
|
|
@ -31,3 +31,4 @@ requests==2.0.0
|
|||
six==1.4.1
|
||||
stripe==1.9.8
|
||||
wsgiref==0.1.2
|
||||
xhtml2pdf
|
|
@ -49,12 +49,13 @@
|
|||
<th>Billing Date/Time</th>
|
||||
<th>Amount Due</th>
|
||||
<th>Status</th>
|
||||
<th></th>
|
||||
</thead>
|
||||
|
||||
<tbody class="invoice" ng-repeat="invoice in invoices">
|
||||
<tr class="invoice-title" ng-click="toggleInvoice(invoice.id)">
|
||||
<td><span class="invoice-datetime">{{ invoice.date * 1000 | date:'medium' }}</span></td>
|
||||
<td><span class="invoice-amount">{{ invoice.amount_due / 100 }}</span></td>
|
||||
<tr class="invoice-title">
|
||||
<td ng-click="toggleInvoice(invoice.id)"><span class="invoice-datetime">{{ invoice.date * 1000 | date:'medium' }}</span></td>
|
||||
<td ng-click="toggleInvoice(invoice.id)"><span class="invoice-amount">{{ invoice.amount_due / 100 }}</span></td>
|
||||
<td>
|
||||
<span class="invoice-status">
|
||||
<span class="success" ng-show="invoice.paid">Paid - Thank you!</span>
|
||||
|
@ -63,6 +64,11 @@
|
|||
<span class="pending" ng-show="!invoice.paid && !invoice.attempted">Payment pending</span>
|
||||
</span>
|
||||
</td>
|
||||
<td>
|
||||
<a ng-show="invoice.paid" href="/receipt?id={{ invoice.id }}" download="receipt.pdf" target="_new">
|
||||
<i class="fa fa-download" title="Download Receipt" bs-tooltip="tooltip.title"></i>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr ng-class="invoiceExpanded[invoice.id] ? 'in' : 'out'" class="invoice-details panel-collapse collapse">
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
from datetime import datetime
|
||||
from jinja2 import Environment, FileSystemLoader
|
||||
from xhtml2pdf import pisa
|
||||
import StringIO
|
||||
|
||||
jinja_options = {
|
||||
"loader": FileSystemLoader('util'),
|
||||
|
@ -7,6 +9,20 @@ jinja_options = {
|
|||
|
||||
env = Environment(**jinja_options)
|
||||
|
||||
|
||||
def renderInvoiceToPdf(invoice, user):
|
||||
""" Renders a nice PDF display for the given invoice. """
|
||||
sourceHtml = renderInvoiceToHtml(invoice, user)
|
||||
output = StringIO.StringIO()
|
||||
pisaStatus = pisa.CreatePDF(sourceHtml, dest=output)
|
||||
if pisaStatus.err:
|
||||
return None
|
||||
|
||||
value = output.getvalue()
|
||||
output.close()
|
||||
return value
|
||||
|
||||
|
||||
def renderInvoiceToHtml(invoice, user):
|
||||
""" Renders a nice HTML display for the given invoice. """
|
||||
def get_price(price):
|
||||
|
|
Reference in a new issue