From 94d5c34152055be753acb38cefbd97cb0e051d56 Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 13 May 2019 21:15:38 +0200 Subject: [PATCH] [hosting/bill] Skip creating MHB for invoices that have been imported already --- hosting/management/commands/fetch_stripe_bills.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/hosting/management/commands/fetch_stripe_bills.py b/hosting/management/commands/fetch_stripe_bills.py index df30535c..20f1cbe0 100644 --- a/hosting/management/commands/fetch_stripe_bills.py +++ b/hosting/management/commands/fetch_stripe_bills.py @@ -45,7 +45,12 @@ class Command(BaseCommand): num_invoice_created = 0 for invoice in all_invoices: invoice['customer'] = user.stripecustomer - num_invoice_created += 1 if MonthlyHostingBill.create(invoice) is not None else logger.error("Did not import invoice for %s" % str(invoice)) + try: + existing_mhb = MonthlyHostingBill.objects.get(invoice_id=invoice['invoice_id']) + logger.debug("Invoice %s exists already. Not importing." % invoice['invoice_id']) + except MonthlyHostingBill.DoesNotExist as dne: + logger.debug("Invoice id %s does not exist" % invoice['invoice_id']) + num_invoice_created += 1 if MonthlyHostingBill.create(invoice) is not None else logger.error("Did not import invoice for %s" % str(invoice)) self.stdout.write( self.style.SUCCESS("Number of invoices imported = %s" % num_invoice_created) )