Ejemplo n.º 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
Ejemplo n.º 2
0
    def post_create(self, form, instance, invoice_id):
        invoice = Invoice.find_by_id(invoice_id)
        if not invoice:
            return abort(404)

        instance.invoice_id = invoice_id
        instance.created = datetime.datetime.utcnow()

        history = InvoiceHistory()
        history.created = instance.created
        history.description = 'Payment of {0} {1}'.format(instance.amount, invoice.currency)
        history.status = 'PAID'
        history.member_id = g.member.id
        history.invoice_id = invoice_id
        history.save()
Ejemplo n.º 3
0
    def post_update(self, form, instance, invoice_id):
        invoice = Invoice.find_by_id(invoice_id)
        if not invoice:
            return abort(404)

        InvoiceHistory.query \
            .filter(InvoiceHistory.status == 'PAID') \
            .filter(InvoiceHistory.invoice_id == invoice_id) \
            .filter(InvoiceHistory.created == instance.created) \
            .delete()

        history = InvoiceHistory()
        history.created = instance.created
        history.description = 'Payment of {0} {1}'.format(instance.amount, invoice.currency)
        history.status = 'PAID'
        history.member_id = g.member.id
        history.invoice_id = invoice_id
        history.save()