From 3f01145cd1f890360506c21d5737710ccc90bbdb Mon Sep 17 00:00:00 2001 From: PCoder Date: Mon, 6 May 2019 08:30:50 +0200 Subject: [PATCH] Add additional None checks --- .../management/commands/deleteuser.py | 74 +++++++++++++++++-- 1 file changed, 67 insertions(+), 7 deletions(-) diff --git a/datacenterlight/management/commands/deleteuser.py b/datacenterlight/management/commands/deleteuser.py index e3be76f1..cc1cd477 100644 --- a/datacenterlight/management/commands/deleteuser.py +++ b/datacenterlight/management/commands/deleteuser.py @@ -86,12 +86,39 @@ class Command(BaseCommand): vm_ids = [] for order in hosting_orders: vm_ids.append(order.vm_id) + # Delete Billing Address - order.billing_address.delete() + if order.billing_address is not None: + order.billing_address.delete() + logger.debug( + "Billing Address {} associated with {} deleted" + "".format(order.billing_address.id, email) + ) + else: + logger.error( + "Error while deleting the billing_address") + # Delete Order Detail - order.order_detail.delete() + if order.order_detail is not None: + order.order_detail.delete() + logger.debug( + "Order Detail {} associated with {} deleted" + "".format(order.order_detail.id, email) + ) + else: + logger.error( + "Error while deleting the order_detail. None") + # Delete order - order.delete() + if order is not None: + order.delete() + logger.debug( + "Order {} associated with {} deleted" + "".format(order.id, email) + ) + else: + logger.error( + "Error while deleting the Order") hosting_bills = HostingBill.objects.filter( customer=stripe_customer.id @@ -99,20 +126,53 @@ class Command(BaseCommand): # delete hosting bills for bill in hosting_bills: - bill.billing_address.delete() - bill.delete() + if bill.billing_address is not None: + bill.billing_address.delete() + logger.debug( + "HostingBills billing address {} associated with {} deleted" + "".format(bill.billing_address.id, email) + ) + else: + logger.error( + "Error while deleting the HostingBill's Billing address") + + if bill is not None: + bill.delete() + logger.debug( + "HostingBill {} associated with {} deleted" + "".format(bill.id, email) + ) + else: + logger.error( + "Error while deleting the HostingBill") # delete VMDetail for vm_id in vm_ids: vm_detail = VMDetail.objects.get(vm_id=vm_id) - vm_detail.delete() + if vm_detail is not None: + vm_detail.delete() + logger.debug( + "vm_detail {} associated with {} deleted" + "".format(vm_detail.id, email) + ) + else: + logger.error( + "Error while deleting the vm_detail") # delete UserCardDetail ucds = UserCardDetail.objects.filter( stripe_customer=stripe_customer ) for ucd in ucds: - ucd.delete() + if ucd is not None: + ucd.delete() + logger.debug( + "User Card Detail {} associated with {} deleted" + "".format(ucd.id, email) + ) + else: + logger.error( + "Error while deleting the User Card Detail") # delete UserHostingKey uhks = UserHostingKey.objects.filter(