Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional changes to the Export contest submissions details script #295

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions judge/management/commands/export_contest_submissions_details.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
import csv
import re

from django.core.management.base import BaseCommand, CommandError

from judge.models import Contest, Submission, SubmissionTestCase
from judge.models import Contest, ContestSubmission, SubmissionTestCase
from judge.utils.raw_sql import use_straight_join
from judge.views.submission import submission_related

# https://stackoverflow.com/a/14693789/16224359
# Error messages may sometimes be formatted with
# ANSI sequences representing text colors and text weight,
# which may display dirty text in raw text files.
# The following regex is to filter such sequences.
ansi_escape = re.compile(r"""
\x1B # ESC
(?: # 7-bit C1 Fe (except CSI)
[@-Z\\-_]
| # or [ for CSI, followed by a control sequence
\[
[0-?]* # Parameter bytes
[ -/]* # Intermediate bytes
[@-~] # Final byte
)
""", re.VERBOSE)


class Command(BaseCommand):
Expand All @@ -21,33 +38,52 @@ def handle(self, *args, **options):
if contest is None:
raise CommandError('contest not found')

queryset = Submission.objects.all()
queryset = ContestSubmission.objects.all()
use_straight_join(queryset)
queryset = submission_related(queryset.order_by('-id'))
queryset = queryset.filter(contest_object=contest)
queryset = queryset.filter(submission__contest_object=contest,
participation__virtual=0) \
.order_by('-id')

self.export_queryset_to_output(queryset, options['output'])

def export_queryset_to_output(self, queryset, output_file):
fout = open(output_file, 'w', newline='')

writer = csv.DictWriter(fout, fieldnames=['username', 'problem', 'submission', 'testcase',
'points', 'time', 'memory', 'feedback'])
'result', 'points', 'time', 'memory', 'feedback'])
writer.writeheader()

for submission in queryset:
for contest_submission in queryset:
submission = contest_submission.submission

# Submission row
escaped_error = ansi_escape.sub('', submission.error)

writer.writerow({
'username': submission.user.username,
'problem': submission.problem.code,
'submission': submission.id,
'result': submission.result,
'points': submission.case_points,
'time': submission.time,
'memory': submission.memory,
'feedback': escaped_error,
})

testcases = SubmissionTestCase.objects.filter(submission=submission)

for testcase in testcases:
case_id = f'case{testcase.case}'
if submission.batch:
case_id += f'/batch{testcase.batch}'

# Testcase rows
writer.writerow({
'username': submission.user.username,
'problem': submission.problem.code,
'submission': submission.id,
'testcase': case_id,
'result': testcase.status,
'points': testcase.points,
'time': testcase.time,
'memory': testcase.memory,
Expand Down