add timestamps to invoice worker

This commit is contained in:
Thomas Steen Rasmussen 2016-05-30 23:05:33 +02:00
parent 0f6d8944a0
commit 61b8c21572

View file

@ -1,6 +1,7 @@
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.core.files import File from django.core.files import File
from django.conf import settings from django.conf import settings
from django.utils import timezone
from shop.pdf import generate_pdf_letter from shop.pdf import generate_pdf_letter
from shop.email import send_invoice_email from shop.email import send_invoice_email
from shop.models import Order, Invoice from shop.models import Order, Invoice
@ -11,17 +12,20 @@ class Command(BaseCommand):
args = 'none' args = 'none'
help = 'Send out invoices that have not been sent yet' help = 'Send out invoices that have not been sent yet'
def output(self, message):
self.stdout.write('%s: %s' % (timezone.now().strftime("%Y-%m-%d %H:%M:%S"), message))
def handle(self, *args, **options): def handle(self, *args, **options):
self.stdout.write('Invoice worker running...') self.output('Invoice worker running...')
while True: while True:
# check if we need to generate any invoices # check if we need to generate any invoices
for order in Order.objects.filter(paid=True, invoice__isnull=True): for order in Order.objects.filter(paid=True, invoice__isnull=True):
# generate invoice for this Order # generate invoice for this Order
Invoice.objects.create(order=order) Invoice.objects.create(order=order)
self.stdout.write('Generated Invoice object for order %s' % order) self.output('Generated Invoice object for order %s' % order)
# check if we need to generate any pdf invoices # check if we need to generate any pdf invoices
for invoice in Invoice.objects.filter(pdf__isnull=True): for invoice in Invoice.objects.filter(pdf=''):
# put the dict with data for the pdf together # put the dict with data for the pdf together
formatdict = { formatdict = {
'invoice': invoice, 'invoice': invoice,
@ -34,14 +38,14 @@ class Command(BaseCommand):
template='invoice.html', template='invoice.html',
formatdict=formatdict, formatdict=formatdict,
) )
self.stdout.write('Generated pdf for invoice %s' % invoice) self.output('Generated pdf for invoice %s' % invoice)
except Exception as E: except Exception as E:
self.stdout.write('ERROR: Unable to generate PDF file for invoice #%s. Error: %s' % (invoice.pk, E)) self.output('ERROR: Unable to generate PDF file for invoice #%s. Error: %s' % (invoice.pk, E))
continue continue
# so, do we have a pdf? # so, do we have a pdf?
if not pdffile: if not pdffile:
self.stdout.write('ERROR: Unable to generate PDF file for invoice #%s' % invoice.pk) self.output('ERROR: Unable to generate PDF file for invoice #%s' % invoice.pk)
continue continue
# update invoice object with the file # update invoice object with the file
@ -52,11 +56,11 @@ class Command(BaseCommand):
for invoice in Invoice.objects.filter(sent_to_customer=False).exclude(pdf=''): for invoice in Invoice.objects.filter(sent_to_customer=False).exclude(pdf=''):
# send the email # send the email
if send_invoice_email(invoice=invoice): if send_invoice_email(invoice=invoice):
self.stdout.write('OK: Invoice email sent to %s' % invoice.order.user.email) self.output('OK: Invoice email sent to %s' % invoice.order.user.email)
invoice.sent_to_customer=True invoice.sent_to_customer=True
invoice.save() invoice.save()
else: else:
self.stdout.write('ERROR: Unable to send invoice email for order %s to %s' % (invoice.order.pk, invoice.order.user.email)) self.output('ERROR: Unable to send invoice email for order %s to %s' % (invoice.order.pk, invoice.order.user.email))
# pause for a bit # pause for a bit
sleep(60) sleep(60)