From 70f0fed63f05a7ddc1125965e5b5c14279eae9e4 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 14 Dec 2019 10:18:39 +0530 Subject: [PATCH 1/4] Add all_customers management command --- .../management/commands/all_customers.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 datacenterlight/management/commands/all_customers.py diff --git a/datacenterlight/management/commands/all_customers.py b/datacenterlight/management/commands/all_customers.py new file mode 100644 index 00000000..d529d28f --- /dev/null +++ b/datacenterlight/management/commands/all_customers.py @@ -0,0 +1,36 @@ +import json +import logging +import sys + +from django.core.management.base import BaseCommand +from membership.models import CustomUser +from hosting.models import ( + HostingOrder, VMDetail, UserCardDetail, UserHostingKey +) +logger = logging.getLogger(__name__) + + +class Command(BaseCommand): + help = '''Dumps the email addresses of all customers who have a VM''' + + def add_arguments(self, parser): + parser.add_argument('-a', '--all_registered', action='store_true', + help='All registered users') + + def handle(self, *args, **options): + all_registered = options['all_registered'] + all_customers_set = set() + if all_registered: + all_customers = CustomUser.objects.filter( + is_admin=False, validated=True + ) + for customer in all_customers: + all_customers_set.add(customer.email) + print(customer.email) + else: + all_hosting_orders = HostingOrder.objects.all() + for order in all_hosting_orders: + all_customers_set.add(order.customer.user.email) + print(order.customer.user.email) + + print("Total customers = %s" % len(all_customers_set)) From 991908c37ed51ff60beffe4f490e526ed5c8604d Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 14 Dec 2019 10:52:20 +0530 Subject: [PATCH 2/4] Fix obtianing active customers only --- datacenterlight/management/commands/all_customers.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/datacenterlight/management/commands/all_customers.py b/datacenterlight/management/commands/all_customers.py index d529d28f..2a2d6573 100644 --- a/datacenterlight/management/commands/all_customers.py +++ b/datacenterlight/management/commands/all_customers.py @@ -26,11 +26,13 @@ class Command(BaseCommand): ) for customer in all_customers: all_customers_set.add(customer.email) - print(customer.email) else: - all_hosting_orders = HostingOrder.objects.all() + all_hosting_orders = HostingOrder.objects.filter() + running_vm_details = VMDetail.objects.filter(terminated_at=None) + running_vm_ids = [rvm.vm_id for rvm in running_vm_details] for order in all_hosting_orders: - all_customers_set.add(order.customer.user.email) - print(order.customer.user.email) - + if order.vm_id in running_vm_ids: + all_customers_set.add(order.customer.user.email) + for cu in all_customers_set: + print(cu) print("Total customers = %s" % len(all_customers_set)) From 7442cbd9ca5c629a2724fc9c58356df400499f61 Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 14 Dec 2019 10:56:14 +0530 Subject: [PATCH 3/4] Print appropriate message --- datacenterlight/management/commands/all_customers.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/datacenterlight/management/commands/all_customers.py b/datacenterlight/management/commands/all_customers.py index 2a2d6573..93b89373 100644 --- a/datacenterlight/management/commands/all_customers.py +++ b/datacenterlight/management/commands/all_customers.py @@ -35,4 +35,7 @@ class Command(BaseCommand): all_customers_set.add(order.customer.user.email) for cu in all_customers_set: print(cu) - print("Total customers = %s" % len(all_customers_set)) + if all_registered: + print("All registered users = %s" % len(all_customers_set)) + else: + print("Total active customers = %s" % len(all_customers_set)) From 6666e40ec4430d8bd50d8dce632b5024bbca715a Mon Sep 17 00:00:00 2001 From: PCoder Date: Sat, 14 Dec 2019 11:00:37 +0530 Subject: [PATCH 4/4] Remove unused imports --- datacenterlight/management/commands/all_customers.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/datacenterlight/management/commands/all_customers.py b/datacenterlight/management/commands/all_customers.py index 93b89373..adbd8552 100644 --- a/datacenterlight/management/commands/all_customers.py +++ b/datacenterlight/management/commands/all_customers.py @@ -1,12 +1,12 @@ -import json import logging -import sys from django.core.management.base import BaseCommand -from membership.models import CustomUser + from hosting.models import ( - HostingOrder, VMDetail, UserCardDetail, UserHostingKey + HostingOrder, VMDetail ) +from membership.models import CustomUser + logger = logging.getLogger(__name__)