|
| 1 | +# Copyright 2023 ETH Zurich |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | +import sys |
| 17 | + |
| 18 | +from typing import List |
| 19 | +from django.conf import settings |
| 20 | +from django.core.management.base import BaseCommand, CommandParser |
| 21 | +from django.core.mail import EmailMessage |
| 22 | + |
| 23 | +from scionlab.models.user import User |
| 24 | + |
| 25 | + |
| 26 | +class Command(BaseCommand): |
| 27 | + help = 'List or send a message to the unique emails of the users in alphabetical order' |
| 28 | + |
| 29 | + def add_arguments(self, parser: CommandParser) -> None: |
| 30 | + parser.add_argument('-a', '--action', type=str, required=True, |
| 31 | + choices=['list', 'send'], |
| 32 | + help='Action of the command: list or send') |
| 33 | + parser.add_argument('--subject', type=str, required=False, |
| 34 | + help='Subject of the email to send') |
| 35 | + parser.add_argument('--body', type=argparse.FileType('r'), default='-', |
| 36 | + help='Input file to read the body of the email from') |
| 37 | + parser.add_argument('--skip_blocks', type=int, default=0, |
| 38 | + help='skip N initial blocks of recipients. Useful to continue ' + |
| 39 | + 'sending after a crash') |
| 40 | + |
| 41 | + def handle(self, *args, **kwargs): |
| 42 | + action = kwargs['action'] |
| 43 | + if action == 'list': |
| 44 | + self.list() |
| 45 | + elif action == 'send': |
| 46 | + self.send(**kwargs) |
| 47 | + |
| 48 | + def list(self): |
| 49 | + # print active users emails: |
| 50 | + for email in self._obtain_active_emails(): |
| 51 | + print(email) |
| 52 | + |
| 53 | + # report user stats: |
| 54 | + inactive = [] |
| 55 | + active = [] |
| 56 | + for u in User.objects.all(): |
| 57 | + if not u.is_active: |
| 58 | + inactive.append(u) |
| 59 | + else: |
| 60 | + active.append(u) |
| 61 | + print(f'--------------------------- inactive: {len(inactive)}') |
| 62 | + print(f'--------------------------- active: {len(active)}') |
| 63 | + |
| 64 | + def send(self, **kwargs): |
| 65 | + # retrieve the email parameters, or complain |
| 66 | + if 'subject' not in kwargs or kwargs['subject'] is None: |
| 67 | + exit('Need a subject') |
| 68 | + subject = kwargs['subject'] |
| 69 | + if kwargs['body'] == sys.stdin: |
| 70 | + print('Type the body of the email, end with ^D') |
| 71 | + with kwargs['body'] as f: |
| 72 | + body = f.read() |
| 73 | + skip_blocks = kwargs['skip_blocks'] |
| 74 | + |
| 75 | + self._send( |
| 76 | + subject=subject, |
| 77 | + body=body, |
| 78 | + skip_blocks=skip_blocks, |
| 79 | + ) |
| 80 | + |
| 81 | + def _send(self, subject: str, body: str, skip_blocks: int): |
| 82 | + recipients = self._obtain_active_emails() |
| 83 | + block_count = (len(recipients) - 1) // settings.MAX_EMAIL_RECIPIENTS + 1 |
| 84 | + for b in range(skip_blocks, block_count): |
| 85 | + # the recipients are a subset of the total |
| 86 | + dest = recipients[ |
| 87 | + b*settings.MAX_EMAIL_RECIPIENTS: |
| 88 | + (b + 1) * settings.MAX_EMAIL_RECIPIENTS] |
| 89 | + # prepare the email and send it |
| 90 | + msg = EmailMessage( |
| 91 | + subject, |
| 92 | + body, |
| 93 | + settings.DEFAULT_FROM_EMAIL, # From |
| 94 | + [], # To |
| 95 | + bcc=dest, |
| 96 | + ) |
| 97 | + msg.send() |
| 98 | + print(f'sent {b+1}/{block_count} -> {dest}') |
| 99 | + print('done') |
| 100 | + |
| 101 | + def _obtain_active_emails(self) -> List[str]: |
| 102 | + emails = User.objects.filter( |
| 103 | + is_active=True, |
| 104 | + ).values_list('email', flat=True).order_by('email').distinct() |
| 105 | + return emails |
| 106 | + |
| 107 | + |
| 108 | +def exit(msg: str): |
| 109 | + print(msg) |
| 110 | + sys.exit(1) |
0 commit comments