Skip to content

Commit

Permalink
Improve script resumability, update template
Browse files Browse the repository at this point in the history
  • Loading branch information
mfraezz committed Jan 6, 2025
1 parent 010c1ce commit bf3c7d8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
19 changes: 10 additions & 9 deletions osf/management/commands/email_all_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

OFFSET = 500000

def email_all_users(email_template, dry_run=False, ids=None, run=0, offset=OFFSET):
def email_all_users(email_template, dry_run=False, ids=None, start_id=0, offset=OFFSET):

if ids:
active_users = OSFUser.objects.filter(id__in=ids)
else:
lower_bound = run * offset
upper_bound = (run + 1) * offset
lower_bound = start_id
upper_bound = start_id + offset
base_query = OSFUser.objects.filter(date_confirmed__isnull=False, deleted=None).exclude(date_disabled__isnull=False).exclude(is_active=False)
active_users = base_query.filter(id__gt=lower_bound, id__lte=upper_bound).order_by('id')

Expand All @@ -42,11 +42,12 @@ def email_all_users(email_template, dry_run=False, ids=None, run=0, offset=OFFSE

total_sent = 0
for user in active_users.iterator():
logger.info(f'Sending email to {user.id}')
try:
mails.send_mail(
to_addr=user.email,
mail=template,
fullname=user.fullname,
given_name=user.given_name or user.fullname,
)
except Exception as e:
logger.error(f'Exception encountered sending email to {user.id}')
Expand Down Expand Up @@ -80,11 +81,11 @@ def add_arguments(self, parser):
)

parser.add_argument(
'--r',
'--start-id',
type=int,
dest='run',
dest='start_id',
default=0,
help='Specify which run this is'
help='Specify id to start from.'
)

parser.add_argument(
Expand All @@ -105,9 +106,9 @@ def add_arguments(self, parser):
def handle(self, *args, **options):
dry_run = options.get('dry_run', False)
template = options.get('template')
run = options.get('run')
start_id = options.get('start_id')
ids = options.get('ids')
offset = options.get('offset', OFFSET)
email_all_users(template, dry_run, run=run, ids=ids, offset=offset)
email_all_users(template, dry_run, start_id=start_id, ids=ids, offset=offset)
if dry_run:
raise RuntimeError('Dry run, only superusers emailed')
8 changes: 4 additions & 4 deletions osf_tests/management_commands/test_email_all_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_email_all_users_dry(self, mock_email, superuser):
mock_email.assert_called_with(
to_addr=superuser.email,
mail=mails.TOU_NOTIF,
fullname=superuser.fullname
given_name=superuser.given_name
)

@pytest.mark.django_db
Expand All @@ -64,10 +64,10 @@ def test_dont_email_inactive_users(
@pytest.mark.django_db
@mock.patch('website.mails.send_mail')
def test_email_all_users_offset(self, mock_email, user, user2):
email_all_users('TOU_NOTIF', offset=1, run=0)
email_all_users('TOU_NOTIF', offset=1, start_id=0)

email_all_users('TOU_NOTIF', offset=1, run=1)
email_all_users('TOU_NOTIF', offset=1, start_id=1)

email_all_users('TOU_NOTIF', offset=1, run=2)
email_all_users('TOU_NOTIF', offset=1, start_id=2)

assert mock_email.call_count == 2
8 changes: 4 additions & 4 deletions website/templates/emails/tou_notif.html.mako
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
<%def name="content()">
<tr>
<td style="border-collapse: collapse;">
Hi ${fullname},<br>
Hi ${given_name},<br>
<br>
On August 10, 2020 the COS Websites and Services <a href="https://github.com/CenterForOpenScience/cos.io/blob/master/TERMS_OF_USE.md">Terms of Use</a> will change. The updates to the Terms are necessary to support continued use of the Websites and Services by the public.<br>
To better understand what has changed, go <a href="https://github.com/CenterForOpenScience/cos.io/pull/1025/files">here</a>.<br>
On Friday, January 10, 2025 the COS Websites and Services <a href="https://github.com/CenterForOpenScience/cos.io/blob/master/TERMS_OF_USE.md">Terms of Use</a> and <a href="https://github.com/CenterForOpenScience/cos.io/blob/master/PRIVACY_POLICY.md">Privacy Policy</a> will change. The updates to the Terms are necessary to support continued use of the Websites and Services by the public.<br>
To better understand what has changed, see the <a href="https://github.com/CenterForOpenScience/cos.io/pull/1033/files">Terms of Use change summary</a> and <a href="https://github.com/CenterForOpenScience/cos.io/pull/1034/files">Privacy Policy change summary</a>.<br>
<br>
If you have any questions email [email protected].<br>
You do not need to take any actions to acknowledge these updates. If you have any questions, please email [email protected].<br>
<br>
Regards,<br>
<br>
Expand Down

0 comments on commit bf3c7d8

Please sign in to comment.