From 4deeee09957ce2811df4d9addd64f1094473ba65 Mon Sep 17 00:00:00 2001 From: Sym Roe Date: Tue, 23 May 2023 14:08:09 +0100 Subject: [PATCH] Lower case emails to filter on unique --- .../management/commands/sync_to_sendgrid.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/dc_signup_form/signup_server/management/commands/sync_to_sendgrid.py b/dc_signup_form/signup_server/management/commands/sync_to_sendgrid.py index 3ba6cd4..86e645f 100644 --- a/dc_signup_form/signup_server/management/commands/sync_to_sendgrid.py +++ b/dc_signup_form/signup_server/management/commands/sync_to_sendgrid.py @@ -1,7 +1,7 @@ import sys import time from django.core.management.base import BaseCommand -from django.db.models import Q +from django.db.models import Q, Func, F from dc_signup_form.signup_server.models import SignupQueue from dc_signup_form.signup_server.wrappers import DCSendGridWrapper @@ -16,15 +16,18 @@ def get_unique_mailing_lists(self): ) def get_new_users(self, mailing_lists): - return SignupQueue.objects.all().filter( - ~Q(email="testy.mctest@democracyclub.org.uk"), - added=False, - mailing_lists=mailing_lists, - ).distinct("email") - + return ( + SignupQueue.objects.all() + .filter( + ~Q(email="testy.mctest@democracyclub.org.uk"), + added=False, + mailing_lists=mailing_lists, + ) + .annotate(email_lower=Func(F("email"), function="lower")) + .distinct("email_lower") + ) def handle(self, *args, **kwargs): - # Assume we're going to finish sucessfully. # If any errors happen, we'll set an error code exit_code = 0