Reformat using black

This commit is contained in:
cuom1999 2022-05-14 12:57:27 -05:00
parent efee4ad081
commit a87fb49918
221 changed files with 19127 additions and 7310 deletions

View file

@ -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