2021-01-29 04:43:10 +00:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from judge.models import SubmissionTestCase, Problem, Profile, Language, Organization
|
2020-12-01 23:18:17 +00:00
|
|
|
|
2021-01-29 04:43:10 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
import csv
|
2020-12-01 23:18:17 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
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])
|
|
|
|
|
2021-01-29 04:43:10 +00:00
|
|
|
|
|
|
|
def import_users(csv_file):
|
2021-04-07 19:08:42 +00:00
|
|
|
# 1st row: username, password, name, organization
|
|
|
|
# ... row: a_username, passhere, my_name, organ
|
2021-01-29 04:43:10 +00:00
|
|
|
try:
|
|
|
|
f = open(csv_file, 'r')
|
|
|
|
except OSError:
|
|
|
|
print("Could not open csv file", csv_file)
|
|
|
|
return
|
|
|
|
|
|
|
|
with f:
|
|
|
|
reader = csv.DictReader(f)
|
|
|
|
|
|
|
|
for row in reader:
|
2021-04-07 19:08:42 +00:00
|
|
|
try:
|
|
|
|
username = row['username']
|
|
|
|
pwd = row['password']
|
|
|
|
except Exception:
|
|
|
|
print('username and/or password column missing')
|
|
|
|
print('Make sure your columns are: username, password, name, organization')
|
|
|
|
|
|
|
|
user, created = User.objects.get_or_create(username=username, defaults={
|
2021-01-29 04:43:10 +00:00
|
|
|
'is_active': True,
|
|
|
|
})
|
|
|
|
|
|
|
|
profile, _ = Profile.objects.get_or_create(user=user, defaults={
|
|
|
|
'language': Language.get_python3(),
|
|
|
|
'timezone': settings.DEFAULT_USER_TIME_ZONE,
|
|
|
|
})
|
2021-04-07 19:08:42 +00:00
|
|
|
if created:
|
|
|
|
print('Created user', username)
|
2021-01-29 04:43:10 +00:00
|
|
|
|
|
|
|
if pwd:
|
|
|
|
user.set_password(pwd)
|
2021-04-07 19:08:42 +00:00
|
|
|
elif created:
|
|
|
|
user.set_password('lqdoj')
|
|
|
|
print('User', username, 'missing password, default=lqdoj')
|
|
|
|
|
|
|
|
if 'name' in row.keys() and row['name']:
|
|
|
|
user.first_name = row['name']
|
|
|
|
|
2021-01-29 04:43:10 +00:00
|
|
|
if 'organization' in row.keys() and row['organization']:
|
|
|
|
org = Organization.objects.get(name=row['organization'])
|
|
|
|
profile.organizations.add(org)
|
2021-06-07 07:00:53 +00:00
|
|
|
user.email = row['email']
|
2021-01-29 04:43:10 +00:00
|
|
|
user.save()
|
|
|
|
profile.save()
|