Example #1
0
def send_pdf_by_email(invoice_id):
    form = InvoiceEmail(request.form)

    if not form.validate():
        return form.errors_as_json()

    invoice = Invoice.find_by_id(invoice_id)
    if not invoice:
        return abort(404)

    message = Message(form.subject.data.encode('utf-8'), sender=(g.member.display, g.member.get_email().encode('utf-8')))
    message.add_recipient(sanitize_address((form.name.data, form.email.data)))
    message.body = form.message.data.encode('utf-8')

    invoice_key = amazons3.get_invoice(invoice)
    message.attach("Invoice_{0}.pdf".format(invoice.reference), "application/pdf", invoice_key.get_contents_as_string())

    mail = Mail()
    mail.init_app(current_app)
    mail.send(message)

    history = InvoiceHistory()
    history.description = 'Sent email to {0}'.format(message.recipients[0])
    history.pdf = invoice_key.key
    history.status = 'SENT'
    history.misc = "{0}\n\n{1}".format(message.subject, message.body)
    history.member_id = g.member.id
    history.invoice_id = invoice.id
    history.save()

    return 'sent', 200
Example #2
0
def export_as_pdf(invoice_id):
    """
    TODO :
        When generatig file, saving as invoice.id-md5(invoice.to_dict().to_string()).pdf
            Deleting old invoice.id-* before
            ~ This will ensure that only one instance of the invoice is always present
            ~ And that if changes are made, the pdf will be automatically regenerated


        Create the correct css
    """
    invoice = Invoice.find_by_id(invoice_id)
    if not invoice:
        return abort(404)

    invoice_key = amazons3.get_invoice(invoice)
    strIO = StringIO.StringIO()
    strIO.write(invoice_key.get_contents_as_string())
    strIO.seek(0)

    return send_file(strIO, mimetype='application/pdf')