-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command-line tool to manually enter students into contests.
- Loading branch information
Showing
1 changed file
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |