forked from mmcinnestaylor/Programming-Contest-Suite
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtasks.py
More file actions
514 lines (421 loc) · 20.9 KB
/
Copy pathtasks.py
File metadata and controls
514 lines (421 loc) · 20.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
import csv
import json
import yaml
import os
from itertools import islice
from math import ceil, log10
from django.contrib.auth.models import User
from django.core.mail import send_mass_mail
from django.db import transaction
from django.template.loader import render_to_string
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_encode
from celery import shared_task
from celery.utils.log import get_task_logger
from contestsuite.settings import MEDIA_ROOT, DEFAULT_FROM_EMAIL
from contestadmin.models import Contest
from core.utils import make_random_password
from manager.models import Course, Faculty, Profile
from register.models import Team
logger = get_task_logger(__name__)
@shared_task
@transaction.atomic
def create_walkin_teams(division, total):
"""
Celery task to create walk-in teams.
division(int): the division in which the team(s) will compete
total(int): the number of teams to create
"""
if total > 0:
logger.debug('Starting walk-in team creation')
if division == 1:
base_name = 'Walk-in-U-'
else:
base_name = 'Walk-in-L-'
existing_teams = Team.objects.filter(
name__contains=base_name)
existing_count = existing_teams.count()
if existing_count > 0:
existing_count_width = ceil(log10(existing_count))
new_count_width = ceil(log10(existing_count+total))
# Update existing team names if 0-padding width changes
if new_count_width > existing_count_width:
for team in existing_teams:
walkin_id = int(team.name.split("-")[-1])
team.name = f"{base_name}{str(walkin_id).zfill(new_count_width)}"
team.save()
logger.debug("Renamed existing Walk-in teams.")
else:
new_count_width = ceil(log10(total))
# Create and write teams to db
for i in range(1, total+1):
name = f"{base_name}{str(existing_count+i).zfill(new_count_width)}"
pin = make_random_password(length=6)
Team.objects.create(name=name, division=division, pin=pin)
logger.debug(f'Created walk-in team {existing_count+i}')
logger.info(f'{total} {base_name[:-1]} teams created.')
else:
logger.error("New Walk-in team count LEQ 0.")
@shared_task
@transaction.atomic
def generate_contest_files(file_format='json'):
"""
Celery task to create input files for the DOMjudge jury system.
- Currently each division (U/L) is set up as a separate contest in DOMjudge
- Generator creates necessary files (teams, accounts, groups) to set up a contest for each division
- Supports TSV, JSON, and YAML formats (file_format parameter); defaults to json if not specified
https://www.domjudge.org/docs/manual/9.0/import.html
"""
logger.info(f"generate_contest_files called with file_format='{file_format}'")
for file in os.listdir(MEDIA_ROOT + '/contest_files/'):
if file.endswith('.json') or file.endswith('.yaml') or file.endswith('.tsv'):
os.remove(os.path.join(MEDIA_ROOT + '/contest_files/', file))
teams = Team.objects.all()
if teams.count() > 0:
fill_width = ceil(log10(teams.count()))
logger.debug('Starting team credential creation')
for i,team in enumerate(teams):
team.contest_id = 'acm-' + str(i+1).zfill(fill_width)
team.contest_password = make_random_password(length=6)
team.save()
logger.info(f'Created credentials for {teams.count()} teams')
# Create DOMjudge contest files per division
for division in Team.DIVISION:
if division[0] == 1: # Upper
account_file = MEDIA_ROOT + f'/contest_files/accounts_upper.{file_format}'
group_file = MEDIA_ROOT + f'/contest_files/groups_upper.{file_format}'
team_file = MEDIA_ROOT + f'/contest_files/teams_upper.{file_format}'
else: # Lower
account_file = MEDIA_ROOT + f'/contest_files/accounts_lower.{file_format}'
group_file = MEDIA_ROOT + f'/contest_files/groups_lower.{file_format}'
team_file = MEDIA_ROOT + f'/contest_files/teams_lower.{file_format}'
# Get teams for current division
teams = Team.objects.filter(division=division[0])
# Create accounts, groups, and teams data structures
# Groups are initialized with current division info
# Upper Division Group -> 6
# Lower Division Group -> 7
if file_format != 'tsv': # JSON or YAML
accounts_data = []
groups_data = [{
'id': str(division[0]+5), # Category ID (str)
'name': division[1] # Name of the team category (str)
}]
teams_data = []
for team in teams:
# Account info
accounts_data.append({
'id': team.contest_id, # Account ID (str)
'username': team.contest_id, # DOMjudge Username (str)
'password': team.contest_password, # DOMjudge Password (str)
'type': 'team', # User type (str) - can be 'team' or 'judge'
'team_id': int((team.contest_id).strip("acm-")), # Team Number (int)
'name': team.contest_id, # Full name of the user (str)
})
# Team profile
teams_data.append({
'id': str(int((team.contest_id).strip("acm-"))), # Team number (str)
'icpc_id': str(int((team.contest_id).strip("acm-"))), # ICPC ID number (str)
'group_ids': [str(team.division + 5)], # Group ID as a list of strings
'name': team.name, # Team name (str)
'display_name': team.name, # Display name (not optional) (str)
'organization_id': 'FSU' # External team affiliation ID (str)
})
# write data
if file_format == 'json':
with open(account_file, 'w') as account_json:
json.dump(accounts_data, account_json, indent=2)
with open(group_file, 'w') as group_json:
json.dump(groups_data, group_json, indent=2)
with open(team_file, 'w') as team_json:
json.dump(teams_data, team_json, indent=2)
else: # YAML format
with open(account_file, 'w') as account_yaml:
yaml.dump(accounts_data, account_yaml, sort_keys=False)
with open(group_file, 'w') as group_yaml:
yaml.dump(groups_data, group_yaml, sort_keys=False)
with open(team_file, 'w') as team_yaml:
yaml.dump(teams_data, team_yaml, sort_keys=False)
else: # TSV format
with open(account_file, 'w', newline='') as account_tsv:
with open(group_file, 'w', newline='') as group_tsv:
with open(team_file, 'w', newline='') as team_tsv:
account_writer = csv.writer(
account_tsv, delimiter='\t', quoting=csv.QUOTE_MINIMAL)
group_writer = csv.writer(
group_tsv, delimiter='\t', quoting=csv.QUOTE_MINIMAL)
team_writer = csv.writer(
team_tsv, delimiter='\t', quoting=csv.QUOTE_MINIMAL)
# File headers
account_writer.writerow(['accounts', '1'])
group_writer.writerow(['File_Version', '1'])
team_writer.writerow(['File_Version', '2'])
# Group info
# Upper Division Group -> 6
# Lower Division Group -> 7
group_writer.writerow([
division[0]+5, # Category ID (int)
division[1] # Name of the team category (str)
])
# Write teams info for current division
for team in teams:
# Account info
account_writer.writerow([
'team', # User type (str): 'team' or 'judge'
team.contest_id, # Full name of the user (str)
team.contest_id, # DOMjudge Username (str)
team.contest_password, # DOMjudge Password (str)
])
# Team profile
team_writer.writerow([
int((team.contest_id).strip("acm-")), # Team Number (int)
int((team.contest_id).strip("acm-")), # External ID (int)
team.division + 5, # Group ID (int)
team.name, # Team name (str)
'Florida State University', # Institution name (str)
'FSU', # Institution short name (str)
'USA', # Country code in ISO 3166-1 alpha-3 format
'', # External institution ID ** not used in our config **
])
logger.debug('Successfully generated contest files')
else:
logger.error("No Team objects exist in database.")
@shared_task
@transaction.atomic
def check_in_out_users(action):
"""
Celery task to allow an admin to check in/out all users.
action(int): If 1, check in for the main contest. If 2 check in for a
practice contest. Othetwise check out users.
"""
# Check-in
if action == 1 or action == 2:
users = User.objects.all()
for user in users:
if user.profile.team == None or user.profile.checked_in == True:
continue
user.profile.checked_in = True
user.save()
if action == 1:
subject = 'Programming Contest DOMjudge Credentials'
message = render_to_string(
'checkin/team_credentials_email.html', {'user': user})
else:
subject = 'Practice Contest DOMjudge Credentials'
message = render_to_string(
'checkin/team_credentials_practice_email.html', {'user': user})
user.email_user(subject, message)
logger.debug(f'Sent credentials to {user.username}')
# Check-out
else:
users = User.objects.all()
for user in users:
user.profile.checked_in = False
user.save()
@shared_task
def generate_ec_reports():
"""
Celery task to create contest participation reports used by faculty to assign
extra credit for contest participation.
- Each file maps to a course registered in the database.
- File creation condition: At least one user has selected
the course in their profile AND checked into the contest
IMPORTANT:
Faculty model currently requires an FSU CS dept email address, so file naming
format <faculty-email_course-code.csv> will map correctly.
Updating Faculty model to support non-FSU CS addresses will require file naming update.
Update 3/8/2026:
Refactored questions_solved to new questions_for_extra_credit property
and changed variable names to more clearly reflect number of extra credits earned
"""
num_courses = 0
faculty_members = Faculty.objects.all()
for faculty in faculty_members:
# All courses for given faculty member
courses = Course.objects.filter(instructor=faculty)
for course in courses:
# All students in given course who checked in on contest day
students = User.objects.filter(profile__courses=course).filter(profile__checked_in=True)
if students.exists():
num_courses += 1
# Filename format: faculty-email_course-code.csv
filename = f"{MEDIA_ROOT}/ec_files/{faculty.email.split('@')[0]}_{course.code}.csv"
# Participation report for given course
with open(filename, 'w', newline='') as csvfile:
writer = csv.writer(csvfile, quoting=csv.QUOTE_MINIMAL)
# File header
writer.writerow(
['fsu_id', 'last_name', 'first_name', 'extra_credits', 'team_division', 'role'])
for student in students:
if student.profile.fsu_id is None:
fsu_id = 'none'
else:
fsu_id = student.profile.fsu_id
if student.profile.team is None:
extra_credits = 'none'
else:
extra_credits = student.profile.team.questions_for_extra_credit
writer.writerow([
fsu_id,
student.last_name,
student.first_name,
extra_credits,
student.profile.team.get_division_code() if student.profile.team else 'none',
student.profile.get_role()
])
logger.info(
f'Processed extra credit files for {num_courses} courses')
@shared_task
def generate_team_csvs():
"""
Celery task which creates CSV files containing team data per division.
"""
for division in Team.DIVISION:
if division[0] == 1: # Upper
team_file = f"{MEDIA_ROOT}/team_files/upper.csv"
else: # Lower
team_file = f"{MEDIA_ROOT}/team_files/lower.csv"
with open(team_file, 'w', newline='') as team_csv:
writer = csv.writer(
team_csv, delimiter=',', quoting=csv.QUOTE_MINIMAL)
# File header
writer.writerow(['team_division', 'team_name', 'questions_answered', 'domjudge_id', 'team_active', 'team_members'])
# Team data
teams = Team.objects.filter(division=division[0])
for team in teams:
writer.writerow([
team.get_division_code(),
team.name,
team.questions_answered,
team.contest_id,
'T' if team.is_active() else 'F',
'_'.join(team.get_members())
])
@shared_task
def email_faculty(domain):
"""
Celery task to notify faculty members of available participation reports.
domain(str): the domain name of the host server (ex. contest.cs.fsu.edu)
"""
faculty_members = Faculty.objects.all()
fpath = f"{MEDIA_ROOT}/ec_files/"
message_subject = 'Programming Contest EC files'
messages = []
for faculty in faculty_members:
found_files = False
# Detemine if EC files exist for given faculty member
for fname in os.listdir(fpath):
uid = faculty.email.split('@')[0]
if uid in fname:
found_files = True
break
if found_files:
message = render_to_string('contestadmin/ec_available_email.html', {
'faculty': faculty,
'domain': domain,
'uid': urlsafe_base64_encode(force_bytes(uid)),
})
else:
message = render_to_string('contestadmin/no_ec_available_email.html', {
'faculty': faculty,
'domain': domain,
'uid': urlsafe_base64_encode(force_bytes(uid)),
})
messages.append((
message_subject,
message,
DEFAULT_FROM_EMAIL,
[faculty.email]
))
# Group messages for mass mailing, send_mass_mail requires Tuple format
messages = tuple(messages)
try:
send_mass_mail(messages, fail_silently=False)
except:
logger.error("Failed to send extra credit notification to faculty.")
@shared_task
@transaction.atomic
def process_contest_results():
"""
Celery task which processes a DOMjudge results file uploaded to the server.
Update 3/8/2026:
We are now importing JSON from /api/v4/contests/{cid}/scoreboard
Reasoning: this API endpoint shows us problem IDs, which we can use
to determine which questions a given team is eligible
for extra credit from.
The old logic for processing TSVs is kept in comments for reference and potential future use
"""
# num_teams = 0
# contest = Contest.objects.all().first()
#
# if not contest:
# logger.error("No Contest object exists in database.")
# else:
# if Team.objects.all().count() > 0:
# # Determine width of numerical portion of DOMjudge usernames
# fill_width = ceil(log10(Team.objects.all().count()))
#
# with open(contest.results.path) as resultsfile:
# results = csv.reader(resultsfile, delimiter="\t", quotechar='"')
#
# # islice - Skip header of file
# for row in islice(results, 1, None):
# # [DOMjudge team ID] <id> -> [Registration team ID] acm-(zfill)<id>
# id = f"acm-{row[0].zfill(fill_width)}"
#
# try:
# team = Team.objects.get(contest_id=id)
# team.questions_answered = row[3]
# team.score = row[4]
# team.last_submission = row[5]
# team.save()
# except:
# logger.error(
# f"Could not process contest results for team {id}")
# else:
# logger.debug(f"Processed team {id}")
# num_teams += 1
#
# logger.info(f"Processed contest results for {num_teams} teams")
# else:
# logger.error("No Team objects exist in database.")
num_teams = 0
contest = Contest.objects.all().first()
if not contest:
logger.error("No Contest object exists in database.")
else:
if Team.objects.all().count() > 0:
with open(contest.results.path) as resultsfile:
# Load JSON data from file
data = json.load(resultsfile)
for row in data.get("rows", []): # iterate through "rows" key in JSON data; "rows" is a list of team result objects
team_id = row.get("team_id")
fill_width = ceil(log10(Team.objects.all().count())) # fill width defined by number of teams
id = f"acm-{team_id.zfill(fill_width)}" # set id to match contest_id field of Team model for lookup
try:
team = Team.objects.get(contest_id=id)
score = row.get("score", {})
problems = row.get("problems", [])
team.questions_answered = score.get("num_solved", 0)
team.score = score.get("total_time", 0) # score = total time
team.last_submission = max((p.get("time", 0) for p in problems), default=0) # get the problem that was solved at the latest time
solved_problems = [
p for p in row.get("problems", [])
if p.get("solved")
]
if team.division == 2:
team.questions_for_extra_credit = len(solved_problems)
else:
team.questions_for_extra_credit = sum(
1 for p in solved_problems
if int(p.get("problem_id", 0)) > 4 # problem ids > 4 are eligible for extra credit for lower division teams
)
team.save()
except Team.DoesNotExist:
logger.error(f"Could not find a team with contest_id {id}")
except Exception as e:
logger.error(f"Could not process results for team {id}: {e}")
else:
logger.debug(f"Processed team {id}")
num_teams += 1
logger.info(f"Processed contest results for {num_teams} teams")