This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/util/invoice.py

53 lines
1.3 KiB
Python
Raw Normal View History

from datetime import datetime
from jinja2 import Environment, FileSystemLoader
from xhtml2pdf import pisa
import StringIO
jinja_options = {
"loader": FileSystemLoader('util'),
}
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
2014-11-24 21:07:38 +00:00
value = output.getvalue()
output.close()
return value
def renderInvoiceToHtml(invoice, user):
""" Renders a nice HTML display for the given invoice. """
def get_price(price):
if not price:
return '$0'
2013-11-15 20:31:05 +00:00
return '$' + '{0:.2f}'.format(float(price) / 100)
def get_range(line):
if line.period and line.period.start and line.period.end:
2014-11-24 21:07:38 +00:00
return ': ' + format_date(line.period.start) + ' - ' + format_date(line.period.end)
return ''
def format_date(timestamp):
return datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d')
data = {
'user': user.username,
'invoice': invoice,
'invoice_date': format_date(invoice.date),
'getPrice': get_price,
'getRange': get_range
2014-11-24 21:07:38 +00:00
}
template = env.get_template('invoice.tmpl')
rendered = template.render(data)
return rendered