2021-07-28 22:58:42 +00:00
|
|
|
import csv
|
|
|
|
from tempfile import mktemp
|
2023-09-10 18:44:04 +00:00
|
|
|
import re
|
2021-07-28 22:58:42 +00:00
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
|
|
|
|
from judge.models import Profile, Language, Organization
|
|
|
|
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
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)",
|
|
|
|
]
|
|
|
|
|
2021-07-28 22:58:42 +00:00
|
|
|
|
|
|
|
def csv_to_dict(csv_file):
|
2022-05-14 17:57:27 +00:00
|
|
|
rows = csv.reader(csv_file.read().decode().split("\n"))
|
2021-07-28 22:58:42 +00:00
|
|
|
header = next(rows)
|
|
|
|
header = [i.lower() for i in header]
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
if "username" not in header:
|
2021-07-28 22:58:42 +00:00
|
|
|
return []
|
|
|
|
|
|
|
|
res = []
|
|
|
|
|
|
|
|
for row in rows:
|
|
|
|
if len(row) != len(header):
|
|
|
|
continue
|
2022-05-14 17:57:27 +00:00
|
|
|
cur_dict = {i: "" for i in fields}
|
2021-07-28 22:58:42 +00:00
|
|
|
for i in range(len(header)):
|
|
|
|
if header[i] not in fields:
|
|
|
|
continue
|
|
|
|
cur_dict[header[i]] = row[i]
|
2022-05-14 17:57:27 +00:00
|
|
|
if cur_dict["username"]:
|
2021-07-28 22:58:42 +00:00
|
|
|
res.append(cur_dict)
|
|
|
|
return res
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
|
2023-09-10 18:44:04 +00:00
|
|
|
def is_valid_username(username):
|
|
|
|
match = re.match(r"\w+", username)
|
|
|
|
return match is not None and match.group() == username
|
|
|
|
|
|
|
|
|
2021-07-28 22:58:42 +00:00
|
|
|
# return result log
|
|
|
|
def import_users(users):
|
2022-05-14 17:57:27 +00:00
|
|
|
log = ""
|
2021-07-28 22:58:42 +00:00
|
|
|
for i, row in enumerate(users):
|
2022-05-14 17:57:27 +00:00
|
|
|
cur_log = str(i + 1) + ". "
|
2021-07-28 22:58:42 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
username = row["username"]
|
2023-09-10 18:44:04 +00:00
|
|
|
if not is_valid_username(username):
|
|
|
|
log += username + ": Invalid username\n"
|
|
|
|
continue
|
2021-07-28 22:58:42 +00:00
|
|
|
|
2023-09-10 18:44:04 +00:00
|
|
|
cur_log += username + ": "
|
2022-05-14 17:57:27 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
)
|
2021-07-28 22:58:42 +00:00
|
|
|
|
|
|
|
if created:
|
2022-05-14 17:57:27 +00:00
|
|
|
cur_log += "Create new - "
|
2021-07-28 22:58:42 +00:00
|
|
|
else:
|
2022-05-14 17:57:27 +00:00
|
|
|
cur_log += "Edit - "
|
2021-07-28 22:58:42 +00:00
|
|
|
|
|
|
|
if pwd:
|
|
|
|
user.set_password(pwd)
|
|
|
|
elif created:
|
2022-05-14 17:57:27 +00:00
|
|
|
user.set_password("lqdoj")
|
|
|
|
cur_log += "Missing password, set password = lqdoj - "
|
2021-07-28 22:58:42 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
if "name" in row.keys() and row["name"]:
|
|
|
|
user.first_name = row["name"]
|
2021-07-28 22:58:42 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
if "school" in row.keys() and row["school"]:
|
|
|
|
user.last_name = row["school"]
|
2021-07-28 22:58:42 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
if row["organizations"]:
|
|
|
|
orgs = row["organizations"].split("&")
|
2021-07-28 22:58:42 +00:00
|
|
|
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:
|
2022-05-14 17:57:27 +00:00
|
|
|
cur_log += "Added to " + ", ".join(added_orgs) + " - "
|
|
|
|
|
|
|
|
if row["email"]:
|
|
|
|
user.email = row["email"]
|
2021-07-28 22:58:42 +00:00
|
|
|
|
|
|
|
user.save()
|
|
|
|
profile.save()
|
2022-05-14 17:57:27 +00:00
|
|
|
cur_log += "Saved\n"
|
2021-07-28 22:58:42 +00:00
|
|
|
log += cur_log
|
2022-05-14 17:57:27 +00:00
|
|
|
log += "FINISH"
|
2021-07-28 22:58:42 +00:00
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
return log
|