Reformat using black
This commit is contained in:
parent
efee4ad081
commit
a87fb49918
221 changed files with 19127 additions and 7310 deletions
|
@ -7,7 +7,7 @@ from moss import MOSS
|
|||
from judge.models import Contest, ContestMoss, ContestParticipation, Submission
|
||||
from judge.utils.celery import Progress
|
||||
|
||||
__all__ = ('rescore_contest', 'run_moss')
|
||||
__all__ = ("rescore_contest", "run_moss")
|
||||
|
||||
|
||||
@shared_task(bind=True)
|
||||
|
@ -16,7 +16,9 @@ def rescore_contest(self, contest_key):
|
|||
participations = contest.users
|
||||
|
||||
rescored = 0
|
||||
with Progress(self, participations.count(), stage=_('Recalculating contest scores')) as p:
|
||||
with Progress(
|
||||
self, participations.count(), stage=_("Recalculating contest scores")
|
||||
) as p:
|
||||
for participation in participations.iterator():
|
||||
participation.recompute_results()
|
||||
rescored += 1
|
||||
|
@ -29,7 +31,7 @@ def rescore_contest(self, contest_key):
|
|||
def run_moss(self, contest_key):
|
||||
moss_api_key = settings.MOSS_API_KEY
|
||||
if moss_api_key is None:
|
||||
raise ImproperlyConfigured('No MOSS API Key supplied')
|
||||
raise ImproperlyConfigured("No MOSS API Key supplied")
|
||||
|
||||
contest = Contest.objects.get(key=contest_key)
|
||||
ContestMoss.objects.filter(contest=contest).delete()
|
||||
|
@ -37,21 +39,34 @@ def run_moss(self, contest_key):
|
|||
length = len(ContestMoss.LANG_MAPPING) * contest.problems.count()
|
||||
moss_results = []
|
||||
|
||||
with Progress(self, length, stage=_('Running MOSS')) as p:
|
||||
with Progress(self, length, stage=_("Running MOSS")) as p:
|
||||
for problem in contest.problems.all():
|
||||
for dmoj_lang, moss_lang in ContestMoss.LANG_MAPPING:
|
||||
result = ContestMoss(contest=contest, problem=problem, language=dmoj_lang)
|
||||
result = ContestMoss(
|
||||
contest=contest, problem=problem, language=dmoj_lang
|
||||
)
|
||||
|
||||
subs = Submission.objects.filter(
|
||||
contest__participation__virtual__in=(ContestParticipation.LIVE, ContestParticipation.SPECTATE),
|
||||
contest_object=contest,
|
||||
problem=problem,
|
||||
language__common_name=dmoj_lang,
|
||||
).order_by('-points').values_list('user__user__username', 'source__source')
|
||||
subs = (
|
||||
Submission.objects.filter(
|
||||
contest__participation__virtual__in=(
|
||||
ContestParticipation.LIVE,
|
||||
ContestParticipation.SPECTATE,
|
||||
),
|
||||
contest_object=contest,
|
||||
problem=problem,
|
||||
language__common_name=dmoj_lang,
|
||||
)
|
||||
.order_by("-points")
|
||||
.values_list("user__user__username", "source__source")
|
||||
)
|
||||
|
||||
if subs.exists():
|
||||
moss_call = MOSS(moss_api_key, language=moss_lang, matching_file_limit=100,
|
||||
comment='%s - %s' % (contest.key, problem.code))
|
||||
moss_call = MOSS(
|
||||
moss_api_key,
|
||||
language=moss_lang,
|
||||
matching_file_limit=100,
|
||||
comment="%s - %s" % (contest.key, problem.code),
|
||||
)
|
||||
|
||||
users = set()
|
||||
|
||||
|
@ -59,7 +74,7 @@ def run_moss(self, contest_key):
|
|||
if username in users:
|
||||
continue
|
||||
users.add(username)
|
||||
moss_call.add_file_from_memory(username, source.encode('utf-8'))
|
||||
moss_call.add_file_from_memory(username, source.encode("utf-8"))
|
||||
|
||||
result.url = moss_call.process()
|
||||
result.submission_count = len(users)
|
||||
|
|
|
@ -4,7 +4,7 @@ from celery import shared_task
|
|||
|
||||
from judge.utils.celery import Progress
|
||||
|
||||
__all__ = ('success', 'failure', 'progress')
|
||||
__all__ = ("success", "failure", "progress")
|
||||
|
||||
|
||||
@shared_task
|
||||
|
@ -14,7 +14,7 @@ def success():
|
|||
|
||||
@shared_task
|
||||
def failure():
|
||||
raise RuntimeError('This task always fails.')
|
||||
raise RuntimeError("This task always fails.")
|
||||
|
||||
|
||||
@shared_task(bind=True)
|
||||
|
|
|
@ -2,19 +2,20 @@ from judge.models import SubmissionTestCase, Problem
|
|||
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
def generate_report(problem):
|
||||
testcases = SubmissionTestCase.objects.filter(submission__problem=problem).all()
|
||||
|
||||
|
||||
score = defaultdict(int)
|
||||
total = defaultdict(int)
|
||||
rate = defaultdict(int)
|
||||
|
||||
for case in testcases.iterator():
|
||||
score[case.case] += int(case.status == 'AC')
|
||||
score[case.case] += int(case.status == "AC")
|
||||
total[case.case] += 1
|
||||
|
||||
for i in score:
|
||||
rate[i] = score[i] / total[i]
|
||||
|
||||
for i, _ in sorted(rate.items(), key=lambda x: x[1], reverse=True):
|
||||
print(i, score[i], total[i], rate[i])
|
||||
print(i, score[i], total[i], rate[i])
|
||||
|
|
|
@ -7,20 +7,23 @@ from django.contrib.auth.models import User
|
|||
from judge.models import Profile, Language, Organization
|
||||
|
||||
|
||||
fields = ['username', 'password', 'name', 'school', 'email', 'organizations']
|
||||
descriptions = ['my_username(edit old one if exist)',
|
||||
'123456 (must have)',
|
||||
'Le Van A (can be empty)',
|
||||
'Le Quy Don (can be empty)',
|
||||
'email@email.com (can be empty)',
|
||||
'org1&org2&org3&... (can be empty - org slug in URL)']
|
||||
fields = ["username", "password", "name", "school", "email", "organizations"]
|
||||
descriptions = [
|
||||
"my_username(edit old one if exist)",
|
||||
"123456 (must have)",
|
||||
"Le Van A (can be empty)",
|
||||
"Le Quy Don (can be empty)",
|
||||
"email@email.com (can be empty)",
|
||||
"org1&org2&org3&... (can be empty - org slug in URL)",
|
||||
]
|
||||
|
||||
|
||||
def csv_to_dict(csv_file):
|
||||
rows = csv.reader(csv_file.read().decode().split('\n'))
|
||||
rows = csv.reader(csv_file.read().decode().split("\n"))
|
||||
header = next(rows)
|
||||
header = [i.lower() for i in header]
|
||||
|
||||
if 'username' not in header:
|
||||
if "username" not in header:
|
||||
return []
|
||||
|
||||
res = []
|
||||
|
@ -28,55 +31,61 @@ def csv_to_dict(csv_file):
|
|||
for row in rows:
|
||||
if len(row) != len(header):
|
||||
continue
|
||||
cur_dict = {i: '' for i in fields}
|
||||
cur_dict = {i: "" for i in fields}
|
||||
for i in range(len(header)):
|
||||
if header[i] not in fields:
|
||||
continue
|
||||
cur_dict[header[i]] = row[i]
|
||||
if cur_dict['username']:
|
||||
if cur_dict["username"]:
|
||||
res.append(cur_dict)
|
||||
return res
|
||||
|
||||
|
||||
|
||||
# return result log
|
||||
def import_users(users):
|
||||
log = ''
|
||||
log = ""
|
||||
for i, row in enumerate(users):
|
||||
cur_log = str(i + 1) + '. '
|
||||
cur_log = str(i + 1) + ". "
|
||||
|
||||
username = row['username']
|
||||
cur_log += username + ': '
|
||||
username = row["username"]
|
||||
cur_log += username + ": "
|
||||
|
||||
pwd = row['password']
|
||||
|
||||
user, created = User.objects.get_or_create(username=username, defaults={
|
||||
'is_active': True,
|
||||
})
|
||||
pwd = row["password"]
|
||||
|
||||
profile, _ = Profile.objects.get_or_create(user=user, defaults={
|
||||
'language': Language.get_python3(),
|
||||
'timezone': settings.DEFAULT_USER_TIME_ZONE,
|
||||
})
|
||||
user, created = User.objects.get_or_create(
|
||||
username=username,
|
||||
defaults={
|
||||
"is_active": True,
|
||||
},
|
||||
)
|
||||
|
||||
profile, _ = Profile.objects.get_or_create(
|
||||
user=user,
|
||||
defaults={
|
||||
"language": Language.get_python3(),
|
||||
"timezone": settings.DEFAULT_USER_TIME_ZONE,
|
||||
},
|
||||
)
|
||||
|
||||
if created:
|
||||
cur_log += 'Create new - '
|
||||
cur_log += "Create new - "
|
||||
else:
|
||||
cur_log += 'Edit - '
|
||||
cur_log += "Edit - "
|
||||
|
||||
if pwd:
|
||||
user.set_password(pwd)
|
||||
elif created:
|
||||
user.set_password('lqdoj')
|
||||
cur_log += 'Missing password, set password = lqdoj - '
|
||||
user.set_password("lqdoj")
|
||||
cur_log += "Missing password, set password = lqdoj - "
|
||||
|
||||
if 'name' in row.keys() and row['name']:
|
||||
user.first_name = row['name']
|
||||
if "name" in row.keys() and row["name"]:
|
||||
user.first_name = row["name"]
|
||||
|
||||
if 'school' in row.keys() and row['school']:
|
||||
user.last_name = row['school']
|
||||
if "school" in row.keys() and row["school"]:
|
||||
user.last_name = row["school"]
|
||||
|
||||
if row['organizations']:
|
||||
orgs = row['organizations'].split('&')
|
||||
if row["organizations"]:
|
||||
orgs = row["organizations"].split("&")
|
||||
added_orgs = []
|
||||
for o in orgs:
|
||||
try:
|
||||
|
@ -86,15 +95,15 @@ def import_users(users):
|
|||
except Organization.DoesNotExist:
|
||||
continue
|
||||
if added_orgs:
|
||||
cur_log += 'Added to ' + ', '.join(added_orgs) + ' - '
|
||||
cur_log += "Added to " + ", ".join(added_orgs) + " - "
|
||||
|
||||
if row["email"]:
|
||||
user.email = row["email"]
|
||||
|
||||
if row['email']:
|
||||
user.email = row['email']
|
||||
|
||||
user.save()
|
||||
profile.save()
|
||||
cur_log += 'Saved\n'
|
||||
cur_log += "Saved\n"
|
||||
log += cur_log
|
||||
log += 'FINISH'
|
||||
log += "FINISH"
|
||||
|
||||
return log
|
||||
return log
|
||||
|
|
|
@ -5,7 +5,7 @@ from django.utils.translation import gettext as _
|
|||
from judge.models import Problem, Profile, Submission
|
||||
from judge.utils.celery import Progress
|
||||
|
||||
__all__ = ('apply_submission_filter', 'rejudge_problem_filter', 'rescore_problem')
|
||||
__all__ = ("apply_submission_filter", "rejudge_problem_filter", "rescore_problem")
|
||||
|
||||
|
||||
def apply_submission_filter(queryset, id_range, languages, results):
|
||||
|
@ -21,7 +21,9 @@ def apply_submission_filter(queryset, id_range, languages, results):
|
|||
|
||||
|
||||
@shared_task(bind=True)
|
||||
def rejudge_problem_filter(self, problem_id, id_range=None, languages=None, results=None):
|
||||
def rejudge_problem_filter(
|
||||
self, problem_id, id_range=None, languages=None, results=None
|
||||
):
|
||||
queryset = Submission.objects.filter(problem_id=problem_id)
|
||||
queryset = apply_submission_filter(queryset, id_range, languages, results)
|
||||
|
||||
|
@ -40,27 +42,37 @@ def rescore_problem(self, problem_id):
|
|||
problem = Problem.objects.get(id=problem_id)
|
||||
submissions = Submission.objects.filter(problem_id=problem_id)
|
||||
|
||||
with Progress(self, submissions.count(), stage=_('Modifying submissions')) as p:
|
||||
with Progress(self, submissions.count(), stage=_("Modifying submissions")) as p:
|
||||
rescored = 0
|
||||
for submission in submissions.iterator():
|
||||
submission.points = round(submission.case_points / submission.case_total * problem.points
|
||||
if submission.case_total else 0, 1)
|
||||
submission.points = round(
|
||||
submission.case_points / submission.case_total * problem.points
|
||||
if submission.case_total
|
||||
else 0,
|
||||
1,
|
||||
)
|
||||
if not problem.partial and submission.points < problem.points:
|
||||
submission.points = 0
|
||||
submission.save(update_fields=['points'])
|
||||
submission.save(update_fields=["points"])
|
||||
submission.update_contest()
|
||||
rescored += 1
|
||||
if rescored % 10 == 0:
|
||||
p.done = rescored
|
||||
|
||||
with Progress(self, submissions.values('user_id').distinct().count(), stage=_('Recalculating user points')) as p:
|
||||
with Progress(
|
||||
self,
|
||||
submissions.values("user_id").distinct().count(),
|
||||
stage=_("Recalculating user points"),
|
||||
) as p:
|
||||
users = 0
|
||||
profiles = Profile.objects.filter(id__in=submissions.values_list('user_id', flat=True).distinct())
|
||||
profiles = Profile.objects.filter(
|
||||
id__in=submissions.values_list("user_id", flat=True).distinct()
|
||||
)
|
||||
for profile in profiles.iterator():
|
||||
profile._updating_stats_only = True
|
||||
profile.calculate_points()
|
||||
cache.delete('user_complete:%d' % profile.id)
|
||||
cache.delete('user_attempted:%d' % profile.id)
|
||||
cache.delete("user_complete:%d" % profile.id)
|
||||
cache.delete("user_attempted:%d" % profile.id)
|
||||
users += 1
|
||||
if users % 10 == 0:
|
||||
p.done = users
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue