Skip to content

Commit

Permalink
Add command-line tool to manually enter students into contests.
Browse files Browse the repository at this point in the history
  • Loading branch information
davepeck committed May 1, 2024
1 parent 9a4895e commit 8b4e9d5
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions server/vb/management/commands/enter_contest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from django.core.management.base import BaseCommand

from server.vb.models import Contest, School
from server.vb.ops import enter_contest, send_validation_link_email


class Command(BaseCommand):
"""Force enter one or more students into a contest."""

help = (
"Force enter one or more students into a contest."
"Students are identified by their email addresses or address patterns."
)

def add_arguments(self, parser):
"""Add command arguments."""
parser.add_argument("contest_id", type=int)
parser.add_argument("emails", nargs="+", type=str)

def handle(self, contest_id, emails: list[str], **options):
"""Handle the command."""
contest = Contest.objects.get(pk=contest_id)
school = contest.school
for email in emails:
self._process_students(contest, school, email)

def _process_students(self, contest: Contest, school: School, email: str):
"""Enter one or more students into a contest, if not already entered."""
if email[0] == "@":
students = school.students.filter(email__endswith=email[1:])
else:
students = school.students.filter(email=email)

when = contest.start_at

for student in students:
if student.email.startswith("frontseat"):
self.stdout.write(f"SKIPFRN {student.email}")
continue
contest_entry, entered = enter_contest(student, contest, when=when)
winner = "!WINNER!" if contest_entry.is_winner else ""
if entered:
self.stdout.write(
f"ENTERED {student.email} ({contest_entry.roll}) {winner}" # noqa
)
if contest_entry.is_winner:
link = send_validation_link_email(
student, student.email, contest_entry
)
self.stdout.write(f"\t--> email sent: {link.relative_url}")
else:
self.stdout.write(
f"ALREADY {student.email} ({contest_entry.roll}) {winner}" # noqa
)

0 comments on commit 8b4e9d5

Please sign in to comment.