Skip to content

Commit

Permalink
Changes to the mass email command.
Browse files Browse the repository at this point in the history
Better help message for the mass email command.
Replace the word destinatary with recipient.
Remove unnecessary conditions is_anonymous or is_authenticated for objects of
the User model (which are always identified and authenticated).
  • Loading branch information
juagargi committed Aug 17, 2023
1 parent 420c613 commit d4dd517
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions scionlab/management/commands/mass_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


class Command(BaseCommand):
help = 'List the unique emails of the users in alphabetical order'
help = 'List or send a message to the unique emails of the users in alphabetical order'

def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument('-a', '--action', type=str, required=True,
Expand All @@ -35,7 +35,7 @@ def add_arguments(self, parser: CommandParser) -> None:
parser.add_argument('--body', type=argparse.FileType('r'), default='-',
help='Input file to read the body of the email from')
parser.add_argument('--skip_blocks', type=int, default=0,
help='skip N initial blocks of destinataries. Useful to continue ' +
help='skip N initial blocks of recipients. Useful to continue ' +
'sending after a crash')

def handle(self, *args, **kwargs):
Expand All @@ -56,11 +56,7 @@ def list(self):
inactive = []
active = []
for u in User.objects.all():
if u.is_anonymous:
anon.append(u)
elif not u.is_authenticated:
unauth.append(u)
elif not u.is_active:
if not u.is_active:
inactive.append(u)
else:
active.append(u)
Expand All @@ -70,7 +66,6 @@ def list(self):
print(f'--------------------------- active: {len(active)}')

def send(self, **kwargs):
print('send email')
# retrieve the email parameters, or complain
if 'subject' not in kwargs or kwargs['subject'] is None:
exit('Need a subject')
Expand All @@ -88,11 +83,11 @@ def send(self, **kwargs):
)

def _send(self, subject: str, body: str, skip_blocks: int):
destinataries = self._obtain_active_emails()
block_count = (len(destinataries) - 1) // settings.MAX_EMAIL_RECIPIENTS + 1
recipients = self._obtain_active_emails()
block_count = (len(recipients) - 1) // settings.MAX_EMAIL_RECIPIENTS + 1
for b in range(skip_blocks, block_count):
# the destinataries are a subset of the total
dest = destinataries[
# the recipients are a subset of the total
dest = recipients[
b*settings.MAX_EMAIL_RECIPIENTS:
(b + 1) * settings.MAX_EMAIL_RECIPIENTS]
# prepare the email and send it
Expand Down

0 comments on commit d4dd517

Please sign in to comment.