Add import users

This commit is contained in:
cuom1999 2021-07-28 17:58:42 -05:00
parent 850076b444
commit 6faf7a10bd
8 changed files with 307 additions and 59 deletions

View file

@ -1,10 +1,6 @@
from django.contrib.auth.models import User
from django.conf import settings
from judge.models import SubmissionTestCase, Problem, Profile, Language, Organization
from judge.models import SubmissionTestCase, Problem
from collections import defaultdict
import csv
def generate_report(problem):
testcases = SubmissionTestCase.objects.filter(submission__problem=problem).all()
@ -21,52 +17,4 @@ def generate_report(problem):
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])
def import_users(csv_file):
# 1st row: username, password, name, organization
# ... row: a_username, passhere, my_name, organ
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:
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={
'is_active': True,
})
profile, _ = Profile.objects.get_or_create(user=user, defaults={
'language': Language.get_python3(),
'timezone': settings.DEFAULT_USER_TIME_ZONE,
})
if created:
print('Created user', username)
if pwd:
user.set_password(pwd)
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']
if 'organization' in row.keys() and row['organization']:
org = Organization.objects.get(name=row['organization'])
profile.organizations.add(org)
user.email = row['email']
user.save()
profile.save()
print(i, score[i], total[i], rate[i])

View file

@ -0,0 +1,98 @@
import csv
from tempfile import mktemp
from django.conf import settings
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)']
def csv_to_dict(csv_file):
rows = csv.reader(csv_file.read().decode().split('\n'))
header = next(rows)
header = [i.lower() for i in header]
if 'username' not in header:
return []
res = []
for row in rows:
if len(row) != len(header):
continue
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']:
res.append(cur_dict)
return res
# return result log
def import_users(users):
log = ''
for i, row in enumerate(users):
cur_log = str(i + 1) + '. '
username = row['username']
cur_log += username + ': '
pwd = row['password']
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 - '
else:
cur_log += 'Edit - '
if pwd:
user.set_password(pwd)
elif created:
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 'school' in row.keys() and row['school']:
user.last_name = row['school']
if row['organizations']:
orgs = row['organizations'].split('&')
added_orgs = []
for o in orgs:
try:
org = Organization.objects.get(slug=o)
profile.organizations.add(org)
added_orgs.append(org.name)
except Organization.DoesNotExist:
continue
if added_orgs:
cur_log += 'Added to ' + ', '.join(added_orgs) + ' - '
user.email = row['email']
user.save()
profile.save()
cur_log += 'Saved\n'
log += cur_log
log += 'FINISH'
return log