From 166e5b843d02c28472fc4c324331a58e99cd7cd4 Mon Sep 17 00:00:00 2001 From: Michael Collins <15347726+michaeljcollinsuk@users.noreply.github.com> Date: Mon, 11 Sep 2023 09:50:21 +0100 Subject: [PATCH] ANPL-1540 add management command to write email addresses for a auth0 group to a CSV file (#1201) Created afeter Susan Baron owner of Segmentation Tool requested all user emails --- .../commands/get_customer_emails_csv.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 controlpanel/cli/management/commands/get_customer_emails_csv.py diff --git a/controlpanel/cli/management/commands/get_customer_emails_csv.py b/controlpanel/cli/management/commands/get_customer_emails_csv.py new file mode 100644 index 000000000..01137c804 --- /dev/null +++ b/controlpanel/cli/management/commands/get_customer_emails_csv.py @@ -0,0 +1,31 @@ +# Standard library +import csv +from datetime import datetime + +# Third-party +from django.core.management import BaseCommand + +# First-party/Local +from controlpanel.api import auth0 + + +class Command(BaseCommand): + help = "Writes a CSV with all customer emails for an auth0 group" + + def add_arguments(self, parser): + parser.add_argument( + "group_name", + type=str, + help="input: The auth0 group name to get customers emails for" + ) + + def handle(self, *args, **options): + group_name = options["group_name"] + auth_instance = auth0.ExtendedAuth0() + group_id = auth_instance.groups.get_group_id(group_name) + timestamp = datetime.now().strftime("%d-%m-%Y_%H%M") + with open(f"{group_name}_customers_{timestamp}.csv", "w") as f: + writer = csv.writer(f) + writer.writerow(["Email"]) + for customer in auth_instance.groups.get_group_members(group_id): + writer.writerow([customer["email"]])