Skip to content

Commit

Permalink
Cleanup user creation script
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Cameron <[email protected]>
  • Loading branch information
JasonLovesDoggo committed Oct 23, 2024
1 parent 1b8a8ab commit dfeabc4
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions dmoj/scripts/contest_login_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
import json
import secrets
import string

from judge.models import *
from django.core.mail import *
from django.contrib.auth.models import User
from django.conf import settings

from django.contrib.auth.models import User
from django.core.mail import *
from judge.models import *

# Must be updated between runs when using email batches
FILE_START_NUM = 0 # File offset
USER_START_NUM = 1100 # Latest team number (after file offset included)
USER_START_NUM = 1100 # Latest team number (after file offset included)

FILE = "lcc2023-11.json" # Must be json with {"team name": [{"name": "", "email": "", "school": ""}, ...], ...}
COMPETITION = "LCC23" # ex. 'Moose' or 'LCC'
Expand Down Expand Up @@ -41,44 +39,46 @@
This is an automated message. If you have any questions or concerns, please contact [email protected].
"""

def send(real=False):
org = Organization.objects.get(id=ORG_ID)
with open(FILE, "r") as f:
data = json.loads(f.read())

mails_sent = 0
num = 0
for team, users in data .items():
num += 1
if num < FILE_START_NUM:
continue

mails_sent += len(users)
if mails_sent > MAIL_LIMIT:
print("Sent %d mails" % (mails_sent - len(users)))
print("Next team: %s)" % team)
print("Update 'FILE_START_NUM' in the script to: %d" % num)
return

username = "%s_%s_%d" % (COMPETITION, team, num + USER_START_NUM-1)
password = ''.join(secrets.choice(string.ascii_letters + string.digits) for i in range(16))
print("Making user: (%s, %s)" % (username, password))
if real:
usr = User.objects.create_user(username, password=password)
profile = Profile(user=usr, is_external_user=True)

msg = MESSAGE.format(**{"user": username, "pass": password, "team": team})
emails = []
notes = ""
for user in users:
emails.append(user["email"])
notes += "%s <%s>\n" % (user["name"], user["email"])

email = EmailMessage(SUBJECT, msg, FROM_EMAIL, [], emails)
print("Preparing message: %s" % emails)
if real:
profile.notes = notes
profile.save()
profile.organizations.add(org)
email.send()
print('email sent')
def send(dry_run=False):
org = Organization.objects.get(id=ORG_ID)
with open(FILE, "r") as f:
data = json.loads(f.read())

mails_sent = 0
num = 0
for team, users in data.items():
num += 1
if num < FILE_START_NUM:
continue

mails_sent += len(users)
if mails_sent > MAIL_LIMIT:
print("Sent %d mails" % (mails_sent - len(users)))
print("Next team: %s)" % team)
print("Update 'FILE_START_NUM' in the script to: %d" % num)
return

username = "%s_%s_%d" % (COMPETITION, team, num + USER_START_NUM - 1)
password = ''.join(secrets.choice(string.ascii_letters + string.digits) for i in range(16))
profile: Profile = None
print("Making user: (%s, %s)" % (username, password))
if not dry_run:
usr = User.objects.create_user(username, password=password)
profile = Profile(user=usr)

msg = MESSAGE.format(**{"user": username, "pass": password, "team": team})
emails = []
notes = ""
for user in users:
emails.append(user["email"])
notes += "%s <%s>\n" % (user["name"], user["email"])

email = EmailMessage(SUBJECT, msg, FROM_EMAIL, [], emails)
print("Preparing message: %s" % emails)
if not dry_run:
profile.notes = notes
profile.save()
profile.organizations.add(org)
email.send()
print('email sent')

0 comments on commit dfeabc4

Please sign in to comment.